1 // KFileDiskDevice.cpp 2 3 #include <errno.h> 4 #include <stdio.h> 5 #include <stdlib.h> 6 #include <unistd.h> 7 8 #include <KernelExport.h> 9 #include <Drivers.h> 10 #include <devfs.h> 11 12 #include <KDiskDeviceUtils.h> 13 #include <KFileDiskDevice.h> 14 #include <KPath.h> 15 16 17 // debugging 18 //#define DBG(x) 19 #define DBG(x) x 20 #define OUT dprintf 21 22 static const char *kFileDevicesDir = "/dev/disk/virtual/files"; 23 24 25 // constructor 26 KFileDiskDevice::KFileDiskDevice(partition_id id) 27 : KDiskDevice(id), 28 fFilePath(NULL) 29 { 30 SetDeviceFlags(DeviceFlags() | B_DISK_DEVICE_IS_FILE); 31 } 32 33 // destructor 34 KFileDiskDevice::~KFileDiskDevice() 35 { 36 Unset(); 37 } 38 39 // SetTo 40 status_t 41 KFileDiskDevice::SetTo(const char *filePath, const char *devicePath) 42 { 43 // check params 44 if (!filePath || strlen(filePath) > B_PATH_NAME_LENGTH 45 || (devicePath && strlen(devicePath) > B_PATH_NAME_LENGTH)) { 46 return B_BAD_VALUE; 47 } 48 // normalize the file path 49 // (should actually not be necessary, since this method is only invoked 50 // by the DDM, which has already normalized the path) 51 KPath tmpFilePath; 52 status_t error = tmpFilePath.SetTo(filePath, true); 53 if (error != B_OK) 54 return error; 55 // check the file 56 struct stat st; 57 if (stat(filePath, &st) != 0) 58 return errno; 59 if (!S_ISREG(st.st_mode)) 60 return B_BAD_VALUE; 61 // create the device, if requested 62 KPath tmpDevicePath; 63 if (!devicePath) { 64 // no device path: we shall create a new device entry 65 if (tmpDevicePath.InitCheck() != B_OK) 66 return tmpDevicePath.InitCheck(); 67 // TODO: Cleanup. The directory creation is done automatically by the devfs. 68 // // make the file devices dir 69 // if (mkdir(kFileDevicesDir, 0777) != 0) { 70 // if (errno != B_FILE_EXISTS) 71 // return errno; 72 // } 73 // make the directory 74 status_t error = _GetDirectoryPath(ID(), &tmpDevicePath); 75 if (error != B_OK) 76 return error; 77 // if (mkdir(tmpDevicePath.Path(), 0777) != 0) 78 // return errno; 79 // get the device path name 80 error = tmpDevicePath.Append("raw"); 81 if (error != B_OK) 82 return error; 83 devicePath = tmpDevicePath.Path(); 84 // register the file as virtual disk device 85 error = _RegisterDevice(filePath, devicePath); 86 if (error != B_OK) 87 return error; 88 } 89 error = set_string(fFilePath, filePath); 90 if (error != B_OK) 91 return error; 92 return KDiskDevice::SetTo(devicePath); 93 } 94 95 // Unset 96 void 97 KFileDiskDevice::Unset() 98 { 99 // remove the device and the directory it resides in 100 if (Path() && ID() >= 0) { 101 _UnregisterDevice(Path()); 102 // TODO: Cleanup. The devfs will automatically remove the directory. 103 // KPath dirPath; 104 // if (_GetDirectoryPath(ID(), &dirPath) == B_OK) 105 // rmdir(dirPath.Path()); 106 } 107 // free file path 108 free(fFilePath); 109 fFilePath = NULL; 110 } 111 112 // InitCheck 113 status_t 114 KFileDiskDevice::InitCheck() const 115 { 116 return KDiskDevice::InitCheck(); 117 } 118 119 // FilePath 120 const char * 121 KFileDiskDevice::FilePath() const 122 { 123 return fFilePath; 124 } 125 126 // GetMediaStatus 127 status_t 128 KFileDiskDevice::GetMediaStatus(status_t *mediaStatus) 129 { 130 // check the file 131 struct stat st; 132 if (stat(fFilePath, &st) == 0 && S_ISREG(st.st_mode)) 133 *mediaStatus = B_OK; 134 else 135 *mediaStatus = B_DEV_NO_MEDIA; 136 return B_OK; 137 } 138 139 // GetGeometry 140 status_t 141 KFileDiskDevice::GetGeometry(device_geometry *geometry) 142 { 143 // check the file 144 struct stat st; 145 if (stat(fFilePath, &st) != 0 || !S_ISREG(st.st_mode)) 146 return B_BAD_VALUE; 147 148 // fill in the geometry 149 // default to 512 bytes block size 150 uint32 blockSize = 512; 151 // Optimally we have only 1 block per sector and only one head. 152 // Since we have only a uint32 for the cylinder count, this won't work 153 // for files > 2TB. So, we set the head count to the minimally possible 154 // value. 155 off_t blocks = st.st_size / blockSize; 156 uint32 heads = (blocks + ULONG_MAX - 1) / ULONG_MAX; 157 if (heads == 0) 158 heads = 1; 159 geometry->bytes_per_sector = blockSize; 160 geometry->sectors_per_track = 1; 161 geometry->cylinder_count = blocks / heads; 162 geometry->head_count = heads; 163 geometry->device_type = B_DISK; // TODO: Add a new constant. 164 geometry->removable = false; 165 geometry->read_only = false; 166 geometry->write_once = false; 167 168 return B_OK; 169 } 170 171 // _RegisterDevice 172 status_t 173 KFileDiskDevice::_RegisterDevice(const char *file, const char *device) 174 { 175 return devfs_publish_file_device(device + 5, file); 176 // we need to remove the "/dev/" part from the path 177 } 178 179 // _UnregisterDevice 180 status_t 181 KFileDiskDevice::_UnregisterDevice(const char *_device) 182 { 183 return devfs_unpublish_file_device(_device + 5); 184 // we need to remove the "/dev/" part from the path 185 } 186 187 // _GetDirectoryPath 188 status_t 189 KFileDiskDevice::_GetDirectoryPath(partition_id id, KPath *path) 190 { 191 if (path == NULL) 192 return B_BAD_VALUE; 193 194 if (path->InitCheck() != B_OK) 195 return path->InitCheck(); 196 197 status_t error = path->SetPath(kFileDevicesDir); 198 if (error == B_OK) { 199 char idBuffer[12]; 200 sprintf(idBuffer, "%ld", id); 201 error = path->Append(idBuffer); 202 } 203 return error; 204 } 205 206