Theodore Ts'o | 3839e65 | 1997-04-26 13:21:57 +0000 | [diff] [blame] | 1 | /* |
| 2 | * setflags.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 | * |
Theodore Ts'o | 543547a | 2010-05-17 21:31:56 -0400 | [diff] [blame] | 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% |
Theodore Ts'o | 3839e65 | 1997-04-26 13:21:57 +0000 | [diff] [blame] | 12 | */ |
| 13 | |
| 14 | /* |
| 15 | * History: |
| 16 | * 93/10/30 - Creation |
| 17 | */ |
| 18 | |
Theodore Ts'o | 50e1e10 | 1997-04-26 13:58:21 +0000 | [diff] [blame] | 19 | #if HAVE_ERRNO_H |
Theodore Ts'o | 3839e65 | 1997-04-26 13:21:57 +0000 | [diff] [blame] | 20 | #include <errno.h> |
Theodore Ts'o | 50e1e10 | 1997-04-26 13:58:21 +0000 | [diff] [blame] | 21 | #endif |
Theodore Ts'o | 50e1e10 | 1997-04-26 13:58:21 +0000 | [diff] [blame] | 22 | #include <sys/types.h> |
Theodore Ts'o | 023d111 | 2002-08-17 14:44:56 -0400 | [diff] [blame] | 23 | #include <sys/stat.h> |
| 24 | #if HAVE_EXT2_IOCTLS |
Theodore Ts'o | 3839e65 | 1997-04-26 13:21:57 +0000 | [diff] [blame] | 25 | #include <sys/ioctl.h> |
Theodore Ts'o | 50e1e10 | 1997-04-26 13:58:21 +0000 | [diff] [blame] | 26 | #endif |
Theodore Ts'o | 3839e65 | 1997-04-26 13:21:57 +0000 | [diff] [blame] | 27 | |
Theodore Ts'o | 3839e65 | 1997-04-26 13:21:57 +0000 | [diff] [blame] | 28 | #include "e2p.h" |
| 29 | |
Theodore Ts'o | efc6f62 | 2008-08-27 23:07:54 -0400 | [diff] [blame] | 30 | /* |
| 31 | * Deal with lame glibc's that define this function without actually |
Theodore Ts'o | 919994a | 2004-06-25 00:52:08 -0400 | [diff] [blame] | 32 | * implementing it. Can you say "attractive nuisance", boys and girls? |
| 33 | * I knew you could! |
| 34 | */ |
| 35 | #ifdef __linux__ |
| 36 | #undef HAVE_CHFLAGS |
| 37 | #endif |
| 38 | |
Theodore Ts'o | 3839e65 | 1997-04-26 13:21:57 +0000 | [diff] [blame] | 39 | int setflags (int fd, unsigned long flags) |
| 40 | { |
Theodore Ts'o | 50e1e10 | 1997-04-26 13:58:21 +0000 | [diff] [blame] | 41 | #if HAVE_CHFLAGS |
Theodore Ts'o | cce382b | 1998-03-09 13:07:09 +0000 | [diff] [blame] | 42 | unsigned long bsd_flags = 0; |
Theodore Ts'o | 50e1e10 | 1997-04-26 13:58:21 +0000 | [diff] [blame] | 43 | |
| 44 | #ifdef UF_IMMUTABLE |
Theodore Ts'o | cce382b | 1998-03-09 13:07:09 +0000 | [diff] [blame] | 45 | if (flags & EXT2_IMMUTABLE_FL) |
| 46 | bsd_flags |= UF_IMMUTABLE; |
Theodore Ts'o | 50e1e10 | 1997-04-26 13:58:21 +0000 | [diff] [blame] | 47 | #endif |
| 48 | #ifdef UF_APPEND |
Theodore Ts'o | cce382b | 1998-03-09 13:07:09 +0000 | [diff] [blame] | 49 | if (flags & EXT2_APPEND_FL) |
| 50 | bsd_flags |= UF_APPEND; |
Theodore Ts'o | 50e1e10 | 1997-04-26 13:58:21 +0000 | [diff] [blame] | 51 | #endif |
| 52 | #ifdef UF_NODUMP |
Theodore Ts'o | cce382b | 1998-03-09 13:07:09 +0000 | [diff] [blame] | 53 | if (flags & EXT2_NODUMP_FL) |
| 54 | bsd_flags |= UF_NODUMP; |
Theodore Ts'o | 50e1e10 | 1997-04-26 13:58:21 +0000 | [diff] [blame] | 55 | #endif |
| 56 | |
Theodore Ts'o | cce382b | 1998-03-09 13:07:09 +0000 | [diff] [blame] | 57 | return fchflags (fd, bsd_flags); |
JP Abgrall | e0ed740 | 2014-03-19 19:08:39 -0700 | [diff] [blame] | 58 | #else /* ! HAVE_CHFLAGS */ |
Theodore Ts'o | 50e1e10 | 1997-04-26 13:58:21 +0000 | [diff] [blame] | 59 | #if HAVE_EXT2_IOCTLS |
JP Abgrall | e0ed740 | 2014-03-19 19:08:39 -0700 | [diff] [blame] | 60 | struct stat buf; |
Theodore Ts'o | cce382b | 1998-03-09 13:07:09 +0000 | [diff] [blame] | 61 | int f; |
| 62 | |
Theodore Ts'o | 023d111 | 2002-08-17 14:44:56 -0400 | [diff] [blame] | 63 | if (!fstat(fd, &buf) && |
| 64 | !S_ISREG(buf.st_mode) && !S_ISDIR(buf.st_mode)) { |
| 65 | errno = EOPNOTSUPP; |
| 66 | return -1; |
| 67 | } |
Theodore Ts'o | cce382b | 1998-03-09 13:07:09 +0000 | [diff] [blame] | 68 | f = (int) flags; |
JP Abgrall | e0ed740 | 2014-03-19 19:08:39 -0700 | [diff] [blame] | 69 | |
| 70 | return ioctl(fd, EXT2_IOC_SETFLAGS, &f); |
| 71 | #else |
Theodore Ts'o | 50e1e10 | 1997-04-26 13:58:21 +0000 | [diff] [blame] | 72 | errno = EOPNOTSUPP; |
| 73 | return -1; |
JP Abgrall | e0ed740 | 2014-03-19 19:08:39 -0700 | [diff] [blame] | 74 | #endif /* HAVE_EXT2_IOCTLS */ |
| 75 | #endif /* HAVE_CHFLAGS */ |
Theodore Ts'o | 3839e65 | 1997-04-26 13:21:57 +0000 | [diff] [blame] | 76 | } |