xref: /haiku/src/bin/isvolume.cpp (revision e221c09e508ffc3c62738140c9b6fc4fa211662a)
1 /*
2  * Copyright 2002-2006, Haiku Inc. All rights reserved.
3  * Distributed under the terms of the MIT License.
4  *
5  * Authors:
6  *		Jonas Sundstrom, jonas.sundstrom@kirilla.com
7  */
8 
9 
10 #include <fs_info.h>
11 
12 #include <stdio.h>
13 #include <string.h>
14 
15 
16 static void
17 usage()
18 {
19 	fprintf(stderr,
20 		"Usage: isvolume {-OPTION} [volumename]\n"
21 		"   Where OPTION is one of:\n"
22 		"           -readonly   - volume is read-only\n"
23 		"           -query      - volume supports queries\n"
24 		"           -attribute  - volume supports attributes\n"
25 		"           -mime       - volume supports MIME information\n"
26 		"           -shared     - volume is shared\n"
27 		"           -persistent - volume is backed on permanent storage\n"
28 		"           -removable  - volume is on removable media\n"
29 		"   If the option is true for the named volume, 'yes' is printed\n"
30 		"   and if the option is false, 'no' is printed. Multiple options\n"
31 		"   can be specified in which case all of them must be true.\n\n"
32 		"   If no volume is specified, the volume of the current directory is assumed.\n");
33 }
34 
35 
36 int
37 main(int32 argc, char** argv)
38 {
39 	dev_t volumeDevice = dev_for_path(".");
40 	uint32 isVolumeFlags = 0;
41 	fs_info volumeInfo;
42 
43 	for (int i = 1; i < argc; i++) {
44 		if (!strcmp(argv[i], "--help")) {
45 			usage();
46 			return 0;
47 		}
48 
49 		if (argv[i][0] == '-') {
50 			if (! strcmp(argv[i], "-readonly"))
51 				isVolumeFlags	|= B_FS_IS_READONLY;
52 			else if (! strcmp(argv[i], "-query"))
53 				isVolumeFlags	|= B_FS_HAS_QUERY;
54 			else if (! strcmp(argv[i], "-attribute"))
55 				isVolumeFlags	|= B_FS_HAS_ATTR;
56 			else if (! strcmp(argv[i], "-mime"))
57 				isVolumeFlags	|= B_FS_HAS_MIME;
58 			else if (! strcmp(argv[i], "-shared"))
59 				isVolumeFlags	|= B_FS_IS_SHARED;
60 			else if (! strcmp(argv[i], "-persistent"))
61 				isVolumeFlags	|= B_FS_IS_PERSISTENT;
62 			else if (! strcmp(argv[i], "-removable"))
63 				isVolumeFlags	|= B_FS_IS_REMOVABLE;
64 			else {
65 				fprintf(stderr,
66 					"%s: option %s is not understood (use --help for help)\n", argv[0], argv[i]);
67 				return 1;
68 			}
69 		} else {
70 			volumeDevice = dev_for_path(argv[i]);
71 
72 			if (volumeDevice < 0) {
73 				fprintf(stderr, "%s: can't get information about volume: %s\n", argv[0], argv[i]);
74 				return 1;
75 			}
76 		}
77 	}
78 
79 	if (fs_stat_dev(volumeDevice, &volumeInfo) == B_OK) {
80 		if (volumeInfo.flags & isVolumeFlags)
81 			printf("yes\n");
82 		else
83 			printf("no\n");
84 
85 		return 0;
86 	} else {
87 		fprintf(stderr, "%s: can't get information about dev_t: %ld\n", argv[0], volumeDevice);
88 		return 1;
89 	}
90 }
91