setflags.c revision d1154eb460efe588eaed3d439c1caaca149fa362
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 * 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#include "config.h" 20#if HAVE_ERRNO_H 21#include <errno.h> 22#endif 23#include <sys/types.h> 24#include <sys/stat.h> 25#if HAVE_EXT2_IOCTLS 26#include <sys/ioctl.h> 27#endif 28 29#include "e2p.h" 30 31/* 32 * Deal with lame glibc's that define this function without actually 33 * implementing it. Can you say "attractive nuisance", boys and girls? 34 * I knew you could! 35 */ 36#ifdef __linux__ 37#undef HAVE_CHFLAGS 38#endif 39 40int setflags (int fd, unsigned long flags) 41{ 42#if HAVE_CHFLAGS 43 unsigned long bsd_flags = 0; 44 45#ifdef UF_IMMUTABLE 46 if (flags & EXT2_IMMUTABLE_FL) 47 bsd_flags |= UF_IMMUTABLE; 48#endif 49#ifdef UF_APPEND 50 if (flags & EXT2_APPEND_FL) 51 bsd_flags |= UF_APPEND; 52#endif 53#ifdef UF_NODUMP 54 if (flags & EXT2_NODUMP_FL) 55 bsd_flags |= UF_NODUMP; 56#endif 57 58 return fchflags (fd, bsd_flags); 59#else /* ! HAVE_CHFLAGS */ 60#if HAVE_EXT2_IOCTLS 61 struct stat buf; 62 int f; 63 64 if (!fstat(fd, &buf) && 65 !S_ISREG(buf.st_mode) && !S_ISDIR(buf.st_mode)) { 66 errno = EOPNOTSUPP; 67 return -1; 68 } 69 f = (int) flags; 70 71 return ioctl(fd, EXT2_IOC_SETFLAGS, &f); 72#else 73 errno = EOPNOTSUPP; 74 return -1; 75#endif /* HAVE_EXT2_IOCTLS */ 76#endif /* HAVE_CHFLAGS */ 77} 78