blob: 48cea9df4aac5cccee5dca9a51568ad4a0016e53 [file] [log] [blame]
"Robert P. J. Day"63fc1a92006-07-02 19:47:05 +00001/* vi: set sw=4 ts=4: */
Mike Frysingerd89e6292005-04-24 05:07:59 +00002/*
3 * fgetversion.c - Get a file version on an ext2 file system
Mike Frysinger548ffd42005-05-07 07:14:41 +00004 * fsetversion.c - Set a file version on an ext2 file system
5 *
Mike Frysingerd89e6292005-04-24 05:07:59 +00006 *
7 * Copyright (C) 1993, 1994 Remy Card <card@masi.ibp.fr>
8 * Laboratoire MASI, Institut Blaise Pascal
9 * Universite Pierre et Marie Curie (Paris VI)
10 *
11 * This file can be redistributed under the terms of the GNU Library General
12 * Public License
13 */
14
15/*
16 * History:
17 * 93/10/30 - Creation
18 */
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 <fcntl.h>
27#include <sys/ioctl.h>
28
29#include "e2p.h"
30
31#ifdef O_LARGEFILE
32#define OPEN_FLAGS (O_RDONLY|O_NONBLOCK|O_LARGEFILE)
33#else
34#define OPEN_FLAGS (O_RDONLY|O_NONBLOCK)
35#endif
36
Tim Rikerc1ef7bd2006-01-25 00:08:53 +000037/*
Mike Frysinger548ffd42005-05-07 07:14:41 +000038 To do fsetversion: unsigned long *ptr_version must be set to NULL.
Tim Rikerc1ef7bd2006-01-25 00:08:53 +000039 and unsigned long version must be set to a value
Mike Frysinger548ffd42005-05-07 07:14:41 +000040 To do fgetversion: unsigned long *ptr_version must NOT be set to NULL
Tim Rikerc1ef7bd2006-01-25 00:08:53 +000041 and unsigned long version is ignored.
Mike Frysinger548ffd42005-05-07 07:14:41 +000042 TITO.
43*/
44
45int fgetsetversion (const char * name, unsigned long * get_version, unsigned long set_version)
Mike Frysingerd89e6292005-04-24 05:07:59 +000046{
47#if HAVE_EXT2_IOCTLS
Mike Frysingerd89e6292005-04-24 05:07:59 +000048 int fd, r, ver, save_errno = 0;
49
50 fd = open (name, OPEN_FLAGS);
51 if (fd == -1)
52 return -1;
Mike Frysinger548ffd42005-05-07 07:14:41 +000053 if (!get_version) {
54 ver = (int) set_version;
55 r = ioctl (fd, EXT2_IOC_SETVERSION, &ver);
56 } else {
57 r = ioctl (fd, EXT2_IOC_GETVERSION, &ver);
58 *get_version = ver;
59 }
Mike Frysingerd89e6292005-04-24 05:07:59 +000060 if (r == -1)
61 save_errno = errno;
Mike Frysingerd89e6292005-04-24 05:07:59 +000062 close (fd);
63 if (save_errno)
64 errno = save_errno;
65 return r;
Mike Frysingerd89e6292005-04-24 05:07:59 +000066#else /* ! HAVE_EXT2_IOCTLS */
Mike Frysingerd89e6292005-04-24 05:07:59 +000067 errno = EOPNOTSUPP;
68 return -1;
69#endif /* ! HAVE_EXT2_IOCTLS */
70}