• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 #ifdef O_LARGEFILE
52 #define OPEN_FLAGS (O_RDONLY|O_NONBLOCK|O_LARGEFILE)
53 #else
54 #define OPEN_FLAGS (O_RDONLY|O_NONBLOCK)
55 #endif
56 
fsetflags(const char * name,unsigned long flags)57 int fsetflags (const char * name, unsigned long flags)
58 {
59 #if HAVE_CHFLAGS && !(APPLE_DARWIN && HAVE_EXT2_IOCTLS)
60 	unsigned long bsd_flags = 0;
61 
62 #ifdef UF_IMMUTABLE
63 	if (flags & EXT2_IMMUTABLE_FL)
64 		bsd_flags |= UF_IMMUTABLE;
65 #endif
66 #ifdef UF_APPEND
67 	if (flags & EXT2_APPEND_FL)
68 		bsd_flags |= UF_APPEND;
69 #endif
70 #ifdef UF_NODUMP
71 	if (flags & EXT2_NODUMP_FL)
72 		bsd_flags |= UF_NODUMP;
73 #endif
74 
75 	return chflags (name, bsd_flags);
76 #else /* !HAVE_CHFLAGS || (APPLE_DARWIN && HAVE_EXT2_IOCTLS) */
77 #if HAVE_EXT2_IOCTLS
78 	int fd, r, f, save_errno = 0;
79 	struct stat buf;
80 
81 	if (!lstat(name, &buf) &&
82 	    !S_ISREG(buf.st_mode) && !S_ISDIR(buf.st_mode)) {
83 		goto notsupp;
84 	}
85 #if !APPLE_DARWIN
86 	fd = open (name, OPEN_FLAGS);
87 	if (fd == -1)
88 		return -1;
89 	f = (int) flags;
90 	r = ioctl (fd, EXT2_IOC_SETFLAGS, &f);
91 	if (r == -1)
92 		save_errno = errno;
93 	close (fd);
94 	if (save_errno)
95 		errno = save_errno;
96 #else /* APPLE_DARWIN */
97 	f = (int) flags;
98 	return syscall(SYS_fsctl, name, EXT2_IOC_SETFLAGS, &f, 0);
99 #endif /* !APPLE_DARWIN */
100 	return r;
101 
102 notsupp:
103 #endif /* HAVE_EXT2_IOCTLS */
104 #endif
105 	errno = EOPNOTSUPP;
106 	return -1;
107 }
108