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 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 int fd, r, f, save_errno = 0; 84 struct stat buf; 85 86 fd = open(name, OPEN_FLAGS); 87 if (fd == -1) { 88 if (errno == ELOOP || errno == ENXIO) 89 errno = EOPNOTSUPP; 90 return -1; 91 } 92 f = (int) flags; 93 r = ioctl(fd, EXT2_IOC_SETFLAGS, &f); 94 if (r == -1) { 95 if (errno == ENOTTY) 96 errno = EOPNOTSUPP; 97 save_errno = errno; 98 } 99 close(fd); 100 if (save_errno) 101 errno = save_errno; 102 return r; 103 #else 104 errno = EOPNOTSUPP; 105 return -1; 106 #endif 107 }