xref: /haiku/src/bin/isvolume.cpp (revision 25a7b01d15612846f332751841da3579db313082)
1422f8ec3SStefano Ceccherini /*
22d294045SStephan Aßmus  * Copyright 2002-2009, 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
72d294045SStephan Aßmus  *		Stephan Aßmus <superstippi@gmx.de>
8422f8ec3SStefano Ceccherini  */
917049c45SAxel Dörfler 
10422f8ec3SStefano Ceccherini #include <fs_info.h>
11422f8ec3SStefano Ceccherini 
1217049c45SAxel Dörfler #include <stdio.h>
1317049c45SAxel Dörfler #include <string.h>
1417049c45SAxel Dörfler 
152d294045SStephan Aßmus #include <DiskDevice.h>
162d294045SStephan Aßmus #include <DiskDeviceRoster.h>
172d294045SStephan Aßmus #include <Volume.h>
182d294045SStephan Aßmus 
19191732a4SStefano Ceccherini 
20191732a4SStefano Ceccherini static void
usage()21191732a4SStefano Ceccherini usage()
22191732a4SStefano Ceccherini {
23191732a4SStefano Ceccherini 	fprintf(stderr,
24191732a4SStefano Ceccherini 		"Usage: isvolume {-OPTION} [volumename]\n"
25191732a4SStefano Ceccherini 		"   Where OPTION is one of:\n"
26191732a4SStefano Ceccherini 		"      -readonly         - volume is read-only\n"
272d294045SStephan Aßmus 		"      -readonly-partion - partition for the volume is read-only\n"
28191732a4SStefano Ceccherini 		"      -query            - volume supports queries\n"
29191732a4SStefano Ceccherini 		"      -attribute        - volume supports attributes\n"
30191732a4SStefano Ceccherini 		"      -mime             - volume supports MIME information\n"
31191732a4SStefano Ceccherini 		"      -shared           - volume is shared\n"
32191732a4SStefano Ceccherini 		"      -persistent       - volume is backed on permanent storage\n"
33191732a4SStefano Ceccherini 		"      -removable        - volume is on removable media\n"
34191732a4SStefano Ceccherini 		"   If the option is true for the named volume, 'yes' is printed\n"
35191732a4SStefano Ceccherini 		"   and if the option is false, 'no' is printed. Multiple options\n"
36191732a4SStefano Ceccherini 		"   can be specified in which case all of them must be true.\n\n"
372d294045SStephan Aßmus 		"   If no volume is specified, the volume of the current directory is "
382d294045SStephan Aßmus 		"assumed.\n");
39191732a4SStefano Ceccherini }
40191732a4SStefano Ceccherini 
4117049c45SAxel Dörfler 
42422f8ec3SStefano Ceccherini int
main(int argc,char ** argv)43aacebf53SMichael Lotz main(int argc, char** argv)
44422f8ec3SStefano Ceccherini {
45422f8ec3SStefano Ceccherini 	dev_t volumeDevice = dev_for_path(".");
46422f8ec3SStefano Ceccherini 	uint32 isVolumeFlags = 0;
47422f8ec3SStefano Ceccherini 	fs_info volumeInfo;
482d294045SStephan Aßmus 	bool doPartitionReadOnlyCheck = false;
4917049c45SAxel Dörfler 
50422f8ec3SStefano Ceccherini 	for (int i = 1; i < argc; i++) {
51422f8ec3SStefano Ceccherini 		if (!strcmp(argv[i], "--help")) {
52191732a4SStefano Ceccherini 			usage();
53422f8ec3SStefano Ceccherini 			return 0;
5417049c45SAxel Dörfler 		}
5517049c45SAxel Dörfler 
56422f8ec3SStefano Ceccherini 		if (argv[i][0] == '-') {
572d294045SStephan Aßmus 			if (strcmp(argv[i], "-readonly") == 0)
58422f8ec3SStefano Ceccherini 				isVolumeFlags |= B_FS_IS_READONLY;
592d294045SStephan Aßmus 			else if (strcmp(argv[i], "-query") == 0)
60422f8ec3SStefano Ceccherini 				isVolumeFlags |= B_FS_HAS_QUERY;
612d294045SStephan Aßmus 			else if (strcmp(argv[i], "-attribute") == 0)
62422f8ec3SStefano Ceccherini 				isVolumeFlags |= B_FS_HAS_ATTR;
632d294045SStephan Aßmus 			else if (strcmp(argv[i], "-mime") == 0)
64422f8ec3SStefano Ceccherini 				isVolumeFlags |= B_FS_HAS_MIME;
652d294045SStephan Aßmus 			else if (strcmp(argv[i], "-shared") == 0)
66422f8ec3SStefano Ceccherini 				isVolumeFlags |= B_FS_IS_SHARED;
672d294045SStephan Aßmus 			else if (strcmp(argv[i], "-persistent") == 0)
68422f8ec3SStefano Ceccherini 				isVolumeFlags |= B_FS_IS_PERSISTENT;
692d294045SStephan Aßmus 			else if (strcmp(argv[i], "-removable") == 0)
70422f8ec3SStefano Ceccherini 				isVolumeFlags |= B_FS_IS_REMOVABLE;
712d294045SStephan Aßmus 			else if (strcmp(argv[i], "-readonly-partion"))
722d294045SStephan Aßmus 				doPartitionReadOnlyCheck = true;
73422f8ec3SStefano Ceccherini 			else {
74422f8ec3SStefano Ceccherini 				fprintf(stderr,
752d294045SStephan Aßmus 					"%s: option %s is not understood (use --help for help)\n",
762d294045SStephan Aßmus 					argv[0], argv[i]);
7713b0cc8cSStefano Ceccherini 				return 1;
7817049c45SAxel Dörfler 			}
79422f8ec3SStefano Ceccherini 		} else {
80422f8ec3SStefano Ceccherini 			volumeDevice = dev_for_path(argv[i]);
8117049c45SAxel Dörfler 
82422f8ec3SStefano Ceccherini 			if (volumeDevice < 0) {
832d294045SStephan Aßmus 				fprintf(stderr, "%s: can't get information about volume: %s\n",
842d294045SStephan Aßmus 					argv[0], argv[i]);
8513b0cc8cSStefano Ceccherini 				return 1;
8617049c45SAxel Dörfler 			}
8717049c45SAxel Dörfler 		}
8817049c45SAxel Dörfler 	}
8917049c45SAxel Dörfler 
902d294045SStephan Aßmus 	if (doPartitionReadOnlyCheck) {
912d294045SStephan Aßmus 		// This requires an extra code-path, because volumes may now appear
922d294045SStephan Aßmus 		// writable, but only because of the "write" file-system overlay.
932d294045SStephan Aßmus 		BVolume volume(volumeDevice);
942d294045SStephan Aßmus 		status_t ret = volume.InitCheck();
952d294045SStephan Aßmus 		if (ret != B_OK) {
96*8110732bSAlex Smith 			fprintf(stderr, "%s: failed to get BVolume for device %" B_PRIdDEV
97*8110732bSAlex Smith 				": %s\n", argv[0], volumeDevice, strerror(ret));
982d294045SStephan Aßmus 			return 1;
992d294045SStephan Aßmus 		}
1002d294045SStephan Aßmus 
1012d294045SStephan Aßmus 		BDiskDeviceRoster roster;
1022d294045SStephan Aßmus 		BDiskDevice diskDevice;
1032d294045SStephan Aßmus 		BPartition* partition;
1045be48305SStephan Aßmus 		ret = roster.FindPartitionByVolume(volume, &diskDevice, &partition);
1052d294045SStephan Aßmus 		if (ret != B_OK) {
106*8110732bSAlex Smith 			fprintf(stderr, "%s: failed to get partition for device %" B_PRIdDEV
107*8110732bSAlex Smith 				": %s\n", argv[0], volumeDevice, strerror(ret));
1082d294045SStephan Aßmus 			return 1;
1092d294045SStephan Aßmus 		}
1102d294045SStephan Aßmus 		// check this option directly and not via fs_stat_dev()
1112d294045SStephan Aßmus 		if (partition->IsReadOnly()) {
1122d294045SStephan Aßmus 			printf("yes\n");
1132d294045SStephan Aßmus 			return 0;
1142d294045SStephan Aßmus 		}
1152d294045SStephan Aßmus 	}
1162d294045SStephan Aßmus 
117422f8ec3SStefano Ceccherini 	if (fs_stat_dev(volumeDevice, &volumeInfo) == B_OK) {
118422f8ec3SStefano Ceccherini 		if (volumeInfo.flags & isVolumeFlags)
11917049c45SAxel Dörfler 			printf("yes\n");
12017049c45SAxel Dörfler 		else
12117049c45SAxel Dörfler 			printf("no\n");
12217049c45SAxel Dörfler 
123422f8ec3SStefano Ceccherini 		return 0;
124422f8ec3SStefano Ceccherini 	} else {
125*8110732bSAlex Smith 		fprintf(stderr, "%s: can't get information about dev_t: %" B_PRIdDEV
126*8110732bSAlex Smith 			"\n", argv[0], volumeDevice);
12713b0cc8cSStefano Ceccherini 		return 1;
12817049c45SAxel Dörfler 	}
12917049c45SAxel Dörfler }
130