• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * fgetflags.c		- Get 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  * This file can be redistributed under the terms of the GNU Library General
9  * Public License
10  */
11 
12 /*
13  * History:
14  * 93/10/30	- Creation
15  */
16 
17 #define _LARGEFILE_SOURCE
18 #define _LARGEFILE64_SOURCE
19 
20 #if HAVE_ERRNO_H
21 #include <errno.h>
22 #endif
23 #if HAVE_UNISTD_H
24 #include <unistd.h>
25 #endif
26 #include <sys/types.h>
27 #include <sys/stat.h>
28 #if HAVE_EXT2_IOCTLS
29 #include <fcntl.h>
30 #include <sys/ioctl.h>
31 #endif
32 
33 #include "e2p.h"
34 
35 #ifdef O_LARGEFILE
36 #define OPEN_FLAGS (O_RDONLY|O_NONBLOCK|O_LARGEFILE)
37 #else
38 #define OPEN_FLAGS (O_RDONLY|O_NONBLOCK)
39 #endif
40 
fgetflags(const char * name,unsigned long * flags)41 int fgetflags (const char * name, unsigned long * flags)
42 {
43 	struct stat buf;
44 #if HAVE_STAT_FLAGS && !(APPLE_DARWIN && HAVE_EXT2_IOCTLS)
45 
46 	if (stat (name, &buf) == -1)
47 		return -1;
48 
49 	*flags = 0;
50 #ifdef UF_IMMUTABLE
51 	if (buf.st_flags & UF_IMMUTABLE)
52 		*flags |= EXT2_IMMUTABLE_FL;
53 #endif
54 #ifdef UF_APPEND
55 	if (buf.st_flags & UF_APPEND)
56 		*flags |= EXT2_APPEND_FL;
57 #endif
58 #ifdef UF_NODUMP
59 	if (buf.st_flags & UF_NODUMP)
60 		*flags |= EXT2_NODUMP_FL;
61 #endif
62 
63 	return 0;
64 #else
65 #if HAVE_EXT2_IOCTLS
66 	int fd, r, f, save_errno = 0;
67 
68 	if (!stat(name, &buf) &&
69 	    !S_ISREG(buf.st_mode) && !S_ISDIR(buf.st_mode)) {
70 		goto notsupp;
71 	}
72 #if !APPLE_DARWIN
73 	fd = open (name, OPEN_FLAGS);
74 	if (fd == -1)
75 		return -1;
76 	r = ioctl (fd, EXT2_IOC_GETFLAGS, &f);
77 	if (r == -1)
78 		save_errno = errno;
79 	*flags = f;
80 	close (fd);
81 	if (save_errno)
82 		errno = save_errno;
83 	return r;
84 #else
85    f = -1;
86    save_errno = syscall(SYS_fsctl, name, EXT2_IOC_GETFLAGS, &f, 0);
87    *flags = f;
88    return (save_errno);
89 #endif
90 #endif /* HAVE_EXT2_IOCTLS */
91 #endif
92 notsupp:
93 	errno = EOPNOTSUPP;
94 	return -1;
95 }
96