Browse Source

Cleanup mount point handling code.

Georgi Chorbadzhiyski 12 years ago
parent
commit
dd4c6cb771
1 changed files with 30 additions and 20 deletions
  1. 30
    20
      fjfs.c

+ 30
- 20
fjfs.c View File

@@ -270,9 +270,14 @@ static int fjfs_unlink(const char *path) {
270 270
 	return unlink(path);
271 271
 }
272 272
 
273
+
274
+static void clear_mountpoint(void) {
275
+	if (mountpoint_created)
276
+		unlink(mountpoint);
277
+}
278
+
273 279
 static void fjfs_destroy(void *f __attribute__((unused))) {
274
-	if (mountpoint_created)
275
-		unlink(mountpoint);
280
+	clear_mountpoint();
276 281
 }
277 282
 
278 283
 static struct fuse_operations concatfs_op = {
@@ -367,6 +372,27 @@ static void parse_parameters(int argc, char *argv[]) {
367 372
 	}
368 373
 }
369 374
 
375
+static int prepare_mountpoint(void) {
376
+	struct stat sb;
377
+	if (stat(mountpoint, &sb) == -1) {
378
+		// Mount point do not exist.
379
+		FILE *f = fopen(mountpoint, "wb");
380
+		if (!f) {
381
+			fprintf(stderr, "Can't create mount point %s : %s\n", mountpoint, strerror(errno));
382
+			exit(EXIT_FAILURE);
383
+		}
384
+		mountpoint_created = 1;
385
+		fclose(f);
386
+	} else {
387
+		// Mount exist, check if it is a file.
388
+		if (!S_ISREG(sb.st_mode)) {
389
+			fprintf(stderr, "Mount point \"%s\" is not a file!\n", mountpoint);
390
+			exit(EXIT_FAILURE);
391
+		}
392
+	}
393
+	return 1;
394
+}
395
+
370 396
 static int init_filelist(int argc, char *argv[]) {
371 397
 	int i, ret = 0;
372 398
 
@@ -412,31 +438,15 @@ static int mount_fuse(char *program_file) {
412 438
 
413 439
 int main(int argc, char *argv[]) {
414 440
 	int ret = EXIT_FAILURE;
415
-	struct stat sb;
416 441
 
417 442
 	parse_parameters(argc, argv);
418 443
 
419
-	if (stat(mountpoint, &sb) == -1) {
420
-		FILE *f = fopen(mountpoint, "wb");
421
-		if (f) {
422
-			mountpoint_created = 1;
423
-			fclose(f);
424
-		} else {
425
-			fprintf(stderr, "Can't create mount point %s : %s\n", mountpoint, strerror(errno));
426
-			exit(EXIT_FAILURE);
427
-		}
428
-	} else {
429
-		if (!S_ISREG(sb.st_mode)) {
430
-			fprintf(stderr, "%s is not a file!\n", mountpoint);
431
-			exit(EXIT_FAILURE);
432
-		}
433
-	}
444
+	mountpoint_created = prepare_mountpoint();
434 445
 
435 446
 	if (init_filelist(argc, argv))
436 447
 		ret = mount_fuse(argv[0]);
437 448
 
438
-	if (mountpoint_created)
439
-		unlink(mountpoint);
449
+	clear_mountpoint();
440 450
 
441 451
 	files_free(&filelist);
442 452
 

Loading…
Cancel
Save