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 #ifndef _LARGEFILE_SOURCE
20 #define _LARGEFILE_SOURCE
21 #endif
22 #ifndef _LARGEFILE64_SOURCE
23 #define _LARGEFILE64_SOURCE
24 #endif
25
26 #include "config.h"
27 #if HAVE_ERRNO_H
28 #include <errno.h>
29 #endif
30 #if HAVE_UNISTD_H
31 #include <unistd.h>
32 #endif
33 #include <sys/types.h>
34 #include <sys/stat.h>
35 #if HAVE_EXT2_IOCTLS
36 #include <fcntl.h>
37 #include <sys/ioctl.h>
38 #endif
39
40 #include "e2p.h"
41
42 #ifndef O_LARGEFILE
43 #define O_LARGEFILE 0
44 #endif
45 #ifndef O_NOFOLLOW
46 #define O_NOFOLLOW 0
47 #endif
48
49 #define OPEN_FLAGS (O_RDONLY|O_NONBLOCK|O_LARGEFILE|O_NOFOLLOW)
50
fgetflags(const char * name,unsigned long * flags)51 int fgetflags (const char * name, unsigned long * flags)
52 {
53 #if HAVE_STAT_FLAGS && !(APPLE_DARWIN && HAVE_EXT2_IOCTLS)
54 struct stat buf;
55
56 if (stat (name, &buf) == -1)
57 return -1;
58
59 *flags = 0;
60 #ifdef UF_IMMUTABLE
61 if (buf.st_flags & UF_IMMUTABLE)
62 *flags |= EXT2_IMMUTABLE_FL;
63 #endif
64 #ifdef UF_APPEND
65 if (buf.st_flags & UF_APPEND)
66 *flags |= EXT2_APPEND_FL;
67 #endif
68 #ifdef UF_NODUMP
69 if (buf.st_flags & UF_NODUMP)
70 *flags |= EXT2_NODUMP_FL;
71 #endif
72
73 return 0;
74 #elif APPLE_DARWIN && HAVE_EXT2_IOCTLS
75 int f, save_errno = 0;
76
77 f = -1;
78 save_errno = syscall(SYS_fsctl, name, EXT2_IOC_GETFLAGS, &f, 0);
79 *flags = f;
80 return (save_errno);
81 #elif HAVE_EXT2_IOCTLS
82 struct stat buf;
83 int fd, r, f, save_errno = 0;
84
85 if (!stat(name, &buf) &&
86 !S_ISREG(buf.st_mode) && !S_ISDIR(buf.st_mode)) {
87 errno = EOPNOTSUPP;
88 return -1;
89 }
90 fd = open(name, OPEN_FLAGS);
91 if (fd == -1) {
92 if (errno == ELOOP || errno == ENXIO)
93 errno = EOPNOTSUPP;
94 return -1;
95 }
96 if (!fstat(fd, &buf) &&
97 !S_ISREG(buf.st_mode) && !S_ISDIR(buf.st_mode)) {
98 close(fd);
99 errno = EOPNOTSUPP;
100 return -1;
101 }
102 r = ioctl(fd, EXT2_IOC_GETFLAGS, &f);
103 if (r == -1) {
104 if (errno == ENOTTY)
105 errno = EOPNOTSUPP;
106 save_errno = errno;
107 }
108 *flags = f;
109 close(fd);
110 if (save_errno)
111 errno = save_errno;
112 return r;
113 #else
114 errno = EOPNOTSUPP;
115 return -1;
116 #endif
117 }
118