blob: 8227ab3be149299e6ae54a621acf36b7bfdd8cdd [file] [log] [blame]
Mike Frysingerd89e6292005-04-24 05:07:59 +00001/*
2 * fgetversion.c - Get a file version on an ext2 file system
Mike Frysinger548ffd42005-05-07 07:14:41 +00003 * fsetversion.c - Set a file version on an ext2 file system
4 *
Mike Frysingerd89e6292005-04-24 05:07:59 +00005 *
6 * Copyright (C) 1993, 1994 Remy Card <card@masi.ibp.fr>
7 * Laboratoire MASI, Institut Blaise Pascal
8 * Universite Pierre et Marie Curie (Paris VI)
9 *
10 * This file can be redistributed under the terms of the GNU Library General
11 * Public License
12 */
13
14/*
15 * History:
16 * 93/10/30 - Creation
17 */
18
19#if HAVE_ERRNO_H
20#include <errno.h>
21#endif
22#if HAVE_UNISTD_H
23#include <unistd.h>
24#endif
25#include <fcntl.h>
26#include <sys/ioctl.h>
27
28#include "e2p.h"
29
30#ifdef O_LARGEFILE
31#define OPEN_FLAGS (O_RDONLY|O_NONBLOCK|O_LARGEFILE)
32#else
33#define OPEN_FLAGS (O_RDONLY|O_NONBLOCK)
34#endif
35
Tim Rikerc1ef7bd2006-01-25 00:08:53 +000036/*
Mike Frysinger548ffd42005-05-07 07:14:41 +000037 To do fsetversion: unsigned long *ptr_version must be set to NULL.
Tim Rikerc1ef7bd2006-01-25 00:08:53 +000038 and unsigned long version must be set to a value
Mike Frysinger548ffd42005-05-07 07:14:41 +000039 To do fgetversion: unsigned long *ptr_version must NOT be set to NULL
Tim Rikerc1ef7bd2006-01-25 00:08:53 +000040 and unsigned long version is ignored.
Mike Frysinger548ffd42005-05-07 07:14:41 +000041 TITO.
42*/
43
44int fgetsetversion (const char * name, unsigned long * get_version, unsigned long set_version)
Mike Frysingerd89e6292005-04-24 05:07:59 +000045{
46#if HAVE_EXT2_IOCTLS
Mike Frysingerd89e6292005-04-24 05:07:59 +000047 int fd, r, ver, save_errno = 0;
48
49 fd = open (name, OPEN_FLAGS);
50 if (fd == -1)
51 return -1;
Mike Frysinger548ffd42005-05-07 07:14:41 +000052 if (!get_version) {
53 ver = (int) set_version;
54 r = ioctl (fd, EXT2_IOC_SETVERSION, &ver);
55 } else {
56 r = ioctl (fd, EXT2_IOC_GETVERSION, &ver);
57 *get_version = ver;
58 }
Mike Frysingerd89e6292005-04-24 05:07:59 +000059 if (r == -1)
60 save_errno = errno;
Mike Frysingerd89e6292005-04-24 05:07:59 +000061 close (fd);
62 if (save_errno)
63 errno = save_errno;
64 return r;
Mike Frysingerd89e6292005-04-24 05:07:59 +000065#else /* ! HAVE_EXT2_IOCTLS */
Mike Frysingerd89e6292005-04-24 05:07:59 +000066 errno = EOPNOTSUPP;
67 return -1;
68#endif /* ! HAVE_EXT2_IOCTLS */
69}