File: umfuseramfile.c

package info (click to toggle)
umview 0.8.2-1.2
  • links: PTS
  • area: main
  • in suites: bullseye, buster, sid
  • size: 5,472 kB
  • sloc: ansic: 67,309; sh: 11,160; ruby: 914; makefile: 424; python: 141
file content (238 lines) | stat: -rw-r--r-- 5,278 bytes parent folder | download | duplicates (3)
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
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
#include <fuse.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <errno.h>
#include <fcntl.h>
#include <unistd.h>

#define STDSIZE 4096
struct ramfile {
	struct stat stat;
	size_t maxlen;
	char *buf;
};

#define CTIME 1
#define MTIME 2
#define ATIME 4

static void update_stimes(struct stat *st, int flags)
{
	time_t now;
	time (&now);

	if (flags & CTIME) st->st_ctime = now;
	if (flags & MTIME) st->st_mtime = now;
	if (flags & ATIME) st->st_atime = now;
}

static int ramfile_getattr(const char *path, struct stat *stbuf)
{
	struct ramfile *rf=fuse_get_context()->private_data;

	if(rf != NULL && strcmp(path, "/") == 0) {
		memcpy(stbuf, &rf->stat, sizeof(struct stat));
		return 0;
	} else 
		return -ENOENT;
}

static int ramfile_fgetattr (const char *path, struct stat *stbuf, 
		struct fuse_file_info *fi)
{
	return ramfile_getattr(path,stbuf);
}

static int ramfile_truncate(const char *path, off_t length)
{
	struct ramfile *rf=fuse_get_context()->private_data;
	if(rf ==  NULL || strcmp(path, "/") != 0)
		return -ENOENT;

	if (length > rf->maxlen)
		return -EFBIG;
	
	if (length > rf->stat.st_size)
		memset(&rf->buf[rf->stat.st_size],0,length - rf->stat.st_size);
	rf->stat.st_size = length;
	update_stimes(&rf->stat, ATIME | MTIME);

	return 0;
}

static int ramfile_open(const char *path, struct fuse_file_info *fi)
{
	if(strcmp(path, "/") != 0)
		return -ENOENT;

	/* save flags */
	return 0;
}

static int ramfile_read(const char *path, char *buf, size_t size, off_t offset,
		struct fuse_file_info *fi)
{
	size_t len;
	struct ramfile *rf=fuse_get_context()->private_data;

	if(rf == NULL || strcmp(path, "/") != 0)
		return -ENOENT;

	len=rf->stat.st_size;
	if (offset < len) {
		if (offset + size > len)
			size = len - offset;
		memcpy(buf, &(rf->buf[offset]), size);
		update_stimes(&rf->stat, ATIME);
	} else
		size = 0;

	return size;
}

static int ramfile_write(const char *path, const char *buf, size_t size, off_t offset,
		struct fuse_file_info *fi)
{
	size_t len;
	struct ramfile *rf=fuse_get_context()->private_data;

	len=rf->maxlen;
	if (offset < len) { 
		if (offset + size > len)
			size = len - offset;
		memcpy(&(rf->buf[offset]), buf, size);
		if (offset + size > rf->stat.st_size)
			rf->stat.st_size=offset + size;
		update_stimes(&rf->stat, MTIME | ATIME);
	} else
		size = 0;

	return size;
}

static int ramfile_chown(const char *path, uid_t uid, gid_t gid)
{
	struct ramfile *rf=fuse_get_context()->private_data;
	if(rf ==  NULL || strcmp(path, "/") != 0)
		return -ENOENT;
	if (uid != -1)
		rf->stat.st_uid = uid;
	if (gid != -1)
		rf->stat.st_gid = gid;
	return 0;
}

#define CHMOD_MASK (S_IRWXU | S_IRWXG | S_IRWXO | S_ISUID | S_ISGID | S_ISVTX)
static int ramfile_chmod(const char *path, mode_t mode)
{
	struct ramfile *rf=fuse_get_context()->private_data;
	if(rf ==  NULL || strcmp(path, "/") != 0)
		return -ENOENT;
	rf->stat.st_mode = (rf->stat.st_mode & ~CHMOD_MASK) | (mode & CHMOD_MASK);
	return 0;
}

static int ramfile_utimens (const char *path, const struct timespec tv[2])
{
	struct ramfile *rf=fuse_get_context()->private_data;
	if(rf ==  NULL || strcmp(path, "/") != 0)
		return -ENOENT;
	rf->stat.st_atime = tv[0].tv_sec;
	rf->stat.st_mtime = tv[1].tv_sec;
  return 0;
}

void *ramfile_init(struct fuse_conn_info *conn)
{
	struct fuse_context *mycontext;
	mycontext=fuse_get_context();
	return mycontext->private_data;
}

static struct fuse_operations ramfile_oper = {
	.init = ramfile_init,
	.getattr	= ramfile_getattr,
	.fgetattr	= ramfile_fgetattr,
	.truncate	= ramfile_truncate,
	.open	= ramfile_open,
	.read	= ramfile_read,
	.write	= ramfile_write,
	.chmod    = ramfile_chmod,
	.chown    = ramfile_chown,
	.utimens    = ramfile_utimens,

};

static void rf_setsize(char *s,struct ramfile *rf)
{
	if (s) {
		long long size=atoi(s);
		while (*s >= '0' && *s <= '9')
			s++;
		switch (*s) {
			case 'k':
			case 'K': size *= 1024; break;
			case 'm':
			case 'M': size *= 1024 * 1024; break;
			case 'g':
			case 'G': size *= 1024 * 1024 * 1024; break;
		}
		rf->maxlen=size;
	}
}

void ramfile_parse_opts(struct ramfile *rf, char *opts)
{
	/* trivial "size" option parse */
	char *sizeptr;
	if ((sizeptr = strstr(opts,"size=")) != NULL)
		rf_setsize(sizeptr+5,rf);
}

int main(int argc, char *argv[])
{
	struct ramfile *rf=calloc(1,sizeof(struct ramfile));
	char *source=argv[argc-2];
	int i;
	int srcfd=-1;
	if (rf == NULL)
		return -ENODEV;
	rf->maxlen=STDSIZE;
	rf->stat.st_mode = S_IFREG | 0666;
	rf->stat.st_nlink = 1;
	rf->stat.st_size = 0;
	if (strcmp(source,"none") != 0) {
		if (stat(source,&(rf->stat)) < 0) {
			free(rf);
			return -ENOENT;
		}
		if (rf->maxlen < rf->stat.st_size)
			rf->maxlen = rf->stat.st_size;
		if ((srcfd = open(source, O_RDONLY)) < 0) {
			free(rf);
			return -EACCES;
		}
	}
	for (i = 0; i < argc-1; i++) {
		if (strcmp(argv[i],"-o")==0)
			ramfile_parse_opts(rf,argv[i+1]);
	}
	rf->buf=malloc(rf->maxlen);
	if (rf->buf == NULL) {
		free(rf);
		if (srcfd >= 0) close(srcfd);
		return -EINVAL;
	}
	if (srcfd >= 0) {
		if ((rf->stat.st_size = read(srcfd, rf->buf, rf->maxlen)) < 0)
			rf->stat.st_size = 0;
		close(srcfd);
	}
	update_stimes(&rf->stat, CTIME | MTIME | ATIME);
	fuse_main(argc, argv, &ramfile_oper, rf);
	free(rf->buf);
	free(rf);

	return 0;
}