Home | History | Annotate | Download | only in e2p
      1 /*
      2  * setflags.c		- Set a file flags on an ext2 file system
      3  *
      4  * Copyright (C) 1993, 1994  Remy Card <card (at) 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 #if HAVE_ERRNO_H
     20 #include <errno.h>
     21 #endif
     22 #include <sys/types.h>
     23 #include <sys/stat.h>
     24 #if HAVE_EXT2_IOCTLS
     25 #include <sys/ioctl.h>
     26 #endif
     27 
     28 #include "e2p.h"
     29 
     30 /*
     31  * Deal with lame glibc's that define this function without actually
     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 
     39 int setflags (int fd, unsigned long flags)
     40 {
     41 #if HAVE_CHFLAGS
     42 	unsigned long bsd_flags = 0;
     43 
     44 #ifdef UF_IMMUTABLE
     45 	if (flags & EXT2_IMMUTABLE_FL)
     46 		bsd_flags |= UF_IMMUTABLE;
     47 #endif
     48 #ifdef UF_APPEND
     49 	if (flags & EXT2_APPEND_FL)
     50 		bsd_flags |= UF_APPEND;
     51 #endif
     52 #ifdef UF_NODUMP
     53 	if (flags & EXT2_NODUMP_FL)
     54 		bsd_flags |= UF_NODUMP;
     55 #endif
     56 
     57 	return fchflags (fd, bsd_flags);
     58 #else /* ! HAVE_CHFLAGS */
     59 #if HAVE_EXT2_IOCTLS
     60 	struct stat buf;
     61 	int	f;
     62 
     63 	if (!fstat(fd, &buf) &&
     64 	    !S_ISREG(buf.st_mode) && !S_ISDIR(buf.st_mode)) {
     65 		errno = EOPNOTSUPP;
     66 		return -1;
     67 	}
     68 	f = (int) flags;
     69 
     70 	return ioctl(fd, EXT2_IOC_SETFLAGS, &f);
     71 #else
     72 	errno = EOPNOTSUPP;
     73 	return -1;
     74 #endif /* HAVE_EXT2_IOCTLS */
     75 #endif /* HAVE_CHFLAGS */
     76 }
     77