xref: /illumos-gate/usr/src/lib/libpkg/common/devtype.c (revision d2f7972d81337947df76c36b8c2a5f290829fa7a)
1 /*
2  * CDDL HEADER START
3  *
4  * The contents of this file are subject to the terms of the
5  * Common Development and Distribution License (the "License").
6  * You may not use this file except in compliance with the License.
7  *
8  * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
9  * or http://www.opensolaris.org/os/licensing.
10  * See the License for the specific language governing permissions
11  * and limitations under the License.
12  *
13  * When distributing Covered Code, include this CDDL HEADER in each
14  * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
15  * If applicable, add the following below this CDDL HEADER, with the
16  * fields enclosed by brackets "[]" replaced with your own identifying
17  * information: Portions Copyright [yyyy] [name of copyright owner]
18  *
19  * CDDL HEADER END
20  */
21 
22 /*
23  * Copyright 2009 Sun Microsystems, Inc.  All rights reserved.
24  * Use is subject to license terms.
25  */
26 
27 /* Copyright (c) 1984, 1986, 1987, 1988, 1989 AT&T */
28 /* All Rights Reserved */
29 
30 
31 
32 #include <stdio.h>
33 #include <string.h>
34 #include <stdlib.h>
35 #include <unistd.h>
36 #include "pkgdev.h"
37 #include "pkglib.h"
38 
39 extern char	*devattr(char *device, char *attribute);	/* libadm.a */
40 
41 int
42 devtype(char *alias, struct pkgdev *devp)
43 {
44 	char *name;
45 	devp->mntflg = 0;
46 	devp->name = alias;
47 	devp->dirname = devp->pathname = devp->mount = NULL;
48 	devp->fstyp = devp->cdevice = devp->bdevice = devp->norewind = NULL;
49 	devp->rdonly = 0;
50 	devp->capacity = 0;
51 
52 	/* see if alias represents an existing file */
53 	if (alias[0] == '/') {
54 		if (!isdir(alias)) {
55 			devp->dirname = devp->name;
56 			return (0);
57 		}
58 	}
59 
60 	/* see if alias represents a mountable device (e.g., a floppy) */
61 	if ((devp->mount = devattr(alias, "mountpt")) != NULL &&
62 	    devp->mount[0] != '\0') {
63 		devp->bdevice = devattr(alias, "bdevice");
64 		if (!devp->bdevice || !devp->bdevice[0]) {
65 			if (devp->bdevice) {
66 				free(devp->bdevice);
67 				devp->bdevice = NULL;
68 			}
69 			return (-1);
70 		}
71 		devp->dirname = devp->mount;
72 	} else if (devp->mount) {
73 		free(devp->mount);
74 		devp->mount = NULL;
75 	}
76 
77 	devp->cdevice = devattr(alias, "cdevice");
78 	if (devp->cdevice && devp->cdevice[0])  {
79 		/* check for capacity */
80 		if (name = devattr(alias, "capacity")) {
81 			if (name[0])
82 				devp->capacity = atoll(name);
83 			free(name);
84 		}
85 		/* check for norewind device */
86 		devp->norewind = devattr(alias, "norewind");
87 		if (devp->norewind && !devp->norewind[0]) {
88 			free(devp->norewind);
89 			devp->norewind = NULL;
90 		}
91 
92 		/* mountable devices will always have associated raw device */
93 		return (0);
94 	}
95 	if (devp->cdevice) {
96 		free(devp->cdevice);
97 		devp->cdevice = NULL;
98 	}
99 	/*
100 	 * if it is not a raw device, it must be a directory or a regular file
101 	 */
102 	name = devattr(alias, "pathname");
103 	if (!name || !name[0]) {
104 		/* Assume a regular file */
105 		if (name)
106 			free(name);
107 		devp->pathname = alias;
108 		return (0);
109 	}
110 	if (!isdir(name))
111 		devp->dirname = name;
112 	else
113 		devp->pathname = name;
114 	return (0);
115 }
116