Fixed mountvolume to make it compile and somewhat work (only tested listing volumes yet).

Added it to the build.


git-svn-id: file:///srv/svn/repos/haiku/haiku/trunk@12545 a95241bf-73f2-0310-859d-f6bbb57e9c96
This commit is contained in:
Axel Dörfler
2005-05-03 00:31:52 +00:00
parent b07ce77575
commit 509676a027
2 changed files with 121 additions and 70 deletions
+8 -1
View File
@@ -2,6 +2,7 @@ SubDir OBOS_TOP src bin ;
UsePrivateHeaders kernel ; UsePrivateHeaders kernel ;
UsePrivateHeaders shared ; UsePrivateHeaders shared ;
UsePrivateHeaders storage ;
# standard commands that don't need any additional library # standard commands that don't need any additional library
StdBinCommands StdBinCommands
@@ -69,7 +70,13 @@ StdBinCommands
shutdown.cpp shutdown.cpp
version.cpp version.cpp
# yes.cpp # yes.cpp
: libroot.so be ; : libroot.so libbe.so ;
# standard commands that need libbe.so, libstdc++.so
StdBinCommands
mountvolume.cpp
: libroot.so libbe.so libstdc++.r4.so
;
# standard commands that need libbe.so, libtranslation.so # standard commands that need libbe.so, libtranslation.so
StdBinCommands StdBinCommands
+113 -69
View File
@@ -1,4 +1,8 @@
// mountvolume.cpp /*
* Copyright 2005, Ingo Weinhold, [email protected]. All rights reserved.
* Distributed under the terms of the MIT License.
*/
#include <stdio.h> #include <stdio.h>
#include <string.h> #include <string.h>
@@ -9,66 +13,71 @@
#include <DiskDevice.h> #include <DiskDevice.h>
#include <DiskDevicePrivate.h> #include <DiskDevicePrivate.h>
#include <DiskDeviceRoster.h> #include <DiskDeviceRoster.h>
#include <DiskDeviceTypes.h>
#include <DiskDeviceList.h>
#include <Partition.h> #include <Partition.h>
#include <Path.h>
#include <fs_volume.h>
extern const char *__progname;
typedef set<string> StringSet; typedef set<string> StringSet;
// usage // usage
static const char *kUsage = static const char *kUsage =
"%s <options> [ <volume name> ... ]\n" "%s <options> [ <volume name> ... ]\n"
"Mounts the volume with name <volume name>, if given. Lists info about\n" "Mounts the volume with name <volume name>, if given. Lists info about\n"
"mounted and mountable volumes and mounts/unmounts volumes.\n" "mounted and mountable volumes and mounts/unmounts volumes.\n"
"\n" "\n"
"The terminology is actually not quite correct: By volumes only partitions\n" "The terminology is actually not quite correct: By volumes only partitions\n"
"living on disk devices are meant.\n" "living on disk devices are meant.\n"
"\n" "\n"
"Options:\n" "Options:\n"
"[general]\n" "[general]\n"
" -s - silent; don't print info about (un)mounting\n" " -s - silent; don't print info about (un)mounting\n"
" -h, --help - print this info text\n" " -h, --help - print this info text\n"
"\n" "\n"
"[mounting]\n" "[mounting]\n"
" -all - mount all mountable volumes\n" " -all - mount all mountable volumes\n"
" -allbfs - mount all mountable BFS volumes\n" " -allbfs - mount all mountable BFS volumes\n"
" -allhfs - mount all mountable HFS volumes\n" " -allhfs - mount all mountable HFS volumes\n"
" -alldos - mount all mountable DOS volumes\n" " -alldos - mount all mountable DOS volumes\n"
" -ro - mount volumes read-only\n" " -ro - mount volumes read-only\n"
" -unmount <volume> - unmount the volume with the name <volume>\n" " -unmount <volume> - unmount the volume with the name <volume>\n"
"\n" "\n"
"[info]\n" "[info]\n"
" -p, -l - list all mounted and mountable volumes\n" " -p, -l - list all mounted and mountable volumes\n"
" -lh - list all existing volumes (incl. not-mountable ones)\n" " -lh - list all existing volumes (incl. not-mountable ones)\n"
" -dd - list all disk existing devices\n" " -dd - list all disk existing devices\n"
"\n" "\n"
"[obsolete]\n" "[obsolete]\n"
" -r - ignored\n" " -r - ignored\n"
" -publishall - ignored\n" " -publishall - ignored\n"
" -publishbfs - ignored\n" " -publishbfs - ignored\n"
" -publishhfs - ignored\n" " -publishhfs - ignored\n"
" -publishdos - ignored\n" " -publishdos - ignored\n"
; ;
// application name // application name
const char *kAppName = "mountvolume"; const char *kAppName = __progname;
// print_usage // print_usage
static static
void void
print_usage(const char *appName, bool error) print_usage(bool error)
{ {
if (appName) { fprintf(error ? stderr : stdout, kUsage, kAppName);
if (const char *lastSlash = strrchr(appName, '/')
appName = lastSlash
} else
appName = kAppName;
fprintf((error ? stderr : stdout), kUsage, appName);
} }
// print_usage_and_exit // print_usage_and_exit
static static
print_usage_and_exit(const char *appName, bool error) void
print_usage_and_exit(bool error)
{ {
print_usage(appName, error); print_usage(error);
exit(error ? 0 : 1); exit(error ? 0 : 1);
} }
@@ -86,7 +95,7 @@ struct MountVisitor : public BDiskDeviceVisitor {
virtual bool Visit(BDiskDevice *device) virtual bool Visit(BDiskDevice *device)
{ {
Visit(device, 0); return Visit(device, 0);
} }
virtual bool Visit(BPartition *partition, int32 level) virtual bool Visit(BPartition *partition, int32 level)
@@ -101,21 +110,21 @@ struct MountVisitor : public BDiskDeviceVisitor {
bool mount = false; bool mount = false;
if (name && toMount.find(name) != toMount.end()) { if (name && toMount.find(name) != toMount.end()) {
toMount.erase(name); toMount.erase(name);
if (!partition->IsMounted() if (!partition->IsMounted())
mount = true; mount = true;
else if (!silent) else if (!silent)
fprintf(stderr, "Volume `%s' already mounted.\n", name); fprintf(stderr, "Volume `%s' already mounted.\n", name);
} else if (mountAll) { } else if (mountAll) {
mount = true; mount = true;
} else if (mountBFS && contentType } else if (mountBFS && type != NULL
&& strcmp(contentType, kPartitionTypeBFS) == 0) { && strcmp(type, kPartitionTypeBFS) == 0) {
mount = true; mount = true;
} else if (mountHFS && contentType } else if (mountHFS && type != NULL
&& strcmp(contentType, kPartitionTypeHFS) == 0) { && strcmp(type, kPartitionTypeHFS) == 0) {
mount = true; mount = true;
} else if (mountDOS && contentType } else if (mountDOS && type != NULL
&& (strcmp(contentType, kPartitionTypeFAT12) == 0 && (strcmp(type, kPartitionTypeFAT12) == 0
|| strcmp(contentType, kPartitionTypeFAT32) == 0)) { || strcmp(type, kPartitionTypeFAT32) == 0)) {
mount = true; mount = true;
} }
@@ -123,7 +132,7 @@ struct MountVisitor : public BDiskDeviceVisitor {
bool unmount = false; bool unmount = false;
if (name && toUnmount.find(name) != toUnmount.end()) { if (name && toUnmount.find(name) != toUnmount.end()) {
toUnmount.erase(name); toUnmount.erase(name);
if (!partition->IsMounted() { if (!partition->IsMounted()) {
unmount = true; unmount = true;
mount = false; mount = false;
} else if (!silent) } else if (!silent)
@@ -138,8 +147,8 @@ struct MountVisitor : public BDiskDeviceVisitor {
if (error == B_OK) { if (error == B_OK) {
printf("Volume `%s' mounted successfully.\n", name); printf("Volume `%s' mounted successfully.\n", name);
} else { } else {
fprintf("Failed to mount volume `%s': %s\n", name, fprintf(stderr, "Failed to mount volume `%s': %s\n",
strerror(error)); name, strerror(error));
} }
} }
} else if (unmount) { } else if (unmount) {
@@ -148,11 +157,13 @@ struct MountVisitor : public BDiskDeviceVisitor {
if (error == B_OK) { if (error == B_OK) {
printf("Volume `%s' unmounted successfully.\n", name); printf("Volume `%s' unmounted successfully.\n", name);
} else { } else {
fprintf("Failed to unmount volume `%s': %s\n", name, fprintf(stderr, "Failed to unmount volume `%s': %s\n",
strerror(error)); name, strerror(error));
} }
} }
} }
return true;
} }
bool silent; bool silent;
@@ -173,13 +184,31 @@ struct PrintPartitionsVisitor : public BDiskDeviceVisitor {
{ {
} }
bool IsUsed()
{
return listMountablePartitions || listAllPartitions;
}
virtual bool Visit(BDiskDevice *device) virtual bool Visit(BDiskDevice *device)
{ {
Visit(device, 0); return Visit(device, 0);
} }
virtual bool Visit(BPartition *partition, int32 level) virtual bool Visit(BPartition *partition, int32 level)
{ {
// get name and type
const char *name = partition->ContentName();
if (name == NULL) {
name = partition->Name();
if (name == NULL)
name = "<unknown>";
}
BPath path;
partition->GetMountPoint(&path);
printf("%-16s %-20s %s\n", name, partition->ContentType(), path.Path());
return true;
} }
bool listMountablePartitions; bool listMountablePartitions;
@@ -192,15 +221,15 @@ int
main(int argc, char **argv) main(int argc, char **argv)
{ {
if (argc < 2) if (argc < 2)
print_usage_and_exit(argv[0], true); print_usage_and_exit(true);
MountVisitor mountVisitor; MountVisitor mountVisitor;
PrintPartitionsVisitor printPartitionsVisitor; PrintPartitionsVisitor printPartitionsVisitor;
bool listAllDevices = false; bool listAllDevices = false;
// parse arguments // parse arguments
int argi = 1;
while (argi < argc) { for (int argi = 1; argi < argc; argi++) {
const char *arg = argv[argi]; const char *arg = argv[argi];
if (arg[0] != '\0' && arg[0] != '-') { if (arg[0] != '\0' && arg[0] != '-') {
@@ -208,21 +237,21 @@ main(int argc, char **argv)
} else if (strcmp(arg, "-s") == 0) { } else if (strcmp(arg, "-s") == 0) {
mountVisitor.silent = true; mountVisitor.silent = true;
} else if (strcmp(arg, "-h") == 0 || strcmp(arg, "--help") == 0) { } else if (strcmp(arg, "-h") == 0 || strcmp(arg, "--help") == 0) {
print_usage_and_exit(argv[0], false); print_usage_and_exit(false);
} else if (strcmp(arg, "-all") == 0) { } else if (strcmp(arg, "-all") == 0) {
mountVisitor.mountAll = true; mountVisitor.mountAll = true;
} else if (strcmp(arg, "-allbfs") == 0) { } else if (strcmp(arg, "-allbfs") == 0) {
mountVisitor.mountAllBFS = true; mountVisitor.mountBFS = true;
} else if (strcmp(arg, "-allhfs") == 0) { } else if (strcmp(arg, "-allhfs") == 0) {
mountVisitor.mountAllHFS = true; mountVisitor.mountHFS = true;
} else if (strcmp(arg, "-alldos") == 0) { } else if (strcmp(arg, "-alldos") == 0) {
mountVisitor.mountAllDOS = true; mountVisitor.mountDOS = true;
} else if (strcmp(arg, "-ro") == 0) { } else if (strcmp(arg, "-ro") == 0) {
mountVisitor.readOnly = true; mountVisitor.readOnly = true;
} else if (strcmp(arg, "-unmount") == 0) { } else if (strcmp(arg, "-unmount") == 0) {
argi++; argi++;
if (argi >= argc) if (argi >= argc)
print_usage_and_exit(argv[0], true); print_usage_and_exit(true);
mountVisitor.toUnmount.insert(argv[argi]); mountVisitor.toUnmount.insert(argv[argi]);
} else if (strcmp(arg, "-p") == 0 || strcmp(arg, "-l") == 0) { } else if (strcmp(arg, "-p") == 0 || strcmp(arg, "-l") == 0) {
printPartitionsVisitor.listMountablePartitions = true; printPartitionsVisitor.listMountablePartitions = true;
@@ -236,7 +265,7 @@ main(int argc, char **argv)
|| strcmp(arg, "-publishdos") == 0) { || strcmp(arg, "-publishdos") == 0) {
// obsolete: ignore // obsolete: ignore
} else } else
print_usage_and_exit(argv[0], true); print_usage_and_exit(true);
} }
// get a disk device list // get a disk device list
@@ -256,12 +285,14 @@ main(int argc, char **argv)
for (StringSet::iterator it = mountVisitor.toMount.begin(); for (StringSet::iterator it = mountVisitor.toMount.begin();
it != mountVisitor.toMount.end(); it != mountVisitor.toMount.end();
it++) { it++) {
fprintf("Failed to mount volume `%s': Volume not found.\n", *it); fprintf(stderr, "Failed to mount volume `%s': Volume not found.\n",
(*it).c_str());
} }
for (StringSet::iterator it = mountVisitor.toUnmount.begin(); for (StringSet::iterator it = mountVisitor.toUnmount.begin();
it != mountVisitor.toUnmount.end(); it != mountVisitor.toUnmount.end();
it++) { it++) {
fprintf("Failed to unmount volume `%s': Volume not found.\n", *it); fprintf(stderr, "Failed to unmount volume `%s': Volume not found.\n",
(*it).c_str());
} }
} }
@@ -274,7 +305,20 @@ main(int argc, char **argv)
} }
// print information // print information
// if (//
if (listAllDevices) {
// TODO
}
if (printPartitionsVisitor.IsUsed()) {
puts("Volume File System Mounted At");
puts("----------------------------------------------------------");
if (printPartitionsVisitor.listAllPartitions)
deviceList.VisitEachPartition(&printPartitionsVisitor);
else
deviceList.VisitEachMountablePartition(&printPartitionsVisitor);
}
return 0; return 0;
} }