summaryrefslogtreecommitdiffstats
path: root/src/statx.c
blob: d4c8d0b7d07623828c3a1645e8406983817be8ac (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
/*
 * statx.c - Map modern statx(2) system call to older stat(2), lstat(2),
 *	     and fstat(2) replacements named {,l,f}statn()
 *
 * Copyright (C) 2018 Werner Fink
 *
 * This program is free software; you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation; either version 2 of the License, or
 * (at your option) any later version.
 *
 *  This program is distributed in the hope that it will be useful,
 *  but WITHOUT ANY WARRANTY; without even the implied warranty of
 *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 *  GNU General Public License for more details.
 *
 *  You should have received a copy of the GNU General Public License
 *  along with this program; if not, write to the Free Software
 *  Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
 */
#ifndef _GNU_SOURCE
#define _GNU_SOURCE
#endif

#ifdef HAVE_CONFIG_H
#include <config.h>
#endif

#include <sys/sysmacros.h>
#include <sys/syscall.h>
#include <sys/types.h>
#include <unistd.h>
#include <fcntl.h>		/* Definition of AT_* constants */

int stat_flags = AT_NO_AUTOMOUNT|AT_STATX_DONT_SYNC;
#ifdef WITH_STATX

#include <errno.h>
#ifndef HAVE_STATX
# define _ASM_GENERIC_FCNTL_H	/* Avoid collisions between asm/fcntl.h and bits/fcntl.h ! */
# include <linux/fcntl.h>	/* Definition of AT_* and AT_STATX_* constants ! */
#endif

#include <sys/stat.h>

#ifndef HAVE_STATX
# ifndef STATX_TYPE
#  include <linux/stat.h>	/* Provides 'struct statx' and STATX_* ! */
# endif
#endif


int statn(const char *pathname, unsigned int mask, struct stat *st)
{
    int flags = stat_flags;
    int dirfd = pathname && *pathname == '/' ? 0 : AT_FDCWD;
    int ret;
    struct statx stx;

#ifndef HAVE_STATX
    ret = syscall(SYS_statx, dirfd, pathname, flags, mask, &stx);
#else
    ret = statx(dirfd, pathname, flags, mask, &stx);
#endif
    if (ret >= 0) {
	st->st_dev  = makedev(stx.stx_dev_major,  stx.stx_dev_minor);
	st->st_rdev = makedev(stx.stx_rdev_major, stx.stx_rdev_minor);

	st->st_ino  = stx.stx_ino;
	st->st_mode = stx.stx_mode;
	st->st_nlink = stx.stx_nlink;
	st->st_uid = stx.stx_uid;
	st->st_gid = stx.stx_gid;
	st->st_size = stx.stx_size;
	st->st_blksize = stx.stx_blksize;
	st->st_blocks = stx.stx_blocks;

	st->st_atim.tv_sec = stx.stx_atime.tv_sec;
	st->st_atim.tv_nsec = stx.stx_atime.tv_nsec;
	st->st_mtim.tv_sec = stx.stx_mtime.tv_sec;
	st->st_mtim.tv_nsec = stx.stx_mtime.tv_nsec;
	st->st_ctim.tv_sec = stx.stx_ctime.tv_sec;
	st->st_ctim.tv_nsec = stx.stx_ctime.tv_nsec;
    } else if (errno==ENOSYS || errno==EINVAL)
        return stat(pathname, st);
    return ret;
}

int fstatn(int fd, unsigned int mask, struct stat *st)
{
    int flags = AT_EMPTY_PATH|stat_flags;
    int ret;
    struct statx stx;

#ifndef HAVE_STATX
    ret = syscall(SYS_statx, fd, "", flags, mask, &stx);
#else
    ret = statx(fd, "", flags, mask, &stx);
#endif
    if (ret >= 0) {
	st->st_dev  = makedev(stx.stx_dev_major,  stx.stx_dev_minor);
	st->st_rdev = makedev(stx.stx_rdev_major, stx.stx_rdev_minor);

	st->st_ino  = stx.stx_ino;
	st->st_mode = stx.stx_mode;
	st->st_nlink = stx.stx_nlink;
	st->st_uid = stx.stx_uid;
	st->st_gid = stx.stx_gid;
	st->st_size = stx.stx_size;
	st->st_blksize = stx.stx_blksize;
	st->st_blocks = stx.stx_blocks;

	st->st_atim.tv_sec = stx.stx_atime.tv_sec;
	st->st_atim.tv_nsec = stx.stx_atime.tv_nsec;
	st->st_mtim.tv_sec = stx.stx_mtime.tv_sec;
	st->st_mtim.tv_nsec = stx.stx_mtime.tv_nsec;
	st->st_ctim.tv_sec = stx.stx_ctime.tv_sec;
	st->st_ctim.tv_nsec = stx.stx_ctime.tv_nsec;
    } else if (errno==ENOSYS || errno==EINVAL)
        return fstat(fd, st);
    return ret;
}

int lstatn(const char *pathname, unsigned int mask, struct stat *st)
{
    int flags = AT_SYMLINK_NOFOLLOW|stat_flags;
    int dirfd = pathname && *pathname == '/' ? 0 : AT_FDCWD;
    int ret;
    struct statx stx;

#ifndef HAVE_STATX
    ret = syscall(SYS_statx, dirfd, pathname, flags, mask, &stx);
#else
    ret = statx(dirfd, pathname, flags, mask, &stx);
#endif
    if (ret >= 0) {
	st->st_dev  = makedev(stx.stx_dev_major,  stx.stx_dev_minor);
	st->st_rdev = makedev(stx.stx_rdev_major, stx.stx_rdev_minor);

	st->st_ino  = stx.stx_ino;
	st->st_mode = stx.stx_mode;
	st->st_nlink = stx.stx_nlink;
	st->st_uid = stx.stx_uid;
	st->st_gid = stx.stx_gid;
	st->st_size = stx.stx_size;
	st->st_blksize = stx.stx_blksize;
	st->st_blocks = stx.stx_blocks;

	st->st_atim.tv_sec = stx.stx_atime.tv_sec;
	st->st_atim.tv_nsec = stx.stx_atime.tv_nsec;
	st->st_mtim.tv_sec = stx.stx_mtime.tv_sec;
	st->st_mtim.tv_nsec = stx.stx_mtime.tv_nsec;
	st->st_ctim.tv_sec = stx.stx_ctime.tv_sec;
	st->st_ctim.tv_nsec = stx.stx_ctime.tv_nsec;
    } else if (errno==ENOSYS || errno==EINVAL)
        return lstat(pathname, st);
    return ret;
}
#endif /* WITH_STATX */