This repository was archived by the owner on May 1, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathio.c
More file actions
129 lines (100 loc) · 2.9 KB
/
io.c
File metadata and controls
129 lines (100 loc) · 2.9 KB
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
#include "unionfs.h"
#include "private.h"
#include <errno.h>
#include <stdbool.h>
#include <stdlib.h>
#include <fcntl.h>
#include <unistd.h>
int ufs_mkdir(struct unionfs* fs, const char* path, mode_t mode)
{
char* real_path = new_real_path(fs, path);
int res = mkdir(real_path, mode);
free(real_path);
return res == 0 ? 0 : -errno;
}
int ufs_remove(struct unionfs* fs, const char* path)
{
for (struct ufs_disk* disk = fs->all_disks; disk != fs->all_disks + fs->disks_count; ++disk) {
char* real_path = get_real_path(disk, path);
errno = 0;
int res = unlink(real_path);
if (res != 0 && errno == EROFS && access(real_path, F_OK) != 0)
errno = ENOENT;
free(real_path);
if (res == 0 || errno != ENOENT)
break;
}
return -errno;
}
int ufs_rmdir(struct unionfs* fs, const char* path)
{
bool dir_removed = false;
for (struct ufs_disk* disk = fs->all_disks; disk != fs->all_disks + fs->disks_count; ++disk) {
char* real_path = get_real_path(disk, path);
if (rmdir(real_path) == 0)
dir_removed = true;
if (!dir_removed && errno == EROFS && access(real_path, F_OK) != 0)
errno = ENOENT;
free(real_path);
if (!dir_removed && errno != ENOENT)
break;
}
return dir_removed ? 0 : -errno;
}
int ufs_truncate(struct unionfs* fs, const char* path, off_t offset)
{
for (struct ufs_disk* disk = fs->all_disks; disk != fs->all_disks + fs->disks_count; ++disk) {
char* real_path = get_real_path(disk, path);
errno = 0;
int res = truncate(real_path, offset);
free(real_path);
if (res == 0 || errno != ENOENT)
break;
}
return -errno;
}
int ufs_ftruncate(struct unionfs* fs, ufs_fd_t fd, off_t offset)
{
(void) fs;
return ftruncate(fd, offset) == 0 ? 0 : -errno;
}
int ufs_open(struct unionfs* fs, const char* path, int flags, ufs_fd_t* fd)
{
for (struct ufs_disk* disk = fs->all_disks; disk != fs->all_disks + fs->disks_count; ++disk) {
char* real_path = get_real_path(disk, path);
errno = 0;
*fd = open(real_path, flags);
free(real_path);
if (*fd > 0 || errno != ENOENT)
break;
}
return -errno;
}
int ufs_open3(struct unionfs* fs, const char* path, int flags, mode_t mode, ufs_fd_t* fd)
{
char* real_path = new_real_path(fs, path);
*fd = open(real_path, flags, mode);
free(real_path);
return *fd > 0 ? 0 : -errno;
}
ssize_t ufs_read(struct unionfs* fs, ufs_fd_t fd, off_t offset, size_t size, void* buf)
{
(void) fs;
return pread(fd, buf, size, offset);
}
ssize_t ufs_write(struct unionfs* fs, ufs_fd_t fd, off_t offset, size_t size, const void* data)
{
(void) fs;
return pwrite(fd, data, size, offset);
}
int ufs_close(struct unionfs* fs, ufs_fd_t fd)
{
(void) fs;
return close(fd) == 0 ? 0 : -errno;
}
int ufs_fsync(struct unionfs* fs, ufs_fd_t fd, int sync_metadata)
{
(void) fs;
int(*func)(int) = sync_metadata ? &fsync : &fdatasync;
return (*func)(fd) == 0 ? 0 : -errno;
}