1 /*
2  * fsetflags.c		- Set 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 /*
43  * Deal with lame glibc's that define this function without actually
44  * implementing it.  Can you say "attractive nuisance", boys and girls?
45  * I knew you could!
46  */
47 #ifdef __linux__
48 #undef HAVE_CHFLAGS
49 #endif
50 
51 #ifndef O_LARGEFILE
52 #define O_LARGEFILE 0
53 #endif
54 #ifndef O_NOFOLLOW
55 #define O_NOFOLLOW 0
56 #endif
57 
58 #define OPEN_FLAGS (O_RDONLY|O_NONBLOCK|O_LARGEFILE|O_NOFOLLOW)
59 
fsetflags(const char * name,unsigned long flags)60 int fsetflags (const char * name, unsigned long flags)
61 {
62 #if HAVE_CHFLAGS && !(APPLE_DARWIN && HAVE_EXT2_IOCTLS)
63 	unsigned long bsd_flags = 0;
64 
65 #ifdef UF_IMMUTABLE
66 	if (flags & EXT2_IMMUTABLE_FL)
67 		bsd_flags |= UF_IMMUTABLE;
68 #endif
69 #ifdef UF_APPEND
70 	if (flags & EXT2_APPEND_FL)
71 		bsd_flags |= UF_APPEND;
72 #endif
73 #ifdef UF_NODUMP
74 	if (flags & EXT2_NODUMP_FL)
75 		bsd_flags |= UF_NODUMP;
76 #endif
77 
78 	return chflags (name, bsd_flags);
79 #elif APPLE_DARWIN && HAVE_EXT2_IOCTLS
80 	int f = (int) flags;
81 	return syscall(SYS_fsctl, name, EXT2_IOC_SETFLAGS, &f, 0);
82 #elif HAVE_EXT2_IOCTLS
83 	struct stat buf;
84 	int fd, r, f, save_errno = 0;
85 
86 	if (!stat(name, &buf) &&
87 	    !S_ISREG(buf.st_mode) && !S_ISDIR(buf.st_mode)) {
88 		errno = EOPNOTSUPP;
89 		return -1;
90 	}
91 	fd = open(name, OPEN_FLAGS);
92 	if (fd == -1) {
93 		if (errno == ELOOP || errno == ENXIO)
94 			errno = EOPNOTSUPP;
95 		return -1;
96 	}
97 	if (!fstat(fd, &buf) &&
98 	    !S_ISREG(buf.st_mode) && !S_ISDIR(buf.st_mode)) {
99 		close(fd);
100 		errno = EOPNOTSUPP;
101 		return -1;
102 	}
103 	f = (int) flags;
104 	r = ioctl(fd, EXT2_IOC_SETFLAGS, &f);
105 	if (r == -1) {
106 		if (errno == ENOTTY)
107 			errno = EOPNOTSUPP;
108 		save_errno = errno;
109 	}
110 	close(fd);
111 	if (save_errno)
112 		errno = save_errno;
113 	return r;
114 #else
115 	errno = EOPNOTSUPP;
116 	return -1;
117 #endif
118 }
119