1 /* 2 * Copyright 2003-2008, Haiku, Inc. All Rights Reserved. 3 * Distributed under the terms of the MIT License. 4 * 5 * Authors: 6 * Ingo Weinhold, bonefish@cs.tu-berlin.de 7 */ 8 9 #include "KFileSystem.h" 10 11 #include <fcntl.h> 12 #include <stdlib.h> 13 #include <unistd.h> 14 15 #include <fs_interface.h> 16 17 #include "ddm_modules.h" 18 #include "KDiskDeviceUtils.h" 19 #include "KPartition.h" 20 21 22 // constructor 23 KFileSystem::KFileSystem(const char *name) 24 : KDiskSystem(name), 25 fModule(NULL) 26 { 27 } 28 29 30 // destructor 31 KFileSystem::~KFileSystem() 32 { 33 } 34 35 36 // Init 37 status_t 38 KFileSystem::Init() 39 { 40 status_t error = KDiskSystem::Init(); 41 if (error != B_OK) 42 return error; 43 error = Load(); 44 if (error != B_OK) 45 return error; 46 error = SetShortName(fModule->short_name); 47 if (error == B_OK) 48 error = SetPrettyName(fModule->pretty_name); 49 50 SetFlags(fModule->flags | B_DISK_SYSTEM_IS_FILE_SYSTEM); 51 Unload(); 52 return error; 53 } 54 55 56 // Identify 57 float 58 KFileSystem::Identify(KPartition *partition, void **cookie) 59 { 60 if (!partition || !cookie || !fModule || !fModule->identify_partition) 61 return -1; 62 int fd = -1; 63 if (partition->Open(O_RDONLY, &fd) != B_OK) 64 return -1; 65 float result = fModule->identify_partition(fd, partition->PartitionData(), 66 cookie); 67 close(fd); 68 return result; 69 } 70 71 72 // Scan 73 status_t 74 KFileSystem::Scan(KPartition *partition, void *cookie) 75 { 76 if (!partition || !fModule || !fModule->scan_partition) 77 return B_ERROR; 78 int fd = -1; 79 status_t result = partition->Open(O_RDONLY, &fd); 80 if (result != B_OK) 81 return result; 82 result = fModule->scan_partition(fd, partition->PartitionData(), cookie); 83 close(fd); 84 return result; 85 } 86 87 88 // FreeIdentifyCookie 89 void 90 KFileSystem::FreeIdentifyCookie(KPartition *partition, void *cookie) 91 { 92 if (!partition || !fModule || !fModule->free_identify_partition_cookie) 93 return; 94 fModule->free_identify_partition_cookie(partition->PartitionData(), cookie); 95 } 96 97 98 // FreeContentCookie 99 void 100 KFileSystem::FreeContentCookie(KPartition *partition) 101 { 102 if (!partition || !fModule || !fModule->free_partition_content_cookie) 103 return; 104 fModule->free_partition_content_cookie(partition->PartitionData()); 105 } 106 107 108 // Defragment 109 status_t 110 KFileSystem::Defragment(KPartition* partition, disk_job_id job) 111 { 112 // to be implemented 113 return B_ERROR; 114 } 115 116 117 // Repair 118 status_t 119 KFileSystem::Repair(KPartition* partition, bool checkOnly, disk_job_id job) 120 { 121 // to be implemented 122 return B_ERROR; 123 } 124 125 126 // Resize 127 status_t 128 KFileSystem::Resize(KPartition* partition, off_t size, disk_job_id job) 129 { 130 // to be implemented 131 return B_ERROR; 132 } 133 134 135 // Move 136 status_t 137 KFileSystem::Move(KPartition* partition, off_t offset, disk_job_id job) 138 { 139 // to be implemented 140 return B_ERROR; 141 } 142 143 144 // SetContentName 145 status_t 146 KFileSystem::SetContentName(KPartition* partition, const char* name, 147 disk_job_id job) 148 { 149 // check parameters 150 if (!partition || !fModule) 151 return B_BAD_VALUE; 152 if (!fModule->set_content_name) 153 return B_NOT_SUPPORTED; 154 155 // open partition device 156 int fd = -1; 157 status_t result = partition->Open(O_RDWR, &fd); 158 if (result != B_OK) 159 return result; 160 161 // let the module do its job 162 result = fModule->set_content_name(fd, partition->ID(), name, job); 163 164 // cleanup and return 165 close(fd); 166 return result; 167 } 168 169 170 // SetContentParameters 171 status_t 172 KFileSystem::SetContentParameters(KPartition* partition, 173 const char* parameters, disk_job_id job) 174 { 175 // check parameters 176 if (!partition || !fModule) 177 return B_BAD_VALUE; 178 if (!fModule->set_content_parameters) 179 return B_NOT_SUPPORTED; 180 181 // open partition device 182 int fd = -1; 183 status_t result = partition->Open(O_RDWR, &fd); 184 if (result != B_OK) 185 return result; 186 187 // let the module do its job 188 result = fModule->set_content_parameters(fd, partition->ID(), parameters, 189 job); 190 191 // cleanup and return 192 close(fd); 193 return result; 194 } 195 196 197 // Initialize 198 status_t 199 KFileSystem::Initialize(KPartition* partition, const char* name, 200 const char* parameters, disk_job_id job) 201 { 202 // check parameters 203 if (!partition || !fModule) 204 return B_BAD_VALUE; 205 if (!fModule->initialize) 206 return B_NOT_SUPPORTED; 207 208 // open partition device 209 int fd = -1; 210 status_t result = partition->Open(O_RDWR, &fd); 211 if (result != B_OK) 212 return result; 213 214 // let the module do its job 215 result = fModule->initialize(fd, partition->ID(), name, parameters, 216 partition->Size(), job); 217 218 // cleanup and return 219 close(fd); 220 return result; 221 } 222 223 224 // LoadModule 225 status_t 226 KFileSystem::LoadModule() 227 { 228 if (fModule) // shouldn't happen 229 return B_OK; 230 231 return get_module(Name(), (module_info **)&fModule); 232 } 233 234 235 // UnloadModule 236 void 237 KFileSystem::UnloadModule() 238 { 239 if (fModule) { 240 put_module(fModule->info.name); 241 fModule = NULL; 242 } 243 } 244 245