xref: /haiku/src/bin/isvolume.cpp (revision aacebf533db7f212bf7de6500b272a26fe8ad4d2)
1422f8ec3SStefano Ceccherini /*
2422f8ec3SStefano Ceccherini  * Copyright 2002-2006, Haiku Inc. All rights reserved.
3422f8ec3SStefano Ceccherini  * Distributed under the terms of the MIT License.
4422f8ec3SStefano Ceccherini  *
5422f8ec3SStefano Ceccherini  * Authors:
6422f8ec3SStefano Ceccherini  *		Jonas Sundstrom, jonas.sundstrom@kirilla.com
7422f8ec3SStefano Ceccherini  */
817049c45SAxel Dörfler 
9422f8ec3SStefano Ceccherini 
10422f8ec3SStefano Ceccherini #include <fs_info.h>
11422f8ec3SStefano Ceccherini 
1217049c45SAxel Dörfler #include <stdio.h>
1317049c45SAxel Dörfler #include <string.h>
1417049c45SAxel Dörfler 
15191732a4SStefano Ceccherini 
16191732a4SStefano Ceccherini static void
17191732a4SStefano Ceccherini usage()
18191732a4SStefano Ceccherini {
19191732a4SStefano Ceccherini 	fprintf(stderr,
20191732a4SStefano Ceccherini 		"Usage: isvolume {-OPTION} [volumename]\n"
21191732a4SStefano Ceccherini 		"   Where OPTION is one of:\n"
22191732a4SStefano Ceccherini 		"           -readonly   - volume is read-only\n"
23191732a4SStefano Ceccherini 		"           -query      - volume supports queries\n"
24191732a4SStefano Ceccherini 		"           -attribute  - volume supports attributes\n"
25191732a4SStefano Ceccherini 		"           -mime       - volume supports MIME information\n"
26191732a4SStefano Ceccherini 		"           -shared     - volume is shared\n"
27191732a4SStefano Ceccherini 		"           -persistent - volume is backed on permanent storage\n"
28191732a4SStefano Ceccherini 		"           -removable  - volume is on removable media\n"
29191732a4SStefano Ceccherini 		"   If the option is true for the named volume, 'yes' is printed\n"
30191732a4SStefano Ceccherini 		"   and if the option is false, 'no' is printed. Multiple options\n"
31191732a4SStefano Ceccherini 		"   can be specified in which case all of them must be true.\n\n"
32191732a4SStefano Ceccherini 		"   If no volume is specified, the volume of the current directory is assumed.\n");
33191732a4SStefano Ceccherini }
34191732a4SStefano Ceccherini 
3517049c45SAxel Dörfler 
36422f8ec3SStefano Ceccherini int
37*aacebf53SMichael Lotz main(int argc, char** argv)
38422f8ec3SStefano Ceccherini {
39422f8ec3SStefano Ceccherini 	dev_t volumeDevice = dev_for_path(".");
40422f8ec3SStefano Ceccherini 	uint32 isVolumeFlags = 0;
41422f8ec3SStefano Ceccherini 	fs_info volumeInfo;
4217049c45SAxel Dörfler 
43422f8ec3SStefano Ceccherini 	for (int i = 1; i < argc; i++) {
44422f8ec3SStefano Ceccherini 		if (!strcmp(argv[i], "--help")) {
45191732a4SStefano Ceccherini 			usage();
46422f8ec3SStefano Ceccherini 			return 0;
4717049c45SAxel Dörfler 		}
4817049c45SAxel Dörfler 
49422f8ec3SStefano Ceccherini 		if (argv[i][0] == '-') {
50422f8ec3SStefano Ceccherini 			if (! strcmp(argv[i], "-readonly"))
51422f8ec3SStefano Ceccherini 				isVolumeFlags	|= B_FS_IS_READONLY;
52422f8ec3SStefano Ceccherini 			else if (! strcmp(argv[i], "-query"))
53422f8ec3SStefano Ceccherini 				isVolumeFlags	|= B_FS_HAS_QUERY;
54422f8ec3SStefano Ceccherini 			else if (! strcmp(argv[i], "-attribute"))
55422f8ec3SStefano Ceccherini 				isVolumeFlags	|= B_FS_HAS_ATTR;
56422f8ec3SStefano Ceccherini 			else if (! strcmp(argv[i], "-mime"))
57422f8ec3SStefano Ceccherini 				isVolumeFlags	|= B_FS_HAS_MIME;
58422f8ec3SStefano Ceccherini 			else if (! strcmp(argv[i], "-shared"))
59422f8ec3SStefano Ceccherini 				isVolumeFlags	|= B_FS_IS_SHARED;
60422f8ec3SStefano Ceccherini 			else if (! strcmp(argv[i], "-persistent"))
61422f8ec3SStefano Ceccherini 				isVolumeFlags	|= B_FS_IS_PERSISTENT;
62422f8ec3SStefano Ceccherini 			else if (! strcmp(argv[i], "-removable"))
63422f8ec3SStefano Ceccherini 				isVolumeFlags	|= B_FS_IS_REMOVABLE;
64422f8ec3SStefano Ceccherini 			else {
65422f8ec3SStefano Ceccherini 				fprintf(stderr,
66422f8ec3SStefano Ceccherini 					"%s: option %s is not understood (use --help for help)\n", argv[0], argv[i]);
6713b0cc8cSStefano Ceccherini 				return 1;
6817049c45SAxel Dörfler 			}
69422f8ec3SStefano Ceccherini 		} else {
70422f8ec3SStefano Ceccherini 			volumeDevice = dev_for_path(argv[i]);
7117049c45SAxel Dörfler 
72422f8ec3SStefano Ceccherini 			if (volumeDevice < 0) {
7317049c45SAxel Dörfler 				fprintf(stderr, "%s: can't get information about volume: %s\n", argv[0], argv[i]);
7413b0cc8cSStefano Ceccherini 				return 1;
7517049c45SAxel Dörfler 			}
7617049c45SAxel Dörfler 		}
7717049c45SAxel Dörfler 	}
7817049c45SAxel Dörfler 
79422f8ec3SStefano Ceccherini 	if (fs_stat_dev(volumeDevice, &volumeInfo) == B_OK) {
80422f8ec3SStefano Ceccherini 		if (volumeInfo.flags & isVolumeFlags)
8117049c45SAxel Dörfler 			printf("yes\n");
8217049c45SAxel Dörfler 		else
8317049c45SAxel Dörfler 			printf("no\n");
8417049c45SAxel Dörfler 
85422f8ec3SStefano Ceccherini 		return 0;
86422f8ec3SStefano Ceccherini 	} else {
87422f8ec3SStefano Ceccherini 		fprintf(stderr, "%s: can't get information about dev_t: %ld\n", argv[0], volumeDevice);
8813b0cc8cSStefano Ceccherini 		return 1;
8917049c45SAxel Dörfler 	}
9017049c45SAxel Dörfler }
91