This repository has been archived by the owner on Oct 4, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 1
/
util.c
212 lines (171 loc) · 3.9 KB
/
util.c
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
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
/*
Copyright (C) 2013 Adam Tkac <[email protected]>
This program can be distributed under the terms of the GNU GPLv3.
See the file COPYING.
This file contains various helper functions.
*/
#include "params.h"
#include "cache.h"
#include "compress_laz.h"
#include "compress_lrzip.h"
#include "log.h"
#include "util.h"
#include <assert.h>
#include <attr/xattr.h>
#include <errno.h>
#include <stdlib.h>
#include <string.h>
#include <sys/fsuid.h>
#include <sys/stat.h>
#include <sys/types.h>
#include <unistd.h>
char
lazfs_exec_hooks(const char *fpath, const char *suffix)
{
size_t len;
int ret;
const char *laz_suffix;
struct stat stbuf;
assert(strlen(suffix) == 4);
/* Don't exec hooks if requested file exists */
ret = lstat(fpath, &stbuf);
if (ret == 0)
return 0;
len = strlen(fpath);
if (len < 4)
return 0;
laz_suffix = fpath + len - 4;
assert(laz_suffix >= fpath);
if (strncmp(laz_suffix, suffix, 4) == 0)
return 1;
return 0;
}
/*
* All the paths I see are relative to the root of the mounted
* filesystem. In order to get to the underlying filesystem, I need to
* have the mountpoint. I'll save it away early on in main(), and then
* whenever I need a path for something I'll call this to construct
* it.
*/
void
lazfs_fullpath(char fpath[PATH_MAX], const char *path)
{
strcpy(fpath, LAZFS_DATA->rootdir);
strncat(fpath, path, PATH_MAX); /* FIXME: long paths will break here */
#if 0
log_debug(" lazfs_fullpath: rootdir = \"%s\", path = \"%s\", fpath = \"%s\"\n",
LAZFS_DATA->rootdir, path, fpath);
#endif
}
int
lazfs_error(const char *str)
{
int ret = -errno;
log_error(" ERROR %s: %s\n", str, strerror(errno));
return ret;
}
int
lazfs_decompress(int sfd, int dfd)
{
return lazfs_laz_decompress(sfd, dfd);
}
int
lazfs_compress(int sfd, int dfd)
{
return lazfs_laz_compress(sfd, dfd);
}
int
lazfs_prepare_tmpfile(const char *path, char *tmppath, int flags, int mode,
int *fdp, int *tmpfdp)
{
int fd = -1, tmpfd = -1, ret;
assert(path != NULL);
assert(tmppath != NULL);
assert(flags != -1 || mode != -1);
assert(fdp != NULL);
assert(tmpfdp != NULL);
log_debug("\nprepare_tmpfile: \"p: %s\", tmpp: \"%s\", fd: \"%d\", "
"tmpfd: \"%d\"\n", path, tmppath, *fdp, *tmpfdp);
if (flags != -1) {
fd = open(path, flags);
if (fd < 0) {
ret = lazfs_error("prepare_tmpfile open");
goto cleanup;
}
} else if (mode != -1) {
fd = creat(path, mode);
if (fd == -1) {
ret = lazfs_error("prepare_tmpfile creat");
goto cleanup;
}
}
tmpfd = mkstemp(tmppath);
if (tmpfd == -1) {
ret = lazfs_error("prepare_tmpfile mkstemp");
goto cleanup;
}
*fdp = fd;
*tmpfdp = tmpfd;
return 0;
cleanup:
if (fd != -1)
close(fd);
if (tmpfd != -1) {
unlink(tmppath);
close(tmpfd);
}
return ret;
}
void
lazfs_finish_tmpfile(char *tmppath, int *fd, int *tmpfd)
{
int ret;
assert(tmppath != NULL);
assert(fd != NULL && *fd > 0);
assert(tmpfd != NULL && *tmpfd > 0);
log_debug("\nfinish_tmpfile: tmppath: \"%s\", fd: \"%d\", "
"tmpfd: \"%d\"\n", tmppath, *fd, *tmpfd);
ret = close(*fd);
assert(ret == 0); /* Close failure indicates a bug */
*fd = -1;
ret = close(*tmpfd);
assert(ret == 0); /* Ditto */
*tmpfd = -1;
ret = unlink(tmppath);
assert(ret == 0); /* Ditto */
}
void
lazfs_setugid(lazfs_ugid_t *ugid)
{
struct fuse_context *fc;
fc = fuse_get_context();
ugid->uid = setfsuid(fc->uid);
ugid->gid = setfsgid(fc->gid);
}
void
lazfs_restoreugid(const lazfs_ugid_t *ugid)
{
setfsuid(ugid->uid);
setfsgid(ugid->gid);
}
#define SIZEATTR "user.lazfssize"
int
lazfs_setsize(const char *path, off_t size)
{
int ret;
ret = setxattr(path, SIZEATTR, &size, sizeof(size), 0);
if (ret == -1)
ret = lazfs_error("lazfs_setsize setxattr");
return ret;
}
int
lazfs_getsize(const char *path, off_t *size)
{
int ret;
ret = getxattr(path, SIZEATTR, size, sizeof(size));
if (ret == -1)
ret = lazfs_error("lazfs_getsize getxattr");
else
ret = 0;
return ret;
}