blob: e8716848785ff30197d731202f9d5c2f8c5983b0 [file] [log] [blame]
Theodore Ts'o3839e651997-04-26 13:21:57 +00001/*
2 * getflags.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'o50e1e101997-04-26 13:58:21 +000019#if HAVE_ERRNO_H
Theodore Ts'o3839e651997-04-26 13:21:57 +000020#include <errno.h>
Theodore Ts'o50e1e101997-04-26 13:58:21 +000021#endif
Theodore Ts'o023d1112002-08-17 14:44:56 -040022#include <sys/types.h>
Theodore Ts'o50e1e101997-04-26 13:58:21 +000023#include <sys/stat.h>
Theodore Ts'o023d1112002-08-17 14:44:56 -040024#if HAVE_EXT2_IOCTLS
Theodore Ts'o3839e651997-04-26 13:21:57 +000025#include <sys/ioctl.h>
Theodore Ts'o50e1e101997-04-26 13:58:21 +000026#endif
Theodore Ts'o3839e651997-04-26 13:21:57 +000027
Theodore Ts'o3839e651997-04-26 13:21:57 +000028#include "e2p.h"
29
30int getflags (int fd, unsigned long * flags)
31{
Theodore Ts'occe382b1998-03-09 13:07:09 +000032 struct stat buf;
Theodore Ts'o023d1112002-08-17 14:44:56 -040033#if HAVE_STAT_FLAGS
Theodore Ts'o50e1e101997-04-26 13:58:21 +000034
Theodore Ts'occe382b1998-03-09 13:07:09 +000035 if (fstat (fd, &buf) == -1)
36 return -1;
Theodore Ts'o50e1e101997-04-26 13:58:21 +000037
Theodore Ts'occe382b1998-03-09 13:07:09 +000038 *flags = 0;
Theodore Ts'o50e1e101997-04-26 13:58:21 +000039#ifdef UF_IMMUTABLE
Theodore Ts'occe382b1998-03-09 13:07:09 +000040 if (buf.st_flags & UF_IMMUTABLE)
41 *flags |= EXT2_IMMUTABLE_FL;
Theodore Ts'o50e1e101997-04-26 13:58:21 +000042#endif
43#ifdef UF_APPEND
Theodore Ts'occe382b1998-03-09 13:07:09 +000044 if (buf.st_flags & UF_APPEND)
45 *flags |= EXT2_APPEND_FL;
Theodore Ts'o50e1e101997-04-26 13:58:21 +000046#endif
47#ifdef UF_NODUMP
Theodore Ts'occe382b1998-03-09 13:07:09 +000048 if (buf.st_flags & UF_NODUMP)
49 *flags |= EXT2_NODUMP_FL;
Theodore Ts'o50e1e101997-04-26 13:58:21 +000050#endif
51
Theodore Ts'occe382b1998-03-09 13:07:09 +000052 return 0;
Theodore Ts'o50e1e101997-04-26 13:58:21 +000053#else
54#if HAVE_EXT2_IOCTLS
Theodore Ts'occe382b1998-03-09 13:07:09 +000055 int r, f;
Theodore Ts'oefc6f622008-08-27 23:07:54 -040056
Theodore Ts'o023d1112002-08-17 14:44:56 -040057 if (!fstat(fd, &buf) &&
58 !S_ISREG(buf.st_mode) && !S_ISDIR(buf.st_mode))
59 goto notsupp;
JP Abgralle0ed7402014-03-19 19:08:39 -070060 r = ioctl(fd, EXT2_IOC_GETFLAGS, &f);
Theodore Ts'occe382b1998-03-09 13:07:09 +000061 *flags = f;
JP Abgralle0ed7402014-03-19 19:08:39 -070062
Theodore Ts'occe382b1998-03-09 13:07:09 +000063 return r;
JP Abgralle0ed7402014-03-19 19:08:39 -070064notsupp:
Theodore Ts'o023d1112002-08-17 14:44:56 -040065#endif /* HAVE_EXT2_IOCTLS */
66#endif
Theodore Ts'o50e1e101997-04-26 13:58:21 +000067 errno = EOPNOTSUPP;
68 return -1;
Theodore Ts'o3839e651997-04-26 13:21:57 +000069}