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