Home | History | Annotate | Download | only in e2p
      1 /*
      2  * fsetflags.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 #define _LARGEFILE_SOURCE
     20 #define _LARGEFILE64_SOURCE
     21 
     22 #if HAVE_ERRNO_H
     23 #include <errno.h>
     24 #endif
     25 #if HAVE_UNISTD_H
     26 #include <unistd.h>
     27 #endif
     28 #include <sys/types.h>
     29 #include <sys/stat.h>
     30 #if HAVE_EXT2_IOCTLS
     31 #include <fcntl.h>
     32 #include <sys/ioctl.h>
     33 #endif
     34 
     35 #include "e2p.h"
     36 
     37 /*
     38  * Deal with lame glibc's that define this function without actually
     39  * implementing it.  Can you say "attractive nuisance", boys and girls?
     40  * I knew you could!
     41  */
     42 #ifdef __linux__
     43 #undef HAVE_CHFLAGS
     44 #endif
     45 
     46 #ifdef O_LARGEFILE
     47 #define OPEN_FLAGS (O_RDONLY|O_NONBLOCK|O_LARGEFILE)
     48 #else
     49 #define OPEN_FLAGS (O_RDONLY|O_NONBLOCK)
     50 #endif
     51 
     52 int fsetflags (const char * name, unsigned long flags)
     53 {
     54 #if HAVE_CHFLAGS && !(APPLE_DARWIN && HAVE_EXT2_IOCTLS)
     55 	unsigned long bsd_flags = 0;
     56 
     57 #ifdef UF_IMMUTABLE
     58 	if (flags & EXT2_IMMUTABLE_FL)
     59 		bsd_flags |= UF_IMMUTABLE;
     60 #endif
     61 #ifdef UF_APPEND
     62 	if (flags & EXT2_APPEND_FL)
     63 		bsd_flags |= UF_APPEND;
     64 #endif
     65 #ifdef UF_NODUMP
     66 	if (flags & EXT2_NODUMP_FL)
     67 		bsd_flags |= UF_NODUMP;
     68 #endif
     69 
     70 	return chflags (name, bsd_flags);
     71 #else /* !HAVE_CHFLAGS || (APPLE_DARWIN && HAVE_EXT2_IOCTLS) */
     72 #if HAVE_EXT2_IOCTLS
     73 	int fd, r, f, save_errno = 0;
     74 	struct stat buf;
     75 
     76 	if (!lstat(name, &buf) &&
     77 	    !S_ISREG(buf.st_mode) && !S_ISDIR(buf.st_mode)) {
     78 		goto notsupp;
     79 	}
     80 #if !APPLE_DARWIN
     81 	fd = open (name, OPEN_FLAGS);
     82 	if (fd == -1)
     83 		return -1;
     84 	f = (int) flags;
     85 	r = ioctl (fd, EXT2_IOC_SETFLAGS, &f);
     86 	if (r == -1)
     87 		save_errno = errno;
     88 	close (fd);
     89 	if (save_errno)
     90 		errno = save_errno;
     91 #else /* APPLE_DARWIN */
     92 	f = (int) flags;
     93 	return syscall(SYS_fsctl, name, EXT2_IOC_SETFLAGS, &f, 0);
     94 #endif /* !APPLE_DARWIN */
     95 	return r;
     96 
     97 notsupp:
     98 #endif /* HAVE_EXT2_IOCTLS */
     99 #endif
    100 	errno = EOPNOTSUPP;
    101 	return -1;
    102 }
    103