blob: b203606fa55a63728b81bfae4ae8a727f65ce918 [file] [log] [blame]
Theodore Ts'o3839e651997-04-26 13:21:57 +00001/*
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'o543547a2010-05-17 21:31:56 -04008 * %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'o3839e651997-04-26 13:21:57 +000012 */
13
14/*
15 * History:
16 * 93/10/30 - Creation
17 */
18
Theodore Ts'o50e1e101997-04-26 13:58:21 +000019#if HAVE_ERRNO_H
Theodore Ts'o3839e651997-04-26 13:21:57 +000020#include <errno.h>
Theodore Ts'o50e1e101997-04-26 13:58:21 +000021#endif
Theodore Ts'o50e1e101997-04-26 13:58:21 +000022#include <sys/types.h>
Theodore Ts'o023d1112002-08-17 14:44:56 -040023#include <sys/stat.h>
24#if HAVE_EXT2_IOCTLS
Theodore Ts'o3839e651997-04-26 13:21:57 +000025#include <sys/ioctl.h>
Theodore Ts'o50e1e101997-04-26 13:58:21 +000026#endif
Theodore Ts'o3839e651997-04-26 13:21:57 +000027
Theodore Ts'o3839e651997-04-26 13:21:57 +000028#include "e2p.h"
29
Theodore Ts'oefc6f622008-08-27 23:07:54 -040030/*
31 * Deal with lame glibc's that define this function without actually
Theodore Ts'o919994a2004-06-25 00:52:08 -040032 * 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'o3839e651997-04-26 13:21:57 +000039int setflags (int fd, unsigned long flags)
40{
Theodore Ts'o50e1e101997-04-26 13:58:21 +000041#if HAVE_CHFLAGS
Theodore Ts'occe382b1998-03-09 13:07:09 +000042 unsigned long bsd_flags = 0;
Theodore Ts'o50e1e101997-04-26 13:58:21 +000043
44#ifdef UF_IMMUTABLE
Theodore Ts'occe382b1998-03-09 13:07:09 +000045 if (flags & EXT2_IMMUTABLE_FL)
46 bsd_flags |= UF_IMMUTABLE;
Theodore Ts'o50e1e101997-04-26 13:58:21 +000047#endif
48#ifdef UF_APPEND
Theodore Ts'occe382b1998-03-09 13:07:09 +000049 if (flags & EXT2_APPEND_FL)
50 bsd_flags |= UF_APPEND;
Theodore Ts'o50e1e101997-04-26 13:58:21 +000051#endif
52#ifdef UF_NODUMP
Theodore Ts'occe382b1998-03-09 13:07:09 +000053 if (flags & EXT2_NODUMP_FL)
54 bsd_flags |= UF_NODUMP;
Theodore Ts'o50e1e101997-04-26 13:58:21 +000055#endif
56
Theodore Ts'occe382b1998-03-09 13:07:09 +000057 return fchflags (fd, bsd_flags);
JP Abgralle0ed7402014-03-19 19:08:39 -070058#else /* ! HAVE_CHFLAGS */
Theodore Ts'o50e1e101997-04-26 13:58:21 +000059#if HAVE_EXT2_IOCTLS
JP Abgralle0ed7402014-03-19 19:08:39 -070060 struct stat buf;
Theodore Ts'occe382b1998-03-09 13:07:09 +000061 int f;
62
Theodore Ts'o023d1112002-08-17 14:44:56 -040063 if (!fstat(fd, &buf) &&
64 !S_ISREG(buf.st_mode) && !S_ISDIR(buf.st_mode)) {
65 errno = EOPNOTSUPP;
66 return -1;
67 }
Theodore Ts'occe382b1998-03-09 13:07:09 +000068 f = (int) flags;
JP Abgralle0ed7402014-03-19 19:08:39 -070069
70 return ioctl(fd, EXT2_IOC_SETFLAGS, &f);
71#else
Theodore Ts'o50e1e101997-04-26 13:58:21 +000072 errno = EOPNOTSUPP;
73 return -1;
JP Abgralle0ed7402014-03-19 19:08:39 -070074#endif /* HAVE_EXT2_IOCTLS */
75#endif /* HAVE_CHFLAGS */
Theodore Ts'o3839e651997-04-26 13:21:57 +000076}