AquilaOS documentation
How it works, and its code
Loading...
Searching...
No Matches
attr.c
Go to the documentation of this file.
1
7
8#include <core/system.h>
9#include <core/time.h>
10#include <fs/vfs.h>
11
14static int store(struct vnode *vnode, struct vnode *saved)
15{
16 int ret = vfs_setattr(vnode);
17
18 if (ret) {
19 vnode->mode = saved->mode;
20 vnode->uid = saved->uid;
21 vnode->gid = saved->gid;
22 vnode->atime = saved->atime;
23 vnode->mtime = saved->mtime;
24 vnode->ctime = saved->ctime;
25 }
26
27 return ret;
28}
29
38int vfs_chmod(struct vnode *vnode, mode_t mode, struct uio *uio)
39{
40 struct vnode saved = *vnode;
41
42 /* only the owner (and root) may change the mode */
43 if (uio->uid && uio->uid != vnode->uid)
44 return -EPERM;
45
46 mode &= 07777;
47
48 /* setgid is dropped unless the file is in the user's group */
49 if (uio->uid && vnode->gid != uio->gid)
50 mode &= ~S_ISGID;
51
52 vnode->mode = (vnode->mode & S_IFMT) | mode;
54
55 return store(vnode, &saved);
56}
57
68int vfs_chown(struct vnode *vnode, uid_t uid, gid_t gid, struct uio *uio)
69{
70 struct vnode saved = *vnode;
71
72 /* (uid_t) -1 and (gid_t) -1 leave the owner or group unchanged */
73 if (uid == (uid_t) -1)
74 uid = vnode->uid;
75
76 if (gid == (gid_t) -1)
77 gid = vnode->gid;
78
79 /* others may only change the group of their files to their own group */
80 if (uio->uid && (uio->uid != vnode->uid || uid != vnode->uid ||
81 (gid != vnode->gid && gid != uio->gid)))
82 return -EPERM;
83
84 /* set-user-id and set-group-id don't carry over to the new owner */
85 if (uio->uid && !S_ISDIR(vnode->mode))
86 vnode->mode &= ~(S_ISUID | S_ISGID);
87
88 vnode->uid = uid;
89 vnode->gid = gid;
91
92 return store(vnode, &saved);
93}
94
97static void set_time(_time_t *time, const struct aquila_timespec *ts, struct timespec *now)
98{
99 int32_t nsec = ts ? (int32_t) ts->tv_nsec : UTIME_NOW;
100
101 if (nsec == UTIME_OMIT)
102 return;
103
104 if (nsec == UTIME_NOW) {
105 *time = *now;
106 } else {
107 time->tv_sec = ts->tv_sec;
108 time->tv_nsec = ts->tv_nsec;
109 }
110}
111
124int vfs_utimens(struct vnode *vnode, const struct aquila_timespec times[2], struct uio *uio)
125{
126 struct vnode saved = *vnode;
127 int to_now = 1;
128
129 if (times) {
130 for (int i = 0; i < 2; ++i) {
131 int32_t nsec = times[i].tv_nsec;
132
133 if (nsec != UTIME_NOW && nsec != UTIME_OMIT) {
134 if (nsec < 0 || nsec >= 1000000000)
135 return -EINVAL;
136
137 to_now = 0;
138 }
139 }
140 }
141
142 /* setting the current time needs write permission, other times
143 * ownership */
144 if (uio->uid && uio->uid != vnode->uid) {
145 if (!to_now)
146 return -EPERM;
147
148 if (vfs_access(vnode, uio, VFS_W))
149 return -EACCES;
150 }
151
152 struct timespec now;
153 gettime(&now);
154
155 set_time(&vnode->atime, times ? &times[0] : NULL, &now);
156 set_time(&vnode->mtime, times ? &times[1] : NULL, &now);
157 vnode->ctime = now;
158
159 return store(vnode, &saved);
160}
int vfs_chown(struct vnode *vnode, uid_t uid, gid_t gid, struct uio *uio)
Changes a vnode's owner and group.
Definition attr.c:68
int vfs_utimens(struct vnode *vnode, const struct aquila_timespec times[2], struct uio *uio)
Changes a vnode's access and modification times, for utimensat().
Definition attr.c:124
static int store(struct vnode *vnode, struct vnode *saved)
Has the filesystem store a vnode's new attributes, and restores the old ones (saved) if it can't.
Definition attr.c:14
int vfs_chmod(struct vnode *vnode, mode_t mode, struct uio *uio)
Changes a vnode's permission bits.
Definition attr.c:38
static void set_time(_time_t *time, const struct aquila_timespec *ts, struct timespec *now)
Sets one time as utimensat() asks: to now for UTIME_NOW (or no time given), unchanged for UTIME_OMIT,...
Definition attr.c:97
#define NULL
Definition stddef.h:27
signed int int32_t
Definition stddef.h:18
uint32_t tv_nsec
Definition types.h:32
time_t tv_sec
Definition types.h:31
Who does a filesystem operation: where its paths start, and its credentials and umask.
Definition vfs.h:60
gid_t gid
Definition vfs.h:64
uid_t uid
Definition vfs.h:63
A file in memory (a vnode): what its inode says, and the kernel's own state.
Definition vfs.h:113
_time_t atime
Definition vfs.h:123
_time_t ctime
Definition vfs.h:125
_time_t mtime
Definition vfs.h:124
gid_t gid
Definition vfs.h:120
mode_t mode
Definition vfs.h:118
uid_t uid
Definition vfs.h:119
int gettime(struct timespec *ts)
Reads the wall clock (CLOCK_REALTIME).
Definition time.c:29
struct timespec _time_t
Definition types.h:24
uint32_t gid_t
Definition types.h:21
uint32_t uid_t
Definition types.h:20
uint32_t mode_t
Definition types.h:15
int vfs_access(struct vnode *vnode, struct uio *uio, int mode)
Checks whether a user may access a vnode in a way: the owner's permissions apply to its owner,...
Definition vfs.c:114
#define VFS_W
Definition vfs.h:296
int vfs_setattr(struct vnode *vnode)
Has the filesystem store a vnode's attributes (mode, owner, times) after they were changed in memory.
Definition vops.c:156