paul@181 | 1 | /* |
paul@181 | 2 | * setflags.c - Set a file flags on an ext2 file system |
paul@181 | 3 | * |
paul@181 | 4 | * Copyright (C) 1993, 1994 Remy Card <card@masi.ibp.fr> |
paul@181 | 5 | * Laboratoire MASI, Institut Blaise Pascal |
paul@181 | 6 | * Universite Pierre et Marie Curie (Paris VI) |
paul@181 | 7 | * |
paul@181 | 8 | * %Begin-Header% |
paul@181 | 9 | * This file may be redistributed under the terms of the GNU Library |
paul@181 | 10 | * General Public License, version 2. |
paul@181 | 11 | * %End-Header% |
paul@181 | 12 | */ |
paul@181 | 13 | |
paul@181 | 14 | /* |
paul@181 | 15 | * History: |
paul@181 | 16 | * 93/10/30 - Creation |
paul@181 | 17 | */ |
paul@181 | 18 | |
paul@181 | 19 | #include "config.h" |
paul@181 | 20 | #if HAVE_ERRNO_H |
paul@181 | 21 | #include <errno.h> |
paul@181 | 22 | #endif |
paul@181 | 23 | #include <sys/types.h> |
paul@181 | 24 | #include <sys/stat.h> |
paul@181 | 25 | #if HAVE_EXT2_IOCTLS |
paul@181 | 26 | #include <sys/ioctl.h> |
paul@181 | 27 | #endif |
paul@181 | 28 | |
paul@181 | 29 | #include "e2p.h" |
paul@181 | 30 | |
paul@181 | 31 | /* |
paul@181 | 32 | * Deal with lame glibc's that define this function without actually |
paul@181 | 33 | * implementing it. Can you say "attractive nuisance", boys and girls? |
paul@181 | 34 | * I knew you could! |
paul@181 | 35 | */ |
paul@181 | 36 | #ifdef __linux__ |
paul@181 | 37 | #undef HAVE_CHFLAGS |
paul@181 | 38 | #endif |
paul@181 | 39 | |
paul@181 | 40 | int setflags (int fd, unsigned long flags) |
paul@181 | 41 | { |
paul@181 | 42 | #if HAVE_CHFLAGS |
paul@181 | 43 | unsigned long bsd_flags = 0; |
paul@181 | 44 | |
paul@181 | 45 | #ifdef UF_IMMUTABLE |
paul@181 | 46 | if (flags & EXT2_IMMUTABLE_FL) |
paul@181 | 47 | bsd_flags |= UF_IMMUTABLE; |
paul@181 | 48 | #endif |
paul@181 | 49 | #ifdef UF_APPEND |
paul@181 | 50 | if (flags & EXT2_APPEND_FL) |
paul@181 | 51 | bsd_flags |= UF_APPEND; |
paul@181 | 52 | #endif |
paul@181 | 53 | #ifdef UF_NODUMP |
paul@181 | 54 | if (flags & EXT2_NODUMP_FL) |
paul@181 | 55 | bsd_flags |= UF_NODUMP; |
paul@181 | 56 | #endif |
paul@181 | 57 | |
paul@181 | 58 | return fchflags (fd, bsd_flags); |
paul@181 | 59 | #else /* ! HAVE_CHFLAGS */ |
paul@181 | 60 | #if HAVE_EXT2_IOCTLS |
paul@181 | 61 | struct stat buf; |
paul@181 | 62 | int f; |
paul@181 | 63 | |
paul@181 | 64 | if (!fstat(fd, &buf) && |
paul@181 | 65 | !S_ISREG(buf.st_mode) && !S_ISDIR(buf.st_mode)) { |
paul@181 | 66 | errno = EOPNOTSUPP; |
paul@181 | 67 | return -1; |
paul@181 | 68 | } |
paul@181 | 69 | f = (int) flags; |
paul@181 | 70 | |
paul@181 | 71 | return ioctl(fd, EXT2_IOC_SETFLAGS, &f); |
paul@181 | 72 | #else |
paul@181 | 73 | errno = EOPNOTSUPP; |
paul@181 | 74 | return -1; |
paul@181 | 75 | #endif /* HAVE_EXT2_IOCTLS */ |
paul@181 | 76 | #endif /* HAVE_CHFLAGS */ |
paul@181 | 77 | } |