2 * Copyright 2008-2009 Haiku Inc. All rights reserved.
3 * Distributed under the terms of the MIT License.
6 * Marco Minutoli, mminutoli@gmail.com
7 * Axel Dörfler, axeld@pinc-software.de
10 #include "FsCreator.h"
14 #include <DiskSystem.h>
17 class UnregisterFileDevice
{
19 UnregisterFileDevice()
25 ~UnregisterFileDevice()
28 BDiskDeviceRoster roster
;
29 roster
.UnregisterFileDevice(fID
);
33 void SetTo(partition_id id
)
48 extern "C" const char* __progname
;
49 static const char* kProgramName
= __progname
;
52 FsCreator::FsCreator(const char* path
, const char* type
, const char* volumeName
,
53 const char* fsOptions
, bool quick
, bool verbose
)
57 fVolumeName(volumeName
),
58 fFsOptions(fsOptions
),
68 UnregisterFileDevice unregisterFileDevice
;
70 BDiskDeviceRoster roster
;
71 BPartition
* partition
;
74 status_t status
= roster
.GetPartitionForPath(fPath
, &device
,
77 if (!strncmp(fPath
, "/dev", 4)) {
78 std::cerr
<< kProgramName
<< ": Failed to get disk device for path "
79 << fPath
<< ": " << strerror(status
) << std::endl
;
83 // try to register file device
85 partition_id id
= roster
.RegisterFileDevice(fPath
);
87 std::cerr
<< kProgramName
<< ": Could not register file device for "
88 "path " << fPath
<< ": " << strerror(status
) << std::endl
;
92 unregisterFileDevice
.SetTo(id
);
94 status
= roster
.GetPartitionWithID(id
, &device
, &partition
);
95 if (!strncmp(fPath
, "/dev", 4)) {
96 std::cerr
<< kProgramName
<< ": Cannot find registered file device "
97 "for path " << fPath
<< ": " << strerror(status
)
103 // check that the device is writable
104 if (partition
->IsReadOnly()) {
105 std::cerr
<< kProgramName
<< ": Cannot initialize read-only device.\n";
109 // check if the device is mounted
110 if (partition
->IsMounted()) {
111 std::cerr
<< kProgramName
<< ": Cannot initialize mounted device.\n";
115 BDiskSystem diskSystem
;
116 if (roster
.GetDiskSystem(&diskSystem
, fType
) != B_OK
) {
117 std::cerr
<< kProgramName
<< ": " << fType
118 << " is an invalid or unsupported file system type.\n";
122 // prepare the device for modifications
123 status
= device
.PrepareModifications();
124 if (status
!= B_OK
) {
125 std::cerr
<< kProgramName
<< ": A problem occurred preparing the "
126 "device for the modifications\n";
130 std::cout
<< "Preparing for modifications...\n\n";
132 // validate parameters
133 BString
name(fVolumeName
);
134 if (partition
->ValidateInitialize(diskSystem
.PrettyName(),
135 &name
, fFsOptions
) != B_OK
) {
136 std::cerr
<< kProgramName
<< ": Parameters validation failed. "
137 "Check what you wrote\n";
142 std::cout
<< "Parameters Validation...\n\n";
143 if (name
!= fVolumeName
) {
144 std::cout
<< "Volume name was adjusted to "
145 << name
.String() << std::endl
;
148 // Initialize the partition
149 status
= partition
->Initialize(diskSystem
.PrettyName(), name
.String(),
151 if (status
!= B_OK
) {
152 std::cerr
<< kProgramName
<< ": Initialization failed: "
153 << strerror(status
) << std::endl
;
158 std::cout
<< "\nAbout to initialize " << fPath
<< " with "
159 << diskSystem
.PrettyName()
160 << "\nAre you sure you want to do this now?\n"
161 << "\nALL YOUR DATA in " << fPath
<< " will be lost forever.\n";
165 std::cout
<< "Continue (yes|[no])? ";
168 reply
= "no"; // silence is dissence
169 } while (reply
!= "yes" && reply
!= "no");
175 BString contentName
= partition
->ContentName();
176 // CommitModifications() will invalidate our partition object
178 status
= device
.CommitModifications();
179 if (status
== B_OK
) {
181 std::cout
<< "Volume \"" << contentName
.String()
182 << "\" has been initialized successfully!" << std::endl
;
185 std::cout
<< kProgramName
<< ": Initialization of \""
186 << contentName
.String() << "\" failed: " << strerror(status
)
191 // TODO: should we keep the file device around, or unregister it
192 // after we're done? This could be an option, too (for now, we'll
193 // just keep them if everything went well).
194 unregisterFileDevice
.Detach();
200 FsCreator::_ReadLine()
204 std::cin
.getline(line
, sizeof(line
), '\n');