xref: /haiku/src/bin/mkfs/FsCreator.cpp (revision b30304acc8c37e678a1bf66976d15bdab103f931)
1 /*
2  * Copyright 2008 Haiku Inc. All rights reserved.
3  * Distributed under the terms of the MIT License.
4  *
5  * Authors:
6  *		Marco Minutoli, mminutoli@gmail.com
7  */
8 
9 #include "FsCreator.h"
10 
11 #include <iostream>
12 
13 #include <DiskSystem.h>
14 
15 
16 FsCreator::FsCreator(const char* devPath, BString& type,
17 	BString& volumeName, const char* fsOpt, bool verbose)
18 	:
19 	fType(type),
20 	fDevicePath(devPath),
21 	fVolumeName(volumeName),
22 	fFsOptions(fsOpt),
23 	fVerbose(verbose)
24 {
25 	BDiskDeviceRoster roster;
26 	status_t ret = roster.GetDeviceForPath(devPath, &fDevice);
27 	if (ret != B_OK) {
28 		std::cerr << "Error: Failed to get disk device for path "
29 				  << devPath << ": " << strerror(ret);
30 	}
31 }
32 
33 
34 bool
35 FsCreator::Run()
36 {
37 	// check that the device is writable
38 	if (fDevice.IsReadOnly()) {
39 		std::cerr << "Error: Can't Inizialize the device. "
40 			"It is read only.\n";
41 		return false;
42 	}
43 
44 	// check if the device is mounted
45 	if (fDevice.IsMounted()) {
46 		std::cerr << "Error: The device has to be unmounted before.\n";
47 		return false;
48 	}
49 
50 	BDiskSystem diskSystem;
51 	BDiskDeviceRoster dDRoster;
52 	bool found = false;
53 	while (dDRoster.GetNextDiskSystem(&diskSystem) == B_OK) {
54 		if (diskSystem.IsFileSystem() && diskSystem.SupportsInitializing()) {
55 			if (diskSystem.ShortName() == fType) {
56 				found = true;
57 				break;
58 			}
59 		}
60 	}
61 
62 	if (!found) {
63 		std::cerr << "Error: " << fType.String()
64 				  << " is an invalid or unsupported file system type.\n";
65 		return false;
66 	}
67 
68 	// prepare the device for modifications
69 	status_t ret = fDevice.PrepareModifications();
70 	if (ret != B_OK) {
71 		std::cerr << "Error: A problem occurred preparing the device for the"
72 			"modifications\n";
73 		return false;
74 	}
75 	if (fVerbose)
76 		std::cout << "Preparing for modifications...\n\n";
77 
78 	// validate parameters
79 	BString name(fVolumeName);
80 	if (fDevice.ValidateInitialize(diskSystem.PrettyName(),
81 			&fVolumeName, fFsOptions) != B_OK) {
82 		std::cerr << "Error: Parameters validation failed. "
83 			"Check what you wrote\n";
84 		std::cerr << ret;
85 		return false;
86 	}
87 	if (fVerbose)
88 		std::cout << "Parameters Validation...\n\n";
89 	if (name != fVolumeName)
90 		std::cout << "Volume name was adjusted to "
91 				  << fVolumeName.String() << std::endl;
92 
93 	// Initialize the partition
94 	ret = fDevice.Initialize(diskSystem.PrettyName(),
95 		fVolumeName.String(), fFsOptions);
96 	if (ret != B_OK) {
97 		std::cerr << "Initialization failed: " <<  strerror(ret) << std::endl;
98 		return false;
99 	}
100 
101 	std::cout << "\nAre you sure you want to do this now?\n"
102 			  << "\nALL YOUR DATA in " << fDevicePath.String()
103 			  << " will be lost forever.\n";
104 
105 	BString reply;
106 	do {
107 		std::cout << "Continue? [yes|no]: ";
108 		reply = _ReadLine();
109 		if (reply == "")
110 			reply = "no"; // silence is dissence
111 	} while (reply != "yes" && reply != "no");
112 
113 	if (reply == "yes") {
114 		ret = fDevice.CommitModifications();
115 		if (ret == B_OK) {
116 			if (fVerbose) {
117 				std::cout << "Volume " << fDevice.ContentName()
118 						  << " has been initialized successfully!\n";
119 			}
120 		} else {
121 			std::cout << "Error: Initialization of " << fDevice.ContentName()
122 					  << " failed: " << strerror(ret) << std::endl;
123 			return false;
124 		}
125 	}
126 	return true;
127 }
128 
129 
130 inline BString
131 FsCreator::_ReadLine()
132 {
133 	char line[255];
134 
135 	std::cin.getline(line, sizeof(line), '\n');
136 
137 	return line;
138 }
139