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 = SetPrettyName(fModule->pretty_name); 47 SetFlags(fModule->flags | B_DISK_SYSTEM_IS_FILE_SYSTEM); 48 Unload(); 49 return error; 50 } 51 52 53 // Identify 54 float 55 KFileSystem::Identify(KPartition *partition, void **cookie) 56 { 57 if (!partition || !cookie || !fModule || !fModule->identify_partition) 58 return -1; 59 int fd = -1; 60 if (partition->Open(O_RDONLY, &fd) != B_OK) 61 return -1; 62 float result = fModule->identify_partition(fd, partition->PartitionData(), 63 cookie); 64 close(fd); 65 return result; 66 } 67 68 69 // Scan 70 status_t 71 KFileSystem::Scan(KPartition *partition, void *cookie) 72 { 73 if (!partition || !fModule || !fModule->scan_partition) 74 return B_ERROR; 75 int fd = -1; 76 status_t result = partition->Open(O_RDONLY, &fd); 77 if (result != B_OK) 78 return result; 79 result = fModule->scan_partition(fd, partition->PartitionData(), cookie); 80 close(fd); 81 return result; 82 } 83 84 85 // FreeIdentifyCookie 86 void 87 KFileSystem::FreeIdentifyCookie(KPartition *partition, void *cookie) 88 { 89 if (!partition || !fModule || !fModule->free_identify_partition_cookie) 90 return; 91 fModule->free_identify_partition_cookie(partition->PartitionData(), cookie); 92 } 93 94 95 // FreeContentCookie 96 void 97 KFileSystem::FreeContentCookie(KPartition *partition) 98 { 99 if (!partition || !fModule || !fModule->free_partition_content_cookie) 100 return; 101 fModule->free_partition_content_cookie(partition->PartitionData()); 102 } 103 104 105 // Defragment 106 status_t 107 KFileSystem::Defragment(KPartition* partition, disk_job_id job) 108 { 109 // to be implemented 110 return B_ERROR; 111 } 112 113 114 // Repair 115 status_t 116 KFileSystem::Repair(KPartition* partition, bool checkOnly, disk_job_id job) 117 { 118 // to be implemented 119 return B_ERROR; 120 } 121 122 123 // Resize 124 status_t 125 KFileSystem::Resize(KPartition* partition, off_t size, disk_job_id job) 126 { 127 // to be implemented 128 return B_ERROR; 129 } 130 131 132 // Move 133 status_t 134 KFileSystem::Move(KPartition* partition, off_t offset, disk_job_id job) 135 { 136 // to be implemented 137 return B_ERROR; 138 } 139 140 141 // SetContentName 142 status_t 143 KFileSystem::SetContentName(KPartition* partition, const char* name, 144 disk_job_id job) 145 { 146 // check parameters 147 if (!partition || !fModule) 148 return B_BAD_VALUE; 149 if (!fModule->set_content_name) 150 return B_NOT_SUPPORTED; 151 152 // open partition device 153 int fd = -1; 154 status_t result = partition->Open(O_RDWR, &fd); 155 if (result != B_OK) 156 return result; 157 158 // let the module do its job 159 result = fModule->set_content_name(fd, partition->ID(), name, job); 160 161 // cleanup and return 162 close(fd); 163 return result; 164 } 165 166 167 // SetContentParameters 168 status_t 169 KFileSystem::SetContentParameters(KPartition* partition, 170 const char* parameters, disk_job_id job) 171 { 172 // check parameters 173 if (!partition || !fModule) 174 return B_BAD_VALUE; 175 if (!fModule->set_content_parameters) 176 return B_NOT_SUPPORTED; 177 178 // open partition device 179 int fd = -1; 180 status_t result = partition->Open(O_RDWR, &fd); 181 if (result != B_OK) 182 return result; 183 184 // let the module do its job 185 result = fModule->set_content_parameters(fd, partition->ID(), parameters, 186 job); 187 188 // cleanup and return 189 close(fd); 190 return result; 191 } 192 193 194 // Initialize 195 status_t 196 KFileSystem::Initialize(KPartition* partition, const char* name, 197 const char* parameters, disk_job_id job) 198 { 199 // check parameters 200 if (!partition || !fModule) 201 return B_BAD_VALUE; 202 if (!fModule->initialize) 203 return B_NOT_SUPPORTED; 204 205 // open partition device 206 int fd = -1; 207 status_t result = partition->Open(O_RDWR, &fd); 208 if (result != B_OK) 209 return result; 210 211 // let the module do its job 212 result = fModule->initialize(fd, partition->ID(), name, parameters, 213 partition->Size(), job); 214 215 // cleanup and return 216 close(fd); 217 return result; 218 } 219 220 221 // LoadModule 222 status_t 223 KFileSystem::LoadModule() 224 { 225 if (fModule) // shouldn't happen 226 return B_OK; 227 228 return get_module(Name(), (module_info **)&fModule); 229 } 230 231 232 // UnloadModule 233 void 234 KFileSystem::UnloadModule() 235 { 236 if (fModule) { 237 put_module(fModule->info.name); 238 fModule = NULL; 239 } 240 } 241 242