1 /*
2  * fgetflags.c		- Get a file flags on an ext2 file system
3  *
4  * Copyright (C) 1993, 1994  Remy Card <card@masi.ibp.fr>
5  *                           Laboratoire MASI, Institut Blaise Pascal
6  *                           Universite Pierre et Marie Curie (Paris VI)
7  *
8  * %Begin-Header%
9  * This file may be redistributed under the terms of the GNU Library
10  * General Public License, version 2.
11  * %End-Header%
12  */
13 
14 /*
15  * History:
16  * 93/10/30	- Creation
17  */
18 
19 #define _LARGEFILE_SOURCE
20 #define _LARGEFILE64_SOURCE
21 
22 #if HAVE_ERRNO_H
23 #include <errno.h>
24 #endif
25 #if HAVE_UNISTD_H
26 #include <unistd.h>
27 #endif
28 #include <sys/types.h>
29 #include <sys/stat.h>
30 #if HAVE_EXT2_IOCTLS
31 #include <fcntl.h>
32 #include <sys/ioctl.h>
33 #endif
34 
35 #include "e2p.h"
36 
37 #ifdef O_LARGEFILE
38 #define OPEN_FLAGS (O_RDONLY|O_NONBLOCK|O_LARGEFILE)
39 #else
40 #define OPEN_FLAGS (O_RDONLY|O_NONBLOCK)
41 #endif
42 
fgetflags(const char * name,unsigned long * flags)43 int fgetflags (const char * name, unsigned long * flags)
44 {
45 	struct stat buf;
46 #if HAVE_STAT_FLAGS && !(APPLE_DARWIN && HAVE_EXT2_IOCTLS)
47 
48 	if (stat (name, &buf) == -1)
49 		return -1;
50 
51 	*flags = 0;
52 #ifdef UF_IMMUTABLE
53 	if (buf.st_flags & UF_IMMUTABLE)
54 		*flags |= EXT2_IMMUTABLE_FL;
55 #endif
56 #ifdef UF_APPEND
57 	if (buf.st_flags & UF_APPEND)
58 		*flags |= EXT2_APPEND_FL;
59 #endif
60 #ifdef UF_NODUMP
61 	if (buf.st_flags & UF_NODUMP)
62 		*flags |= EXT2_NODUMP_FL;
63 #endif
64 
65 	return 0;
66 #else /* !HAVE_STAT_FLAGS || (APPLE_DARWIN && HAVE_EXT2_IOCTLS) */
67 #if HAVE_EXT2_IOCTLS
68 	int fd, r, f, save_errno = 0;
69 
70 	if (!lstat(name, &buf) &&
71 	    !S_ISREG(buf.st_mode) && !S_ISDIR(buf.st_mode)) {
72 		goto notsupp;
73 	}
74 #if !APPLE_DARWIN
75 	fd = open (name, OPEN_FLAGS);
76 	if (fd == -1)
77 		return -1;
78 	r = ioctl (fd, EXT2_IOC_GETFLAGS, &f);
79 	if (r == -1)
80 		save_errno = errno;
81 	*flags = f;
82 	close (fd);
83 	if (save_errno)
84 		errno = save_errno;
85 	return r;
86 #else /* APPLE_DARWIN */
87    f = -1;
88    save_errno = syscall(SYS_fsctl, name, EXT2_IOC_GETFLAGS, &f, 0);
89    *flags = f;
90    return (save_errno);
91 #endif /* !APPLE_DARWIN */
92 notsupp:
93 #endif /* HAVE_EXT2_IOCTLS */
94 #endif
95 	errno = EOPNOTSUPP;
96 	return -1;
97 }
98