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