Browse Source

Implement populating of file list from glob and from files on cmd line.

Georgi Chorbadzhiyski 12 years ago
parent
commit
0175379bf7
1 changed files with 84 additions and 20 deletions
  1. 84
    20
      fjfs.c

+ 84
- 20
fjfs.c View File

@@ -35,6 +35,9 @@
35 35
 #include <errno.h>
36 36
 #include <unistd.h>
37 37
 #include <sys/types.h>
38
+#include <dirent.h>
39
+#include <libgen.h>
40
+#include <fnmatch.h>
38 41
 #include <fuse.h>
39 42
 
40 43
 /* Handle list of files */
@@ -89,8 +92,7 @@ static void files_free(struct files **pfiles) {
89 92
 	}
90 93
 }
91 94
 
92
-#if 0
93
-void files_dump(struct files *files) {
95
+static void files_dump(struct files *files) {
94 96
 	int i;
95 97
 	fprintf(stdout,"num_files:%d\n", files->num_files);
96 98
 	fprintf(stdout,"alloc_files:%d\n", files->alloc_files);
@@ -102,7 +104,6 @@ void files_dump(struct files *files) {
102 104
 		fprintf(stdout,"file[%d]->size=%llu\n", i, (unsigned long long)f->size);
103 105
 	}
104 106
 }
105
-#endif
106 107
 
107 108
 static int files_add_file(struct files *files, char *filename) {
108 109
 	int ret = 0;
@@ -159,6 +160,41 @@ static int files_load_filelist(struct files *files, char *filename) {
159 160
 	return ret;
160 161
 }
161 162
 
163
+static int files_load_glob(struct files *files, char *glob_match) {
164
+	int i, entries, ret = 0;
165
+	struct dirent **namelist;
166
+	char *f1 = strdup(glob_match);
167
+	char *f2 = strdup(glob_match);
168
+	char *dir  = dirname(f1);
169
+	char *match = basename(f2);
170
+
171
+	if (debug)
172
+		fprintf(stderr, "dir:%s match:%s req:%s\n", dir, match, glob_match);
173
+	entries = scandir(dir, &namelist, NULL, alphasort);
174
+	if (entries < 0) {
175
+		fprintf(stderr, "scandir %s : %s\n", dir, strerror(errno));
176
+		free(f1);
177
+		free(f2);
178
+		return 0;
179
+	}
180
+
181
+	char *filename = calloc(1, strlen(dir) + NAME_MAX + 16);
182
+	for (i=0;i<entries;i++) {
183
+		struct dirent *entry = namelist[i];
184
+		if (fnmatch(match, entry->d_name, FNM_PATHNAME) == 0) {
185
+			sprintf(filename, "%s/%s", dir, entry->d_name);
186
+			ret += files_add_file(files, filename);
187
+		}
188
+		free(entry);
189
+	}
190
+	free(filename);
191
+	free(namelist);
192
+	free(f1);
193
+	free(f2);
194
+	return ret;
195
+}
196
+
197
+
162 198
 static int fuse_getattr(const char *path, struct stat *stbuf) {
163 199
 	if (strcmp(path, "/") != 0)
164 200
 		return -ENOENT;
@@ -331,6 +367,49 @@ static void parse_parameters(int argc, char *argv[]) {
331 367
 	}
332 368
 }
333 369
 
370
+static int init_filelist(int argc, char *argv[]) {
371
+	int i, ret = 0;
372
+
373
+	filelist = files_alloc();
374
+	if (!filelist)
375
+		return ret;
376
+
377
+	switch (list_mode) {
378
+	case FL_FILE:
379
+		ret = files_load_filelist(filelist, filenames);
380
+		break;
381
+	case FL_GLOB:
382
+		ret = files_load_glob(filelist, filenames);
383
+		break;
384
+	case FL_ARGS:
385
+		for (i = optind + 1; i < argc; i++) {
386
+			ret += files_add_file(filelist, argv[i]);
387
+		}
388
+		break;
389
+	}
390
+
391
+	if (debug)
392
+		files_dump(filelist);
393
+
394
+	if (!ret)
395
+		fprintf(stderr, "ERROR: No files were selected for joining.\n");
396
+
397
+	return ret;
398
+}
399
+
400
+static int mount_fuse(char *program_file) {
401
+	char *fuse_argv[5];
402
+	fuse_argv[0] = program_file;
403
+	fuse_argv[1] = mountpoint;
404
+	fuse_argv[2] = "-o";
405
+	if (!allow_other)
406
+		fuse_argv[3] = "nonempty,fsname=fjfs";
407
+	else
408
+		fuse_argv[3] = "nonempty,allow_other,fsname=fjfs";
409
+	fuse_argv[4]  = 0;
410
+	return fuse_main(4, fuse_argv, &concatfs_op, NULL);
411
+}
412
+
334 413
 int main(int argc, char *argv[]) {
335 414
 	int ret = EXIT_FAILURE;
336 415
 	struct stat sb;
@@ -353,23 +432,8 @@ int main(int argc, char *argv[]) {
353 432
 		}
354 433
 	}
355 434
 
356
-	filelist = files_alloc();
357
-	if (!files_load_filelist(filelist, filenames)) {
358
-		fprintf(stderr, "Error no files loaded.\n");
359
-		files_free(&filelist);
360
-		if (mountpoint_created)
361
-			unlink(mountpoint);
362
-		exit(EXIT_FAILURE);
363
-	}
364
-
365
-	char *fuse_argv[5];
366
-	fuse_argv[0] = argv[0];
367
-	fuse_argv[1] = mountpoint;
368
-	fuse_argv[2] = "-o";
369
-	fuse_argv[3] = "nonempty,allow_other,fsname=fjfs";
370
-	fuse_argv[4]  = 0;
371
-
372
-	ret = fuse_main(4, fuse_argv, &concatfs_op, NULL);
435
+	if (init_filelist(argc, argv))
436
+		ret = mount_fuse(argv[0]);
373 437
 
374 438
 	if (mountpoint_created)
375 439
 		unlink(mountpoint);

Loading…
Cancel
Save