blob: ca3ea16638efa410c6317eb5f3196d7348da695f [file] [log] [blame]
Theodore Ts'o3839e651997-04-26 13:21:57 +00001/*
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 *
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'o66d8c3f2000-12-02 06:11:44 +000019#define _LARGEFILE_SOURCE
20#define _LARGEFILE64_SOURCE
21
Theodore Ts'o50e1e101997-04-26 13:58:21 +000022#if HAVE_ERRNO_H
Theodore Ts'o3839e651997-04-26 13:21:57 +000023#include <errno.h>
Theodore Ts'o50e1e101997-04-26 13:58:21 +000024#endif
25#if HAVE_UNISTD_H
Theodore Ts'o3839e651997-04-26 13:21:57 +000026#include <unistd.h>
Theodore Ts'o50e1e101997-04-26 13:58:21 +000027#endif
Theodore Ts'o023d1112002-08-17 14:44:56 -040028#include <sys/types.h>
Theodore Ts'o50e1e101997-04-26 13:58:21 +000029#include <sys/stat.h>
Theodore Ts'o023d1112002-08-17 14:44:56 -040030#if HAVE_EXT2_IOCTLS
Theodore Ts'o50e1e101997-04-26 13:58:21 +000031#include <fcntl.h>
Theodore Ts'o3839e651997-04-26 13:21:57 +000032#include <sys/ioctl.h>
Theodore Ts'o50e1e101997-04-26 13:58:21 +000033#endif
Theodore Ts'o3839e651997-04-26 13:21:57 +000034
Theodore Ts'o3839e651997-04-26 13:21:57 +000035#include "e2p.h"
36
Theodore Ts'o66d8c3f2000-12-02 06:11:44 +000037#ifdef O_LARGEFILE
38#define OPEN_FLAGS (O_RDONLY|O_NONBLOCK|O_LARGEFILE)
39#else
40#define OPEN_FLAGS (O_RDONLY|O_NONBLOCK)
41#endif
42
Theodore Ts'o3839e651997-04-26 13:21:57 +000043int fgetflags (const char * name, unsigned long * flags)
44{
Theodore Ts'occe382b1998-03-09 13:07:09 +000045 struct stat buf;
Theodore Ts'o3c203cb2004-01-31 21:16:35 -050046#if HAVE_STAT_FLAGS && !(APPLE_DARWIN && HAVE_EXT2_IOCTLS)
Theodore Ts'o50e1e101997-04-26 13:58:21 +000047
Theodore Ts'occe382b1998-03-09 13:07:09 +000048 if (stat (name, &buf) == -1)
49 return -1;
Theodore Ts'o50e1e101997-04-26 13:58:21 +000050
Theodore Ts'occe382b1998-03-09 13:07:09 +000051 *flags = 0;
Theodore Ts'o50e1e101997-04-26 13:58:21 +000052#ifdef UF_IMMUTABLE
Theodore Ts'occe382b1998-03-09 13:07:09 +000053 if (buf.st_flags & UF_IMMUTABLE)
54 *flags |= EXT2_IMMUTABLE_FL;
Theodore Ts'o50e1e101997-04-26 13:58:21 +000055#endif
56#ifdef UF_APPEND
Theodore Ts'occe382b1998-03-09 13:07:09 +000057 if (buf.st_flags & UF_APPEND)
58 *flags |= EXT2_APPEND_FL;
Theodore Ts'o50e1e101997-04-26 13:58:21 +000059#endif
60#ifdef UF_NODUMP
Theodore Ts'occe382b1998-03-09 13:07:09 +000061 if (buf.st_flags & UF_NODUMP)
62 *flags |= EXT2_NODUMP_FL;
Theodore Ts'o50e1e101997-04-26 13:58:21 +000063#endif
64
Theodore Ts'occe382b1998-03-09 13:07:09 +000065 return 0;
JP Abgralle0ed7402014-03-19 19:08:39 -070066#else /* !HAVE_STAT_FLAGS || (APPLE_DARWIN && HAVE_EXT2_IOCTLS) */
Theodore Ts'o50e1e101997-04-26 13:58:21 +000067#if HAVE_EXT2_IOCTLS
Theodore Ts'oa8a813e2002-07-14 16:13:55 -040068 int fd, r, f, save_errno = 0;
Theodore Ts'o3839e651997-04-26 13:21:57 +000069
Theodore Ts'o1a4ce9d2007-10-22 01:36:13 -040070 if (!lstat(name, &buf) &&
Theodore Ts'o023d1112002-08-17 14:44:56 -040071 !S_ISREG(buf.st_mode) && !S_ISDIR(buf.st_mode)) {
Theodore Ts'o023d1112002-08-17 14:44:56 -040072 goto notsupp;
73 }
Theodore Ts'o3c203cb2004-01-31 21:16:35 -050074#if !APPLE_DARWIN
Theodore Ts'o66d8c3f2000-12-02 06:11:44 +000075 fd = open (name, OPEN_FLAGS);
Theodore Ts'o3839e651997-04-26 13:21:57 +000076 if (fd == -1)
77 return -1;
Theodore Ts'occe382b1998-03-09 13:07:09 +000078 r = ioctl (fd, EXT2_IOC_GETFLAGS, &f);
Theodore Ts'oa8a813e2002-07-14 16:13:55 -040079 if (r == -1)
80 save_errno = errno;
Theodore Ts'occe382b1998-03-09 13:07:09 +000081 *flags = f;
Theodore Ts'o3839e651997-04-26 13:21:57 +000082 close (fd);
Theodore Ts'oa8a813e2002-07-14 16:13:55 -040083 if (save_errno)
84 errno = save_errno;
Theodore Ts'o3839e651997-04-26 13:21:57 +000085 return r;
JP Abgralle0ed7402014-03-19 19:08:39 -070086#else /* APPLE_DARWIN */
Theodore Ts'o3c203cb2004-01-31 21:16:35 -050087 f = -1;
88 save_errno = syscall(SYS_fsctl, name, EXT2_IOC_GETFLAGS, &f, 0);
89 *flags = f;
90 return (save_errno);
JP Abgralle0ed7402014-03-19 19:08:39 -070091#endif /* !APPLE_DARWIN */
92notsupp:
Theodore Ts'o023d1112002-08-17 14:44:56 -040093#endif /* HAVE_EXT2_IOCTLS */
94#endif
Theodore Ts'o50e1e101997-04-26 13:58:21 +000095 errno = EOPNOTSUPP;
96 return -1;
Theodore Ts'o3839e651997-04-26 13:21:57 +000097}