| 1 | /**************************************************************************** |
|---|
| 2 | |
|---|
| 3 | Copyright 2004, 2007 Virginia Polytechnic Institute and State University |
|---|
| 4 | |
|---|
| 5 | This file is part of the OSSIE Core Framework. |
|---|
| 6 | |
|---|
| 7 | OSSIE Core Framework is free software; you can redistribute it and/or modify |
|---|
| 8 | it under the terms of the Lesser GNU General Public License as published by |
|---|
| 9 | the Free Software Foundation; either version 2.1 of the License, or |
|---|
| 10 | (at your option) any later version. |
|---|
| 11 | |
|---|
| 12 | OSSIE Core Framework is distributed in the hope that it will be useful, |
|---|
| 13 | but WITHOUT ANY WARRANTY; without even the implied warranty of |
|---|
| 14 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
|---|
| 15 | Lesser GNU General Public License for more details. |
|---|
| 16 | |
|---|
| 17 | You should have received a copy of the Lesser GNU General Public License |
|---|
| 18 | along with OSSIE Core Framework; if not, write to the Free Software |
|---|
| 19 | Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA |
|---|
| 20 | |
|---|
| 21 | ****************************************************************************/ |
|---|
| 22 | |
|---|
| 23 | #include <iostream> |
|---|
| 24 | #include <string> |
|---|
| 25 | |
|---|
| 26 | #include "ossie/cf.h" |
|---|
| 27 | #include "ossie/FileManager_impl.h" |
|---|
| 28 | #include "ossie/debug.h" |
|---|
| 29 | |
|---|
| 30 | FileManager_impl::FileManager_impl ():FileSystem_impl () |
|---|
| 31 | { |
|---|
| 32 | DEBUG(4, FileManager, "Entering constructor.") |
|---|
| 33 | numMounts = 0; |
|---|
| 34 | mount_table = new CF::FileManager::MountSequence(5); |
|---|
| 35 | |
|---|
| 36 | rootFileSys_serv = new FileSystem_impl (); |
|---|
| 37 | rootFileSys = rootFileSys_serv->_this(); |
|---|
| 38 | |
|---|
| 39 | mount ("/", rootFileSys); |
|---|
| 40 | |
|---|
| 41 | DEBUG(4, FileManager, "Leaving constructor.") |
|---|
| 42 | } |
|---|
| 43 | |
|---|
| 44 | FileManager_impl::~FileManager_impl() |
|---|
| 45 | |
|---|
| 46 | { |
|---|
| 47 | DEBUG(4, FileManager, "In destructor.") |
|---|
| 48 | } |
|---|
| 49 | |
|---|
| 50 | void |
|---|
| 51 | FileManager_impl::mount (const char *mountPoint, |
|---|
| 52 | CF::FileSystem_ptr _fileSystem) |
|---|
| 53 | throw (CORBA::SystemException, CF::InvalidFileName, |
|---|
| 54 | CF::FileManager::InvalidFileSystem, |
|---|
| 55 | CF::FileManager::MountPointAlreadyExists) |
|---|
| 56 | { |
|---|
| 57 | DEBUG(4, FileManager, "Entering mount with " << mountPoint) |
|---|
| 58 | |
|---|
| 59 | if (CORBA::is_nil (_fileSystem)) |
|---|
| 60 | throw CF::FileManager::InvalidFileSystem (); |
|---|
| 61 | |
|---|
| 62 | CF::FileManager::MountType _mt; |
|---|
| 63 | |
|---|
| 64 | for (unsigned int i=0; i < mount_table->length(); i++) { |
|---|
| 65 | if (strcmp(mountPoint, mount_table[i].mountPoint) == 0) |
|---|
| 66 | throw CF::FileManager::MountPointAlreadyExists (); |
|---|
| 67 | } |
|---|
| 68 | |
|---|
| 69 | numMounts++; |
|---|
| 70 | mount_table->length(numMounts); |
|---|
| 71 | |
|---|
| 72 | mount_table[numMounts-1].mountPoint = CORBA::string_dup(mountPoint); |
|---|
| 73 | mount_table[numMounts-1].fs = CF::FileSystem::_duplicate(_fileSystem); |
|---|
| 74 | |
|---|
| 75 | |
|---|
| 76 | DEBUG(4, FileManager, "Leaving mount.") |
|---|
| 77 | } |
|---|
| 78 | |
|---|
| 79 | |
|---|
| 80 | void |
|---|
| 81 | FileManager_impl::unmount (const char *mountPoint) |
|---|
| 82 | throw (CORBA::SystemException, CF::FileManager::NonExistentMount) |
|---|
| 83 | { |
|---|
| 84 | DEBUG(4, FileManager, "Entering unmount with " << mountPoint) |
|---|
| 85 | |
|---|
| 86 | for (unsigned int i = 0; i < mount_table->length(); i++) |
|---|
| 87 | { |
|---|
| 88 | if (strcmp (mount_table[i].mountPoint, mountPoint) == 0) { |
|---|
| 89 | DEBUG(4, FileManager, "Found mount point to delete.") |
|---|
| 90 | for (unsigned int j = i; j < mount_table->length(); ++j) ///\todo this leaks FileSystems etc (check) |
|---|
| 91 | mount_table[j] = mount_table[j+1]; |
|---|
| 92 | |
|---|
| 93 | mount_table->length(mount_table->length() - 1); |
|---|
| 94 | DEBUG(4, FileManager, "Leaving unmount.") |
|---|
| 95 | return; |
|---|
| 96 | } |
|---|
| 97 | } |
|---|
| 98 | |
|---|
| 99 | std::cerr << "Throwing CF::FileManager::NonExistentMount from FileManger unmount." << std::endl; |
|---|
| 100 | throw CF::FileManager::NonExistentMount (); |
|---|
| 101 | } |
|---|
| 102 | |
|---|
| 103 | |
|---|
| 104 | void |
|---|
| 105 | FileManager_impl::remove (const char *fileName) |
|---|
| 106 | throw (CORBA::SystemException, CF::FileException, CF::InvalidFileName) |
|---|
| 107 | { |
|---|
| 108 | DEBUG(4, FileManager, "Entering remove with " << fileName) |
|---|
| 109 | |
|---|
| 110 | unsigned int FSIndex(0); |
|---|
| 111 | std::string FSPath; |
|---|
| 112 | |
|---|
| 113 | getFSandFSPath(fileName, FSIndex, FSPath); |
|---|
| 114 | |
|---|
| 115 | if (mount_table[FSIndex].fs->exists (FSPath.c_str())) |
|---|
| 116 | mount_table[FSIndex].fs->remove (FSPath.c_str()); |
|---|
| 117 | |
|---|
| 118 | DEBUG(4, FileManager, "Leaving remove.") |
|---|
| 119 | } |
|---|
| 120 | |
|---|
| 121 | |
|---|
| 122 | void |
|---|
| 123 | FileManager_impl::copy (const char *sourceFileName, |
|---|
| 124 | const char *destinationFileName) |
|---|
| 125 | throw (CORBA::SystemException, CF::InvalidFileName, CF::FileException) |
|---|
| 126 | { |
|---|
| 127 | DEBUG(4, FileManager, "Entering copy with " << sourceFileName << " to " << destinationFileName) |
|---|
| 128 | |
|---|
| 129 | unsigned int srcFS(0), dstFS(0); |
|---|
| 130 | std::string srcPath; |
|---|
| 131 | std::string dstPath; |
|---|
| 132 | |
|---|
| 133 | getFSandFSPath(sourceFileName, srcFS, srcPath); |
|---|
| 134 | getFSandFSPath(destinationFileName, dstFS, dstPath); |
|---|
| 135 | |
|---|
| 136 | |
|---|
| 137 | if (!mount_table[srcFS].fs->exists (sourceFileName)) |
|---|
| 138 | throw CF::InvalidFileName (); |
|---|
| 139 | |
|---|
| 140 | if (srcFS == dstFS) { // Check if copy is within one FileSystem |
|---|
| 141 | mount_table[srcFS].fs->copy (srcPath.c_str(), dstPath.c_str()); |
|---|
| 142 | return; |
|---|
| 143 | } |
|---|
| 144 | |
|---|
| 145 | // Copy file across FileSystems |
|---|
| 146 | |
|---|
| 147 | CF::File_var srcFile = mount_table[srcFS].fs->open(srcPath.c_str(), true); |
|---|
| 148 | |
|---|
| 149 | unsigned int srcSize = srcFile->sizeOf(); |
|---|
| 150 | |
|---|
| 151 | if (srcSize == 0) { ///\todo Check spec to see why we throw if size == 0 |
|---|
| 152 | srcFile->close(); |
|---|
| 153 | throw CF::FileException (); |
|---|
| 154 | } |
|---|
| 155 | |
|---|
| 156 | if (!mount_table[dstFS].fs->exists(dstPath.c_str())) |
|---|
| 157 | mount_table[dstFS].fs->create(dstPath.c_str()); |
|---|
| 158 | |
|---|
| 159 | CF::File_var dstFile =mount_table[dstFS].fs->open (dstPath.c_str(), false); |
|---|
| 160 | |
|---|
| 161 | CF::OctetSequence_var data; |
|---|
| 162 | |
|---|
| 163 | srcFile->read (data, srcSize); |
|---|
| 164 | |
|---|
| 165 | dstFile->write (data); |
|---|
| 166 | |
|---|
| 167 | srcFile->close(); |
|---|
| 168 | dstFile->close(); |
|---|
| 169 | } |
|---|
| 170 | |
|---|
| 171 | |
|---|
| 172 | CORBA::Boolean FileManager_impl::exists (const char *fileName) |
|---|
| 173 | throw (CORBA::SystemException, CF::InvalidFileName) |
|---|
| 174 | { |
|---|
| 175 | DEBUG(4, FileManager, "Entering exists with " << fileName) |
|---|
| 176 | |
|---|
| 177 | unsigned int fileFS(0); |
|---|
| 178 | std::string filePath; |
|---|
| 179 | |
|---|
| 180 | getFSandFSPath(fileName, fileFS, filePath); |
|---|
| 181 | |
|---|
| 182 | DEBUG(6, FileManager, "Running FS.exists for FS " << mount_table[fileFS].mountPoint << " and path " << filePath) |
|---|
| 183 | if (mount_table[fileFS].fs->exists (filePath.c_str())) |
|---|
| 184 | return true; |
|---|
| 185 | else |
|---|
| 186 | return false; |
|---|
| 187 | } |
|---|
| 188 | |
|---|
| 189 | |
|---|
| 190 | CF::FileSystem::FileInformationSequence * |
|---|
| 191 | FileManager_impl::list (const char *pattern) throw (CORBA::SystemException, |
|---|
| 192 | CF::FileException, |
|---|
| 193 | CF::InvalidFileName) |
|---|
| 194 | { |
|---|
| 195 | DEBUG(4, FileManager, "Entering list with " << pattern) |
|---|
| 196 | |
|---|
| 197 | unsigned int fileFS(0); |
|---|
| 198 | std::string filePath; |
|---|
| 199 | |
|---|
| 200 | getFSandFSPath(pattern, fileFS, filePath); |
|---|
| 201 | |
|---|
| 202 | CF::FileSystem::FileInformationSequence_var result = mount_table[fileFS].fs->list (filePath.c_str()); |
|---|
| 203 | |
|---|
| 204 | return result._retn(); |
|---|
| 205 | } |
|---|
| 206 | |
|---|
| 207 | |
|---|
| 208 | CF::File_ptr FileManager_impl::create (const char *fileName) throw (CORBA:: |
|---|
| 209 | SystemException, |
|---|
| 210 | CF:: |
|---|
| 211 | InvalidFileName, |
|---|
| 212 | CF:: |
|---|
| 213 | FileException) |
|---|
| 214 | { |
|---|
| 215 | DEBUG(4, FileManager, "Entering create with " << fileName) |
|---|
| 216 | |
|---|
| 217 | unsigned int fileFS(0); |
|---|
| 218 | std::string filePath; |
|---|
| 219 | |
|---|
| 220 | getFSandFSPath(fileName, fileFS, filePath); |
|---|
| 221 | |
|---|
| 222 | CF::File_var file_var = mount_table[fileFS].fs->create (filePath.c_str()); |
|---|
| 223 | |
|---|
| 224 | return file_var._retn(); |
|---|
| 225 | } |
|---|
| 226 | |
|---|
| 227 | |
|---|
| 228 | CF::File_ptr FileManager_impl::open (const char *fileName, |
|---|
| 229 | CORBA::Boolean read_Only) throw (CORBA:: |
|---|
| 230 | SystemException, |
|---|
| 231 | CF:: |
|---|
| 232 | InvalidFileName, |
|---|
| 233 | CF:: |
|---|
| 234 | FileException) |
|---|
| 235 | { |
|---|
| 236 | DEBUG(4, FileManager, "Entering open with " << fileName) |
|---|
| 237 | |
|---|
| 238 | unsigned int fileFS(0); |
|---|
| 239 | std::string filePath; |
|---|
| 240 | |
|---|
| 241 | getFSandFSPath(fileName, fileFS, filePath); |
|---|
| 242 | |
|---|
| 243 | CF::File_var file_var = mount_table[fileFS].fs->open (filePath.c_str(), read_Only); |
|---|
| 244 | |
|---|
| 245 | return file_var._retn(); |
|---|
| 246 | } |
|---|
| 247 | |
|---|
| 248 | |
|---|
| 249 | void |
|---|
| 250 | FileManager_impl::mkdir (const char *directoryName) |
|---|
| 251 | throw (CORBA::SystemException, CF::FileException, CF::InvalidFileName) |
|---|
| 252 | { |
|---|
| 253 | DEBUG(4, FileManager, "Entering mkdir with " << directoryName) |
|---|
| 254 | |
|---|
| 255 | unsigned int fileFS(0); |
|---|
| 256 | std::string filePath; |
|---|
| 257 | |
|---|
| 258 | getFSandFSPath(directoryName, fileFS, filePath); |
|---|
| 259 | |
|---|
| 260 | mount_table[fileFS].fs->mkdir(filePath.c_str()); |
|---|
| 261 | |
|---|
| 262 | DEBUG(4, FileManager, "Leaving mkdir.") |
|---|
| 263 | } |
|---|
| 264 | |
|---|
| 265 | |
|---|
| 266 | void |
|---|
| 267 | FileManager_impl::rmdir (const char *directoryName) |
|---|
| 268 | throw (CORBA::SystemException, CF::FileException, CF::InvalidFileName) |
|---|
| 269 | { |
|---|
| 270 | DEBUG(4, FileManager, "Entering rmdir with " << directoryName) |
|---|
| 271 | |
|---|
| 272 | unsigned int fileFS(0); |
|---|
| 273 | std::string filePath; |
|---|
| 274 | |
|---|
| 275 | getFSandFSPath(directoryName, fileFS, filePath); |
|---|
| 276 | |
|---|
| 277 | mount_table[fileFS].fs->rmdir (filePath.c_str()); |
|---|
| 278 | |
|---|
| 279 | DEBUG(4, FileManager, "Leaving rmdir.") |
|---|
| 280 | } |
|---|
| 281 | |
|---|
| 282 | |
|---|
| 283 | void |
|---|
| 284 | FileManager_impl::query (CF::Properties & fileSysProperties) |
|---|
| 285 | throw (CORBA::SystemException, CF::FileSystem::UnknownFileSystemProperties) |
|---|
| 286 | { |
|---|
| 287 | DEBUG(4, FileManager, "Entering query.") |
|---|
| 288 | |
|---|
| 289 | #if 0 |
|---|
| 290 | unsigned int fileFS(0); |
|---|
| 291 | char *filePath = new char[MAXPATHLEN]; |
|---|
| 292 | |
|---|
| 293 | getFSandFSPath(fileName, fileFS, filePath); |
|---|
| 294 | #endif |
|---|
| 295 | |
|---|
| 296 | bool check; |
|---|
| 297 | |
|---|
| 298 | for (unsigned int i = 0; i < fileSysProperties.length (); i++) |
|---|
| 299 | { |
|---|
| 300 | check = false; |
|---|
| 301 | |
|---|
| 302 | if (strcmp (fileSysProperties[i].id, CF::FileSystem::SIZE) == 0) |
|---|
| 303 | { |
|---|
| 304 | CORBA::Long totalSize, temp; |
|---|
| 305 | totalSize = 0; |
|---|
| 306 | |
|---|
| 307 | for (unsigned int j = 0; j < mount_table->length(); j++) |
|---|
| 308 | { |
|---|
| 309 | CF::DataType dt; |
|---|
| 310 | dt.id = CORBA::string_dup ("SIZE"); |
|---|
| 311 | CF::Properties pr (2, 1, &dt, 0); |
|---|
| 312 | |
|---|
| 313 | mount_table[j].fs->query (pr); |
|---|
| 314 | |
|---|
| 315 | CF::DataType * _dt = pr.get_buffer (); |
|---|
| 316 | |
|---|
| 317 | for (unsigned int k = 0; k < pr.length (); k++) |
|---|
| 318 | { |
|---|
| 319 | _dt->value >>= temp; |
|---|
| 320 | totalSize = totalSize + temp; |
|---|
| 321 | _dt++; |
|---|
| 322 | } |
|---|
| 323 | |
|---|
| 324 | fileSysProperties[i].value >>= temp; |
|---|
| 325 | fileSysProperties[i].value <<= totalSize + temp; |
|---|
| 326 | |
|---|
| 327 | check = true; |
|---|
| 328 | } |
|---|
| 329 | } |
|---|
| 330 | |
|---|
| 331 | if (strcmp (fileSysProperties[i].id, |
|---|
| 332 | CF::FileSystem::AVAILABLE_SIZE) == 0) |
|---|
| 333 | { |
|---|
| 334 | CORBA::Long totalSize; |
|---|
| 335 | totalSize = 0; |
|---|
| 336 | |
|---|
| 337 | for (unsigned int i = 0; i < mount_table->length(); i++) |
|---|
| 338 | { |
|---|
| 339 | } |
|---|
| 340 | |
|---|
| 341 | check = true; |
|---|
| 342 | } |
|---|
| 343 | |
|---|
| 344 | if (!check) |
|---|
| 345 | throw CF::FileSystem::UnknownFileSystemProperties (); |
|---|
| 346 | |
|---|
| 347 | ///\todo Add functionality to query ALL FileManager properties |
|---|
| 348 | } |
|---|
| 349 | DEBUG(4, FileManager, "Leaving query.") |
|---|
| 350 | } |
|---|
| 351 | |
|---|
| 352 | |
|---|
| 353 | CF::FileManager::MountSequence* |
|---|
| 354 | FileManager_impl::getMounts ()throw (CORBA::SystemException) |
|---|
| 355 | { |
|---|
| 356 | DEBUG(4, FileManager, "Entering MountSequence."); |
|---|
| 357 | CF::FileManager::MountSequence_var result = new CF::FileManager::MountSequence(mount_table); |
|---|
| 358 | return result._retn(); |
|---|
| 359 | } |
|---|
| 360 | |
|---|
| 361 | void FileManager_impl::getFSandFSPath(const char *path, unsigned int &mountTableIndex, std::string &FSPath) |
|---|
| 362 | { |
|---|
| 363 | DEBUG(5, FileManager, "Entering getFSandFSPath with path " << path) |
|---|
| 364 | |
|---|
| 365 | boost::filesystem::path fullPath(path); |
|---|
| 366 | boost::filesystem::path::iterator fullItr(fullPath.begin()); |
|---|
| 367 | std::string tmpFSPath; |
|---|
| 368 | |
|---|
| 369 | unsigned int lastMatchLength(0); |
|---|
| 370 | |
|---|
| 371 | for(unsigned int i(0); i < numMounts; ++i) { |
|---|
| 372 | unsigned int matchLength = pathMatches(path,mount_table[i].mountPoint, tmpFSPath); |
|---|
| 373 | if ( matchLength > lastMatchLength) { |
|---|
| 374 | mountTableIndex = i; |
|---|
| 375 | lastMatchLength = matchLength; |
|---|
| 376 | FSPath = tmpFSPath; |
|---|
| 377 | } |
|---|
| 378 | } |
|---|
| 379 | DEBUG(5, FileManager, "Found mountIndex " << mountTableIndex << " and local path " << FSPath) |
|---|
| 380 | |
|---|
| 381 | } |
|---|
| 382 | |
|---|
| 383 | unsigned int FileManager_impl::pathMatches(const char * path, const char *mPoint, std::string &FSPath) |
|---|
| 384 | { |
|---|
| 385 | DEBUG(5, FileManager, "Entering pathMatches with path " << path << " mount point " << mPoint) |
|---|
| 386 | |
|---|
| 387 | boost::filesystem::path fullPath(path); |
|---|
| 388 | boost::filesystem::path::iterator fullItr(fullPath.begin()); |
|---|
| 389 | |
|---|
| 390 | boost::filesystem::path mPath(mPoint); |
|---|
| 391 | boost::filesystem::path::iterator mItr(mPath.begin()); |
|---|
| 392 | |
|---|
| 393 | unsigned int commonElements(0); |
|---|
| 394 | |
|---|
| 395 | while (fullItr != fullPath.end()) { |
|---|
| 396 | if (*fullItr != *mItr) |
|---|
| 397 | break; |
|---|
| 398 | ++fullItr; |
|---|
| 399 | ++mItr; |
|---|
| 400 | ++commonElements; |
|---|
| 401 | } |
|---|
| 402 | |
|---|
| 403 | |
|---|
| 404 | boost::filesystem::path localPath("/"); |
|---|
| 405 | |
|---|
| 406 | while (fullItr != fullPath.end()) { |
|---|
| 407 | localPath /= *fullItr; |
|---|
| 408 | ++fullItr; |
|---|
| 409 | } |
|---|
| 410 | FSPath = localPath.string(); |
|---|
| 411 | |
|---|
| 412 | DEBUG(5, FileManager, "Path matches with " << commonElements << " path elements in common and remaining path " << FSPath) |
|---|
| 413 | |
|---|
| 414 | return commonElements; |
|---|
| 415 | } |
|---|