Browse Source

Create mount point file if it doesn't exsist.

Georgi Chorbadzhiyski 12 years ago
parent
commit
4e5f1e980a
1 changed files with 36 additions and 11 deletions
  1. 36
    11
      fjfs.c

+ 36
- 11
fjfs.c View File

@@ -51,6 +51,9 @@ struct files {
51 51
 };
52 52
 
53 53
 struct files *filelist;
54
+char *filenames;
55
+char *mountpoint;
56
+int mountpoint_created = 0;
54 57
 
55 58
 struct files *files_alloc(void) {
56 59
 	struct files *f = calloc(1, sizeof(struct files));
@@ -215,51 +218,73 @@ static int fuse_read(const char *path, char *buf, size_t size, off_t offset, str
215 218
 	return totreaden;
216 219
 }
217 220
 
221
+static int fjfs_unlink(const char *path) {
222
+	if (strcmp(path, "/") != 0)
223
+		return -ENOENT;
224
+	return unlink(path);
225
+}
226
+
227
+static void fjfs_destroy(void *f __attribute__((unused))) {
228
+	if (mountpoint_created)
229
+		unlink(mountpoint);
230
+}
231
+
218 232
 static struct fuse_operations concatfs_op = {
219 233
 	.getattr	= fuse_getattr,
220 234
 	.read		= fuse_read,
235
+	.unlink		= fjfs_unlink,
236
+	.destroy	= fjfs_destroy,
221 237
 };
222 238
 
223 239
 int main(int argc, char *argv[]) {
224 240
 	int ret;
225
-	char *filenames;
226
-	char *mountpoint_file;
227 241
 	struct stat sb;
228 242
 
229 243
 	if (argc < 3) {
230
-		fprintf(stderr, "Usage: fjfs filelist.txt mount-point-file\n");
244
+		fprintf(stderr, "Usage: %s filelist.txt mount-point-file\n", argv[0]);
231 245
 		exit(EXIT_FAILURE);
232 246
 	}
233 247
 
234
-	filenames = argv[1];
235
-	mountpoint_file = argv[2];
248
+	filenames  = argv[1];
249
+	mountpoint = argv[2];
236 250
 
237
-	if (stat(mountpoint_file, &sb) == -1) {
238
-		fprintf(stderr, "Can't mount on %s : %s\n", mountpoint_file, strerror(errno));
239
-		exit(EXIT_FAILURE);
251
+	if (stat(mountpoint, &sb) == -1) {
252
+		FILE *f = fopen(mountpoint, "wb");
253
+		if (f) {
254
+			mountpoint_created = 1;
255
+			fclose(f);
256
+		} else {
257
+			fprintf(stderr, "Can't create mount point %s : %s\n", mountpoint, strerror(errno));
258
+			exit(EXIT_FAILURE);
259
+		}
240 260
 	} else {
241 261
 		if (!S_ISREG(sb.st_mode)) {
242
-			fprintf(stderr, "%s is not a file!\n", mountpoint_file);
262
+			fprintf(stderr, "%s is not a file!\n", mountpoint);
243 263
 			exit(EXIT_FAILURE);
244 264
 		}
245 265
 	}
246 266
 
247 267
 	filelist = files_alloc();
248 268
 	if (!files_load_filelist(filelist, filenames)) {
249
-		fprintf(stderr, "Error no files loaded from %s.\n", argv[1]);
269
+		fprintf(stderr, "Error no files loaded.\n");
250 270
 		files_free(&filelist);
271
+		if (mountpoint_created)
272
+			unlink(mountpoint);
251 273
 		exit(EXIT_FAILURE);
252 274
 	}
253 275
 
254 276
 	char *fuse_argv[5];
255 277
 	fuse_argv[0] = argv[0];
256
-	fuse_argv[1] = mountpoint_file;
278
+	fuse_argv[1] = mountpoint;
257 279
 	fuse_argv[2] = "-o";
258 280
 	fuse_argv[3] = "nonempty,allow_other,fsname=fjfs";
259 281
 	fuse_argv[4]  = 0;
260 282
 
261 283
 	ret = fuse_main(4, fuse_argv, &concatfs_op, NULL);
262 284
 
285
+	if (mountpoint_created)
286
+		unlink(mountpoint);
287
+
263 288
 	files_free(&filelist);
264 289
 
265 290
 	return ret;

Loading…
Cancel
Save