summaryrefslogtreecommitdiff
path: root/usr/src/lib/libpkg/common/isdir.c
blob: 1e5b1e94bb2ad1e5445177b31ab21b2ad40f148b (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
/*
 * CDDL HEADER START
 *
 * The contents of this file are subject to the terms of the
 * Common Development and Distribution License (the "License").
 * You may not use this file except in compliance with the License.
 *
 * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
 * or http://www.opensolaris.org/os/licensing.
 * See the License for the specific language governing permissions
 * and limitations under the License.
 *
 * When distributing Covered Code, include this CDDL HEADER in each
 * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
 * If applicable, add the following below this CDDL HEADER, with the
 * fields enclosed by brackets "[]" replaced with your own identifying
 * information: Portions Copyright [yyyy] [name of copyright owner]
 *
 * CDDL HEADER END
 */

/*
 * Copyright 2004 Sun Microsystems, Inc.  All rights reserved.
 * Use is subject to license terms.
 * Copyright 2015 Nexenta Systems, Inc. All rights reserved.
 */

/* Copyright (c) 1984, 1986, 1987, 1988, 1989 AT&T */
/* All Rights Reserved */


#include <stdio.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <archives.h>
#include <errno.h>
#include <fcntl.h>
#include <limits.h>
#include <stdlib.h>
#include <unistd.h>
#include <string.h>
#include "pkglocale.h"
#include "pkglibmsgs.h"

/*
 * Defines for cpio/compression checks.
 */
#define	BIT_MASK		0x1f
#define	BLOCK_MASK		0x80

#define	MASK_CK(x, y)	(((x) & (y)) == (y))
#define	ISCOMPCPIO	((unsigned char) cm.c_mag[0] == m_h[0] && \
			(unsigned char) cm.c_mag[1] == m_h[1] && \
			(MASK_CK((unsigned char) cm.c_mag[2], BLOCK_MASK) || \
			MASK_CK((unsigned char) cm.c_mag[2], BIT_MASK)))

#define	ISCPIO		(cm.b_mag != CMN_BIN && \
			(strcmp(cm.c_mag, CMS_ASC) == 0) && \
			(strcmp(cm.c_mag, CMS_CHR) == 0) && \
			(strcmp(cm.c_mag, CMS_CRC) == 0))

/* location of distributed file system types database */

#define	REMOTE_FS_DBFILE	"/etc/dfs/fstypes"

/* character array used to hold dfs types database contents */

static long		numRemoteFstypes = -1;
static char		**remoteFstypes = (char **)NULL;

/* forward declarations */

static void _InitRemoteFstypes(void);

int isFdRemote(int a_fd);
int isPathRemote(char *a_path);
int isFstypeRemote(char *a_fstype);
int isdir(char *path);
int isfile(char *dir, char *file);
int iscpio(char *path, int *iscomp);

/*
 * Name:	isdir
 * Description:	determine if specified path exists and is a directory
 * Arguments:	path - pointer to string representing the path to verify
 * returns: 0 - directory exists
 *	    1 - directory does not exist or is not a directory
 * NOTE:	errno is set appropriately
 */

int
isdir(char *path)
{
	struct stat statbuf;

	/* return error if path does not exist */

	if (stat(path, &statbuf) != 0) {
		return (1);
	}

	/* return error if path is not a directory */

	if ((statbuf.st_mode & S_IFMT) != S_IFDIR) {
		errno = ENOTDIR;
		return (1);
	}

	return (0);
}

/*
 * Name:	isfile
 * Description:	determine if specified path exists and is a directory
 * Arguments:	dir - pointer to string representing the directory where
 *			the file is located
 *			== NULL - use "file" argument only
 *		file - pointer to string representing the file to verify
 * Returns:	0 - success - file exists
 *		1 - failure - file does not exist OR is not a file
 * NOTE:	errno is set appropriately
 */

int
isfile(char *dir, char *file)
{
	struct stat statbuf;
	char	path[PATH_MAX];

	/* construct full path if directory specified */

	if (dir) {
		(void) snprintf(path, sizeof (path), "%s/%s", dir, file);
		file = path;
	}

	/* return error if path does not exist */

	if (stat(file, &statbuf) != 0) {
		return (1);
	}

	/* return error if path is a directory */

	if ((statbuf.st_mode & S_IFMT) == S_IFDIR) {
		errno = EISDIR;
		return (1);
	}

	/* return error if path is not a file */

	if ((statbuf.st_mode & S_IFMT) != S_IFREG) {
		errno = EINVAL;
		return (1);
	}

	return (0);
}

int
iscpio(char *path, int *iscomp)
{
	/*
	 * Compressed File Header.
	 */
	unsigned char m_h[] = { "\037\235" };		/* 1F 9D */

	static union {
		short int	b_mag;
		char		c_mag[CMS_LEN];
	}	cm;

	struct stat	statb;
	int		fd;


	*iscomp = 0;

	if ((fd = open(path, O_RDONLY, 0)) == -1) {
		if (errno != ENOENT) {
			perror("");
			(void) fprintf(stderr, pkg_gt(ERR_ISCPIO_OPEN), path);
		}
		return (0);
	} else {
		if (fstat(fd, &statb) == -1) {
			perror("");
			(void) fprintf(stderr, pkg_gt(ERR_ISCPIO_FSTAT), path);
			(void) close(fd);
			return (0);
		} else {
			if (S_ISREG(statb.st_mode)) {	/* Must be a file */
				if (read(fd, cm.c_mag, sizeof (cm.c_mag)) !=
				    sizeof (cm.c_mag)) {
					perror("");
					(void) fprintf(stderr,
					    pkg_gt(ERR_ISCPIO_READ), path);
					(void) close(fd);
					return (0);
				}
				/*
				 * Try to determine if the file is a compressed
				 * file, if that fails, try to determine if it
				 * is a cpio archive, if that fails, then we
				 * fail!
				 */
				if (ISCOMPCPIO) {
					*iscomp = 1;
					(void) close(fd);
					return (1);
				} else if (ISCPIO) {
					(void) fprintf(stderr,
					    pkg_gt(ERR_ISCPIO_NOCPIO),
					    path);
					(void) close(fd);
					return (0);
				}
				(void) close(fd);
				return (1);
			} else {
				(void) close(fd);
				return (0);
			}
		}
	}
}

/*
 * Name:	isPathRemote
 * Description:	determine if a path object is local or remote
 * Arguments:	a_path - [RO, *RO] - (char *)
 *			Pointer to string representing the path to check
 * Returns:	int
 *			1 - the path is remote
 *			0 - the path is local to this system
 *			-1 - cannot determine if path is remote or local
 */

int
isPathRemote(char *a_path)
{
	int		r;
	struct stat	statbuf;

	r = lstat(a_path, &statbuf);
	if (r < 0) {
		return (-1);
	}

	return (isFstypeRemote(statbuf.st_fstype));
}

/*
 * Name:	isFdRemote
 * Description:	determine if an open file is local or remote
 * Arguments:	a_fd - [RO, *RO] - (int)
 *			Integer representing open file to check
 * Returns:	int
 *			1 - the path is remote
 *			0 - the path is local to this system
 *			-1 - cannot determine if path is remote or local
 */

int
isFdRemote(int a_fd)
{
	int		r;
	struct stat	statbuf;

	r = fstat(a_fd, &statbuf);
	if (r < 0) {
		return (-1);
	}

	return (isFstypeRemote(statbuf.st_fstype));
}

/*
 * Name:	isFstypeRemote
 * Description:	determine if a file system type is remote (distributed)
 * Arguments:	a_fstype - [RO, *RO] - (char *)
 *			Pointer to string representing the file system type
 *			to check
 * Returns:	int
 *			1 - the file system type is remote
 *			0 - the file system type is local to this system
 */

int
isFstypeRemote(char *a_fstype)
{
	int	i;

	/* initialize the list if it is not yet initialized */

	_InitRemoteFstypes();

	/* scan the list looking for the specified type */

	for (i = 0; i < numRemoteFstypes; i++) {
		if (strcmp(remoteFstypes[i], a_fstype) == 0) {
			return (1);
		}
	}

	/* type not found in remote file system type list - is not remote */

	return (0);
}

/*
 * Name:	_InitRemoteFstypes
 * Description:	initialize table of remote file system type names
 * Arguments:	none
 * Returns:	none
 * Side Effects:
 *	- The global array "(char **)remoteFstypes" is set to the
 *	  address of an array of string pointers, each of which represents
 *	  a single remote file system type
 *	- The global variable "(long) numRemoteFstypes" is set to the total
 *	  number of remote file system type strings (names) that are
 *	  contained in the "remoteFstypes" global array.
 *	- numRemoteFstypes is initialized to "-1" before any attempt has been
 *	  made to read the remote file system type name database.
 */
static void
_InitRemoteFstypes(void)
{
	FILE    *fp;
	char    line_buf[LINE_MAX];

	/* return if already initialized */

	if (numRemoteFstypes > 0) {
		return;
	}

	/* if list is uninitialized, start with zero */

	if (numRemoteFstypes == -1) {
		numRemoteFstypes = 0;
	}

	/* open the remote file system type database file */

	if ((fp = fopen(REMOTE_FS_DBFILE, "r")) == NULL) {
		/* no remote type database: use predefined remote types */
		remoteFstypes = (char **)realloc(remoteFstypes,
		    sizeof (char *) * (numRemoteFstypes+2));
		remoteFstypes[numRemoteFstypes++] = "nfs";	/* +1 */
		remoteFstypes[numRemoteFstypes++] = "autofs";	/* +2 */
		return;
	}

	/*
	 * Read the remote file system type database; from fstypes(5):
	 *
	 * fstypes resides in directory /etc/dfs and lists distributed file
	 * system utilities packages installed on the system. For each installed
	 * distributed file system type, there is a line that begins with the
	 * file system type name (for example, ``nfs''), followed by white space
	 * and descriptive text.
	 *
	 * Lines will look at lot like this:
	 *
	 *	nfs NFS Utilities
	 *	autofs AUTOFS Utilities
	 */

	while (fgets(line_buf, sizeof (line_buf), fp) != NULL) {
		char		buf[LINE_MAX];
		static char	format[128] = {'\0'};

		if (format[0] == '\0') {
			/* create bounded format: %ns */
			(void) snprintf(format, sizeof (format),
			    "%%%ds", sizeof (buf)-1);
		}

		(void) sscanf(line_buf, format, buf);

		remoteFstypes = realloc(remoteFstypes,
		    sizeof (char *) * (numRemoteFstypes+1));
		remoteFstypes[numRemoteFstypes++] = strdup(buf);
	}

	/* close database file and return */

	(void) fclose(fp);
}