File: file_attr.c

package info (click to toggle)
xfsprogs 6.17.0-2
  • links: PTS
  • area: main
  • in suites: forky, sid
  • size: 11,324 kB
  • sloc: ansic: 167,334; sh: 4,604; makefile: 1,336; python: 835; cpp: 5
file content (121 lines) | stat: -rw-r--r-- 2,240 bytes parent folder | download
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
// SPDX-License-Identifier: GPL-2.0
/*
 * Copyright (c) 2025 Red Hat, Inc.
 * All Rights Reserved.
 */

#include "file_attr.h"
#include <stdio.h>
#include <errno.h>
#include <string.h>
#include <sys/syscall.h>
#include <asm/types.h>
#include <fcntl.h>

static void
file_attr_to_fsxattr(
	const struct file_attr	*fa,
	struct fsxattr		*fsxa)
{
	memset(fsxa, 0, sizeof(struct fsxattr));

	fsxa->fsx_xflags = fa->fa_xflags;
	fsxa->fsx_extsize = fa->fa_extsize;
	fsxa->fsx_nextents = fa->fa_nextents;
	fsxa->fsx_projid = fa->fa_projid;
	fsxa->fsx_cowextsize = fa->fa_cowextsize;
}

static void
fsxattr_to_file_attr(
	const struct fsxattr	*fsxa,
	struct file_attr	*fa)
{
	memset(fa, 0, sizeof(struct file_attr));

	fa->fa_xflags = fsxa->fsx_xflags;
	fa->fa_extsize = fsxa->fsx_extsize;
	fa->fa_nextents = fsxa->fsx_nextents;
	fa->fa_projid = fsxa->fsx_projid;
	fa->fa_cowextsize = fsxa->fsx_cowextsize;
}

int
xfrog_file_getattr(
	const int		dfd,
	const char		*path,
	const struct stat	*stat,
	struct file_attr	*fa,
	const unsigned int	at_flags)
{
	int			error;
	int			fd;
	struct fsxattr		fsxa;

#ifdef HAVE_FILE_GETATTR
	error = syscall(__NR_file_getattr, dfd, path, fa,
			sizeof(struct file_attr), at_flags);
	if (error && errno != ENOSYS)
		return error;

	if (!error)
		return error;
#endif

	if (SPECIAL_FILE(stat->st_mode)) {
		errno = EOPNOTSUPP;
		return -1;
	}

	fd = open(path, O_RDONLY|O_NOCTTY);
	if (fd == -1)
		return fd;

	error = ioctl(fd, FS_IOC_FSGETXATTR, &fsxa);
	close(fd);
	if (error)
		return error;

	fsxattr_to_file_attr(&fsxa, fa);

	return error;
}

int
xfrog_file_setattr(
	const int		dfd,
	const char		*path,
	const mode_t		mode,
	struct file_attr	*fa,
	const unsigned int	at_flags)
{
	int			error;
	int			fd;
	struct fsxattr		fsxa;

#ifdef HAVE_FILE_GETATTR /* file_get/setattr goes together */
	error = syscall(__NR_file_setattr, dfd, path, fa,
			sizeof(struct file_attr), at_flags);
	if (error && errno != ENOSYS)
		return error;

	if (!error)
		return error;
#endif

	if (SPECIAL_FILE(mode)) {
		errno = EOPNOTSUPP;
		return -1;
	}

	fd = open(path, O_RDONLY|O_NOCTTY);
	if (fd == -1)
		return fd;

	file_attr_to_fsxattr(fa, &fsxa);

	error = ioctl(fd, FS_IOC_FSSETXATTR, &fsxa);
	close(fd);

	return error;
}