summaryrefslogtreecommitdiff
path: root/src/mfs_vnops.c
blob: fc029e447799e3ba970d76ca8dffa9eadb9df238 (plain)
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
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
/* -*- Mode: C; tab-width: 8; indent-tabs-mode: t; c-basic-offset: 8 -*- */

/*
 * Musicfs is a FUSE module implementing a media filesystem in userland.
 * Copyright (C) 2008  Ulf Lilleengen, Kjetil Ørbekk
 * 
 * This program is free software; you can redistribute it and/or
 * modify it under the terms of the GNU General Public License version
 * 2, as published by the Free Software Foundation.
 * 
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 * 
 * You should have received a copy of the GNU General Public License along
 * with this program; if not, write to the Free Software Foundation, Inc.,
 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
 *
 * A copy of the license can typically be found in COPYING
 */

#include <sys/types.h>
#include <sys/time.h>
#include <dirent.h>
#include <sys/param.h>
#include <sys/uio.h>
#include <unistd.h>
#include <pthread.h>

#include <stdio.h>
#include <stdlib.h>
#include <errno.h>
#include <string.h>
#include <err.h>

#include <fusever.h>
#include <fuse.h>
#include <tag_c.h>
#include <musicfs.h>
#include <debug.h>

static int mfs_getattr (const char *path, struct stat *stbuf)
{
	
	char *realpath;
	int res;

	int status = 0;
	memset (stbuf, 0, sizeof (struct stat));

	if (strcmp(path, "/") == 0) {
		stbuf->st_mode = S_IFDIR | 0755;
		stbuf->st_nlink = 2;
		return 0;
	}

	if (strcmp(path, "/.config") == 0) {
		char *mfsrc = mfs_get_home_path(".mfsrc");
		if (mfsrc == NULL)
			return (-ENOMEM);
		res = stat(mfsrc, stbuf);
		DEBUG("stat result for %s: %d\n", mfsrc, res);
		free(mfsrc);
		return (res);
	}

	enum mfs_filetype type = mfs_get_filetype(path);
	switch (type) {
	case MFS_DIRECTORY:
		stbuf->st_mode = S_IFDIR | 0555;
		stbuf->st_nlink = 1;
		stbuf->st_size = 12;
		return 0;

	case MFS_FILE:
		realpath = NULL;
		status = mfs_realpath(path, &realpath);
		if (status != 0)
			return status;
		res = stat(realpath, stbuf);
		free(realpath);
		return (res);
	}
	return (-ENOENT);
}


static int mfs_readdir (const char *path, void *buf, fuse_fill_dir_t filler,
						off_t offset, struct fuse_file_info *fi)
{
	struct filler_data fd;
	struct lookuphandle *lh;

	filler (buf, ".", NULL, 0);
	filler (buf, "..", NULL, 0);
	fd.buf = buf;
	fd.filler = filler;

	if (!strcmp(path, "/")) {
		filler(buf, "Artists", NULL, 0);
		filler(buf, "Genres", NULL, 0);
		filler(buf, "Tracks", NULL, 0);
		filler(buf, "Albums", NULL, 0);
		filler(buf, ".config", NULL, 0);
		return (0);
	}

	/*
	 * 1. Parse the path.
	 * 2. Find the mp3s that matches the tags given from the path.
	 * 3. Return the list of those mp3s.
	 */
	if (strncmp(path, "/Artists", 8) == 0) {
		mfs_lookup_artist(path, &fd);
		return (0);
	} else if (strncmp(path, "/Genres", 7) == 0) {
		mfs_lookup_genre(path, &fd);
		return (0);
	} else if (strcmp(path, "/Tracks") == 0) {
		lh = mfs_lookup_start(0, &fd, mfs_lookup_list,
		    "SELECT DISTINCT artistname||' - '||title||'.'||extension "
		    "FROM song");
		mfs_lookup_finish(lh);
		return (0);
	} else if (strncmp(path, "/Albums", 7) == 0) {
		mfs_lookup_album(path, &fd);
		return (0);
	}

	return (-ENOENT);
}

static int mfs_open (const char *path, struct fuse_file_info *fi)
{
	char *realpath;
	int fd, status;

	if (strcmp(path, "/.config") == 0)
		return (0);

	status = mfs_realpath(path, &realpath);
	if (status != 0)
		return status;
	fd = open(realpath, O_RDONLY);
	free(realpath);
	if (fd < 0)
		return (-errno);
	fi->fh = (uint64_t)fd;
	return (0);
}

static int mfs_read (const char *path, char *buf, size_t size, off_t offset,
					 struct fuse_file_info *fi)
{
	int fd;
	size_t bytes;

	DEBUG("read: path(%s) offset(%d) size(%d)\n", path, (int)offset,
		  (int)size);
	if (strcmp(path, "/.config") == 0) {
		char *mfsrc = mfs_get_home_path(".mfsrc");
		if (mfsrc == NULL)
			return (-ENOMEM);
		int fd = open(mfsrc, O_RDONLY);
		free(mfsrc);
		lseek(fd, offset, SEEK_SET);
		bytes = read(fd, buf, size);
		close(fd);
		return (bytes);
	}

	fd = (int)fi->fh;
	if (fd < 0)
		return (-EIO);
	lseek(fd, offset, SEEK_SET);
	bytes = read(fd, buf, size);
	return (bytes);

	/*
	 * 1. Find the mnode given the path. If not in cache, read through mp3
	 *    list to find it. 
	 * 2. Read the offset of the mp3 and return the data.
	 */
}

static int mfs_write(const char *path, const char *buf, size_t size,
					 off_t offset, struct fuse_file_info *fi)
{
	size_t bytes;

	DEBUG("write: path(%s) offset(%d) size(%d)\n", path, (int)offset,
		  (int)size);

	if (strcmp(path, "/.config") == 0) {
		char *mfsrc = mfs_get_home_path(".mfsrc");
		if (mfsrc == NULL)
			return (-ENOMEM);
		int fd = open(mfsrc, O_WRONLY);
		free(mfsrc);
		lseek(fd, offset, SEEK_SET);
		bytes = write(fd, buf, size);
		close(fd);
		return (bytes);
	}

	return (-1);	
}

static int mfs_create(const char *path, mode_t mode, struct fuse_file_info *fi)
{
	DEBUG("create %s\n", path);
	return (-1);
}

static int mfs_mknod(const char *path, mode_t mode, dev_t rdev)
{
	DEBUG("mknod %s\n", path);
	return (-1);
}

static int mfs_truncate(const char *path, off_t size)
{
	char *mfsrc;
	int res;
	DEBUG("truncating %s to size %d\n", path, (int)size);

	if (strcmp(path, "/.config") == 0) {
		mfsrc = mfs_get_home_path(".mfsrc");
		if (mfsrc == NULL)
			return (-ENOMEM);
		res = truncate(mfsrc, size);
		DEBUG("truncated %s with result: %d\n", mfsrc, res);
		free(mfsrc);
		return (res);
	}

	return (-1);
}

static int mfs_flush(const char *path, struct fuse_file_info *fi)
{
	int fd;

	DEBUG("flushing path %s\n", path);
	if (strcmp(path, "/.config") == 0) {
		return (0);
	}

	fd = (int)fi->fh;
	/* Fd is not valid, return flush error. */
	if (fd < 0)
		return (-ENOENT);
	return (0);
}

static int mfs_fsync(const char *path, int datasync,
					 struct fuse_file_info *fi)
{
	int fd;
	DEBUG("fsync path %s\n", path);

	if (strcmp(path, "/.config") == 0) {
		return (0);
	}

	fd = (int)fi->fh;
	/* Fd is not valid, return fsync error. */
	if (fd < 0)
		return (-ENOENT);
	return (fsync(fd));
}

static int mfs_release(const char *path, struct fuse_file_info *fi)
{
	int fd;

	DEBUG("release %s\n", path);

	if (strcmp(path, "/.config") == 0) {
		/* Reload configuration file */
		mfs_reload_config();
	}

	fd = (int)fi->fh;
	if (fd > 0)
		close(fd);

	return (0);
}

static int mfs_chmod(const char *path, mode_t mode)
{
	char *mfsrc;
	int ret;

	DEBUG("chmod %s, %d\n", path, (int)mode);
	if (strcmp(path, "/.config") == 0) {
		mfsrc = mfs_get_home_path(".mfsrc");
		if (mfsrc == NULL)
			return (-ENOMEM);
		ret = chmod(mfsrc, mode);
		free(mfsrc);
		return (ret);
	}
	
	return (-1);
}

static int mfs_utimens(const char *path, const struct timespec tv[2])
{
	char *mfsrc;
	int ret;

	DEBUG("utime %s\n", path);
	
	if (strcmp(path, "/.config") == 0) {
		mfsrc = mfs_get_home_path(".mfsrc");
		if (mfsrc == NULL)
			return (-ENOMEM);
		ret = 0; /* utimes(mfsrc, tval); */
		free(mfsrc);
		return (ret);
	}

	return (-1);
}

static int mfs_access(const char *path, int mask)
{
	DEBUG("access called for path %s\n", path);
	return (0);
}

static int mfs_setxattr(const char *path, const char *name,
						const char *val, size_t size, int flags)
{
	DEBUG("setxattr on path %s: %s=%s\n", path, name, val);
	return (0);
}

static struct fuse_operations mfs_ops = {
	.getattr    = mfs_getattr,
	.readdir    = mfs_readdir,
	.open       = mfs_open,
	.read       = mfs_read,
	.write      = mfs_write,
	.mknod      = mfs_mknod,
	.create     = mfs_create,
	.truncate   = mfs_truncate,
	.flush      = mfs_flush,
	.fsync      = mfs_fsync,
	.chmod      = mfs_chmod,
	.release    = mfs_release,
	.utimens    = mfs_utimens,
	.setxattr   = mfs_setxattr,
};

static int musicfs_opt_proc (void *data, const char *arg, int key,
						   struct fuse_args *outargs)
{
	/* Using config now. This is how we added the additional parameter: */
	/* 	static int musicpath_set = 0; */
	/* 	if (key == FUSE_OPT_KEY_NONOPT && !musicpath_set) { */
	/* 		/\* The source directory isn't already set, let's do it *\/ */
	/* 		strcpy(musicpath, arg); */
	/* 		musicpath_set = 1; */
	/* 		return (0); */
	/* 	} */

	return (1);
}

int
mfs_run(int argc, char **argv)
{
	int ret;

	if (argc < 1) {
		fprintf(stderr, "Usage: %s <mountpoint>\n", argv[0]); 
		return (-1);
	}

	/* Make a copy of the arguments */
	char **argv_ = malloc(sizeof(char*) * (argc+1));
	for (int i = 0; i < argc; i++)
		asprintf(argv_+i, argv[i]);
	argv_[argc] = NULL;

	struct fuse_args args = { argc, argv_, 1 };

	/* Until we fix some bugs, these are mandatory */
	fuse_opt_add_arg(&args, "-s");
	fuse_opt_add_arg(&args, "-d");

	if (fuse_opt_parse(&args, NULL, NULL, musicfs_opt_proc) != 0)
		exit (1);

	mfs_init();

	ret = 0;
	ret = fuse_main(args.argc, args.argv, &mfs_ops, NULL);
	return (ret);
}