| 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 <boost/filesystem/path.hpp> |
|---|
| 27 | #include <boost/filesystem/exception.hpp> |
|---|
| 28 | #include <boost/filesystem/operations.hpp> |
|---|
| 29 | |
|---|
| 30 | #ifndef BOOST_VERSION |
|---|
| 31 | #include <boost/version.hpp> |
|---|
| 32 | #endif |
|---|
| 33 | |
|---|
| 34 | #if BOOST_VERSION < 103500 |
|---|
| 35 | # include <boost/filesystem/cerrno.hpp> |
|---|
| 36 | #else |
|---|
| 37 | # include <boost/cerrno.hpp> |
|---|
| 38 | #endif |
|---|
| 39 | |
|---|
| 40 | namespace fs = boost::filesystem; |
|---|
| 41 | |
|---|
| 42 | #include "ossie/cf.h" |
|---|
| 43 | #include "ossie/FileManager_impl.h" |
|---|
| 44 | #include "ossie/debug.h" |
|---|
| 45 | |
|---|
| 46 | FileManager_impl::FileManager_impl ():FileSystem_impl () |
|---|
| 47 | { |
|---|
| 48 | DEBUG(4, FileManager, "Entering constructor."); |
|---|
| 49 | |
|---|
| 50 | // this is legacy code that I'm no sure actually did anything anyway. |
|---|
| 51 | // commenting as the default name checking has been removed from boost. |
|---|
| 52 | //if (fs::path::default_name_check_writable()) |
|---|
| 53 | // fs::path::default_name_check(boost::filesystem::portable_posix_name); |
|---|
| 54 | |
|---|
| 55 | numMounts = 0; |
|---|
| 56 | mount_table = new CF::FileManager::MountSequence(5); |
|---|
| 57 | |
|---|
| 58 | DEBUG(4, FileManager, "Leaving constructor.") |
|---|
| 59 | } |
|---|
| 60 | |
|---|
| 61 | FileManager_impl::~FileManager_impl() |
|---|
| 62 | |
|---|
| 63 | { |
|---|
| 64 | DEBUG(4, FileManager, "In destructor.") |
|---|
| 65 | } |
|---|
| 66 | |
|---|
| 67 | void |
|---|
| 68 | FileManager_impl::mount (const char *mountPoint, |
|---|
| 69 | CF::FileSystem_ptr _fileSystem) |
|---|
| 70 | throw (CORBA::SystemException, CF::InvalidFileName, |
|---|
| 71 | CF::FileManager::InvalidFileSystem, |
|---|
| 72 | CF::FileManager::MountPointAlreadyExists) |
|---|
| 73 | { |
|---|
| 74 | DEBUG(4, FileManager, "Entering mount with " << mountPoint) |
|---|
| 75 | |
|---|
| 76 | if (CORBA::is_nil (_fileSystem)) |
|---|
| 77 | throw CF::FileManager::InvalidFileSystem (); |
|---|
| 78 | |
|---|
| 79 | CF::FileManager::MountType _mt; |
|---|
| 80 | |
|---|
| 81 | for (unsigned int i=0; i < mount_table->length(); i++) { |
|---|
| 82 | if (strcmp(mountPoint, mount_table[i].mountPoint) == 0) |
|---|
| 83 | throw CF::FileManager::MountPointAlreadyExists (); |
|---|
| 84 | } |
|---|
| 85 | |
|---|
| 86 | numMounts++; |
|---|
| 87 | mount_table->length(numMounts); |
|---|
| 88 | |
|---|
| 89 | mount_table[numMounts-1].mountPoint = CORBA::string_dup(mountPoint); |
|---|
| 90 | mount_table[numMounts-1].fs = CF::FileSystem::_duplicate(_fileSystem); |
|---|
| 91 | |
|---|
| 92 | |
|---|
| 93 | DEBUG(4, FileManager, "Leaving mount.") |
|---|
| 94 | } |
|---|
| 95 | |
|---|
| 96 | |
|---|
| 97 | void |
|---|
| 98 | FileManager_impl::unmount (const char *mountPoint) |
|---|
| 99 | throw (CORBA::SystemException, CF::FileManager::NonExistentMount) |
|---|
| 100 | { |
|---|
| 101 | DEBUG(4, FileManager, "Entering unmount with " << mountPoint) |
|---|
| 102 | |
|---|
| 103 | for (unsigned int i = 0; i < numMounts; i++) { |
|---|
| 104 | if (strcmp (mount_table[i].mountPoint, mountPoint) == 0) { |
|---|
| 105 | DEBUG(4, FileManager, "Found mount point to delete.") |
|---|
| 106 | for (unsigned int j = i; j < mount_table->length() - 1; ++j) ///\todo this leaks FileSystems etc (check) |
|---|
| 107 | mount_table[j] = mount_table[j+1]; |
|---|
| 108 | |
|---|
| 109 | mount_table->length(mount_table->length() - 1); |
|---|
| 110 | numMounts--; |
|---|
| 111 | DEBUG(4, FileManager, "Leaving unmount.") |
|---|
| 112 | return; |
|---|
| 113 | } |
|---|
| 114 | } |
|---|
| 115 | |
|---|
| 116 | std::cerr << "Throwing CF::FileManager::NonExistentMount from FileManger unmount." << std::endl; |
|---|
| 117 | throw CF::FileManager::NonExistentMount (); |
|---|
| 118 | } |
|---|
| 119 | |
|---|
| 120 | |
|---|
| 121 | void |
|---|
| 122 | FileManager_impl::remove (const char *fileName) |
|---|
| 123 | throw (CORBA::SystemException, CF::FileException, CF::InvalidFileName) |
|---|
| 124 | { |
|---|
| 125 | DEBUG(4, FileManager, "Entering remove with " << fileName) |
|---|
| 126 | |
|---|
| 127 | if (fileName[0] != '/' || !ossieSupport::isValidFileName(fileName)) { |
|---|
| 128 | DEBUG(7, FileManager, "remove passed bad filename, throwing exception."); |
|---|
| 129 | throw CF::InvalidFileName (CF::CFEINVAL, "[FileManager::remove] Invalid file name"); |
|---|
| 130 | } |
|---|
| 131 | |
|---|
| 132 | unsigned int FSIndex(0); |
|---|
| 133 | std::string FSPath; |
|---|
| 134 | |
|---|
| 135 | getFSandFSPath(fileName, FSIndex, FSPath); |
|---|
| 136 | |
|---|
| 137 | if (mount_table[FSIndex].fs->exists (FSPath.c_str())) |
|---|
| 138 | mount_table[FSIndex].fs->remove (FSPath.c_str()); |
|---|
| 139 | |
|---|
| 140 | DEBUG(4, FileManager, "Leaving remove.") |
|---|
| 141 | } |
|---|
| 142 | |
|---|
| 143 | |
|---|
| 144 | void |
|---|
| 145 | FileManager_impl::copy (const char *sourceFileName, |
|---|
| 146 | const char *destinationFileName) |
|---|
| 147 | throw (CORBA::SystemException, CF::InvalidFileName, CF::FileException) |
|---|
| 148 | { |
|---|
| 149 | DEBUG(4, FileManager, "Entering copy with " << sourceFileName << " to " << destinationFileName) |
|---|
| 150 | |
|---|
| 151 | if (!ossieSupport::isValidFileName(sourceFileName) || !ossieSupport::isValidFileName(destinationFileName)) { |
|---|
| 152 | DEBUG(7, FileManager, "copy passed bad filename, throwing exception."); |
|---|
| 153 | throw CF::InvalidFileName (CF::CFEINVAL, "[FileManager::copy] Invalid file name"); |
|---|
| 154 | } |
|---|
| 155 | |
|---|
| 156 | unsigned int srcFS(0), dstFS(0); |
|---|
| 157 | std::string srcPath; |
|---|
| 158 | std::string dstPath; |
|---|
| 159 | |
|---|
| 160 | getFSandFSPath(sourceFileName, srcFS, srcPath); |
|---|
| 161 | getFSandFSPath(destinationFileName, dstFS, dstPath); |
|---|
| 162 | |
|---|
| 163 | |
|---|
| 164 | if (!mount_table[srcFS].fs->exists (srcPath.c_str())) { |
|---|
| 165 | DEBUG(3, FileManager, "Throwing exception from copy because source file does not exist."); |
|---|
| 166 | throw CF::InvalidFileName (CF::CFEINVAL, "[FileSystem::Copy] Invalid file name"); |
|---|
| 167 | } |
|---|
| 168 | |
|---|
| 169 | if (srcFS == dstFS) { // Check if copy is within one FileSystem |
|---|
| 170 | try { |
|---|
| 171 | mount_table[srcFS].fs->copy (srcPath.c_str(), dstPath.c_str()); |
|---|
| 172 | } catch ( CF::FileException& ) { |
|---|
| 173 | std::cout << "[FileManager::copy] \"fs->copy\" " << srcPath << " to " << dstPath << " failed with CF::FileException\n"; |
|---|
| 174 | exit(EXIT_FAILURE); |
|---|
| 175 | } catch ( CF::InvalidFileName& ) { |
|---|
| 176 | std::cout << "[FileManager::copy] \"fs->copy\" " << srcPath << " to " << dstPath << " failed with CF::InvalidFileName\n"; |
|---|
| 177 | exit(EXIT_FAILURE); |
|---|
| 178 | } catch ( CORBA::SystemException& se ) { |
|---|
| 179 | std::cout << "[FileManager::copy] \"fs->copy\" " << srcPath << " to " << dstPath << " failed with CORBA::SystemException\n"; |
|---|
| 180 | exit(EXIT_FAILURE); |
|---|
| 181 | } catch ( ... ) { |
|---|
| 182 | std::cout << "[FileManager::copy] \"fs->copy\" " << srcPath << " to " << dstPath << " failed with Unknown Exception\n"; |
|---|
| 183 | exit(EXIT_FAILURE); |
|---|
| 184 | } |
|---|
| 185 | return; |
|---|
| 186 | } |
|---|
| 187 | |
|---|
| 188 | // Copy file across FileSystems |
|---|
| 189 | |
|---|
| 190 | CF::File_var srcFile; |
|---|
| 191 | try { |
|---|
| 192 | srcFile = mount_table[srcFS].fs->open(srcPath.c_str(), true); |
|---|
| 193 | } catch ( CORBA::SystemException& se ) { |
|---|
| 194 | std::cout << "[FileManager::copy] \"fs->open\" " << srcPath << " failed with CORBA::SystemException\n"; |
|---|
| 195 | exit(EXIT_FAILURE); |
|---|
| 196 | } catch ( ... ) { |
|---|
| 197 | std::cout << "[FileManager::copy] \"fs->open\" " << srcPath << " failed with Unknown Exception\n"; |
|---|
| 198 | exit(EXIT_FAILURE); |
|---|
| 199 | } |
|---|
| 200 | |
|---|
| 201 | unsigned int srcSize = srcFile->sizeOf(); |
|---|
| 202 | |
|---|
| 203 | if (srcSize == 0) { ///\todo Check spec to see why we throw if size == 0 |
|---|
| 204 | try { |
|---|
| 205 | srcFile->close(); |
|---|
| 206 | } catch ( CORBA::SystemException& se ) { |
|---|
| 207 | std::cout << "[FileManager::copy] \"srcFile->close\" failed with CORBA::SystemException for file size of zero\n"; |
|---|
| 208 | exit(EXIT_FAILURE); |
|---|
| 209 | } catch ( ... ) { |
|---|
| 210 | std::cout << "[FileManager::copy] \"srcFile->close\" failed with Unknown Exception for file size of zero\n"; |
|---|
| 211 | exit(EXIT_FAILURE); |
|---|
| 212 | } |
|---|
| 213 | throw CF::FileException (); |
|---|
| 214 | } |
|---|
| 215 | |
|---|
| 216 | if (!mount_table[dstFS].fs->exists(dstPath.c_str())) { |
|---|
| 217 | try { |
|---|
| 218 | mount_table[dstFS].fs->create(dstPath.c_str()); |
|---|
| 219 | } catch ( CORBA::SystemException& se ) { |
|---|
| 220 | std::cout << "[FileManager::copy] \"fs->create\" " << dstPath << " failed with CORBA::SystemException\n"; |
|---|
| 221 | exit(EXIT_FAILURE); |
|---|
| 222 | } catch ( ... ) { |
|---|
| 223 | std::cout << "[FileManager::copy] \"fs->create\" " << dstPath << " failed with Unknown Exception\n"; |
|---|
| 224 | exit(EXIT_FAILURE); |
|---|
| 225 | } |
|---|
| 226 | } |
|---|
| 227 | |
|---|
| 228 | CF::File_var dstFile; |
|---|
| 229 | try { |
|---|
| 230 | dstFile = mount_table[dstFS].fs->open (dstPath.c_str(), false); |
|---|
| 231 | } catch ( CORBA::SystemException& se ) { |
|---|
| 232 | std::cout << "[FileManager::copy] \"fs->open\" " << dstPath << " failed with CORBA::SystemException\n"; |
|---|
| 233 | exit(EXIT_FAILURE); |
|---|
| 234 | } catch ( ... ) { |
|---|
| 235 | std::cout << "[FileManager::copy] \"fs->copy\" " << dstPath << " failed with Unknown Exception\n"; |
|---|
| 236 | exit(EXIT_FAILURE); |
|---|
| 237 | } |
|---|
| 238 | |
|---|
| 239 | CF::OctetSequence_var data; |
|---|
| 240 | |
|---|
| 241 | try { |
|---|
| 242 | srcFile->read (data, srcSize); |
|---|
| 243 | } catch ( CORBA::SystemException& se ) { |
|---|
| 244 | std::cout << "[FileManager::copy] \"srcFile->read\" failed with CORBA::SystemException\n"; |
|---|
| 245 | exit(EXIT_FAILURE); |
|---|
| 246 | } catch ( ... ) { |
|---|
| 247 | std::cout << "[FileManager::copy] \"srcFile->read\" failed with Unknown Exception\n"; |
|---|
| 248 | exit(EXIT_FAILURE); |
|---|
| 249 | } |
|---|
| 250 | |
|---|
| 251 | try { |
|---|
| 252 | dstFile->write (data); |
|---|
| 253 | } catch ( CORBA::SystemException& se ) { |
|---|
| 254 | std::cout << "[FileManager::copy] \"dstFile->write\" failed with CORBA::SystemException\n"; |
|---|
| 255 | exit(EXIT_FAILURE); |
|---|
| 256 | } catch ( ... ) { |
|---|
| 257 | std::cout << "[FileManager::copy] \"dstFile->write\" failed with Unknown Exception\n"; |
|---|
| 258 | exit(EXIT_FAILURE); |
|---|
| 259 | } |
|---|
| 260 | |
|---|
| 261 | try { |
|---|
| 262 | srcFile->close(); |
|---|
| 263 | } catch ( CORBA::SystemException& se ) { |
|---|
| 264 | std::cout << "[FileManager::copy] \"srcFile->close\" failed with CORBA::SystemException\n"; |
|---|
| 265 | exit(EXIT_FAILURE); |
|---|
| 266 | } catch ( ... ) { |
|---|
| 267 | std::cout << "[FileManager::copy] \"srcFile->close\" failed with Unknown Exception\n"; |
|---|
| 268 | exit(EXIT_FAILURE); |
|---|
| 269 | } |
|---|
| 270 | |
|---|
| 271 | try { |
|---|
| 272 | dstFile->close(); |
|---|
| 273 | } catch ( CORBA::SystemException& se ) { |
|---|
| 274 | std::cout << "[FileManager::copy] \"dstFile->close\" failed with CORBA::SystemException\n"; |
|---|
| 275 | exit(EXIT_FAILURE); |
|---|
| 276 | } catch ( ... ) { |
|---|
| 277 | std::cout << "[FileManager::copy] \"dstFile->close\" failed with Unknown Exception\n"; |
|---|
| 278 | exit(EXIT_FAILURE); |
|---|
| 279 | } |
|---|
| 280 | } |
|---|
| 281 | |
|---|
| 282 | |
|---|
| 283 | CORBA::Boolean FileManager_impl::exists (const char *fileName) |
|---|
| 284 | throw (CORBA::SystemException, CF::InvalidFileName) |
|---|
| 285 | { |
|---|
| 286 | DEBUG(4, FileManager, "Entering exists with " << fileName) |
|---|
| 287 | |
|---|
| 288 | if (!ossieSupport::isValidFileName(fileName)) { |
|---|
| 289 | DEBUG(7, FileManager, "exists passed bad filename, throwing exception."); |
|---|
| 290 | throw CF::InvalidFileName (CF::CFEINVAL, "[FileManager::exists] Invalid file name"); |
|---|
| 291 | } |
|---|
| 292 | |
|---|
| 293 | DEBUG(6, FileManager, "Running FS.exists for file " << fileName) |
|---|
| 294 | |
|---|
| 295 | for ( unsigned int i = 0; i < numMounts; i++ ) { |
|---|
| 296 | if (mount_table[i].fs->exists (fileName) ) |
|---|
| 297 | return true; |
|---|
| 298 | } |
|---|
| 299 | return false; |
|---|
| 300 | } |
|---|
| 301 | |
|---|
| 302 | |
|---|
| 303 | CF::FileSystem::FileInformationSequence * |
|---|
| 304 | FileManager_impl::list (const char *pattern) throw (CORBA::SystemException, |
|---|
| 305 | CF::FileException, |
|---|
| 306 | CF::InvalidFileName) |
|---|
| 307 | { |
|---|
| 308 | DEBUG(4, FileManager, "Entering list with " << pattern); |
|---|
| 309 | |
|---|
| 310 | if (pattern[0] != '/') |
|---|
| 311 | throw CF::InvalidFileName(CF::CFEINVAL, "[FileManager::list] Relative path given."); |
|---|
| 312 | |
|---|
| 313 | CF::FileSystem::FileInformationType* fit_arr[numMounts]; |
|---|
| 314 | int fit_length[numMounts]; |
|---|
| 315 | int result_length = 0; |
|---|
| 316 | int tmp_length = 0; |
|---|
| 317 | try { |
|---|
| 318 | for ( unsigned int i = 0; i < numMounts; i++ ) { |
|---|
| 319 | DEBUG(4, FileManager, "Calling FileSystem->list"); |
|---|
| 320 | CF::FileSystem::FileInformationSequence_var fis = mount_table[i].fs->list (pattern); |
|---|
| 321 | DEBUG(4, FileManager, "Returned from FileSystem->list"); |
|---|
| 322 | result_length += fis->length(); |
|---|
| 323 | DEBUG(4, FileManager, "Recording "<<result_length<<" matches") |
|---|
| 324 | fit_length[i] = fis->length(); |
|---|
| 325 | fit_arr[i] = fis->get_buffer(true); |
|---|
| 326 | } |
|---|
| 327 | DEBUG(4, FileManager, "Creating FIS results"); |
|---|
| 328 | tmp_length = result_length; |
|---|
| 329 | CF::FileSystem::FileInformationType *fit = new CF::FileSystem::FileInformationType[result_length]; |
|---|
| 330 | CF::FileSystem::FileInformationType *tmp; |
|---|
| 331 | CF::FileSystem::FileInformationType *beg = fit; |
|---|
| 332 | DEBUG(4, FileManager, "Adding to FIS results"); |
|---|
| 333 | for ( int j = 0; j < numMounts; j++ ) { |
|---|
| 334 | std::cout << "[FileManager::list] In mount number " << j << "\n"; |
|---|
| 335 | tmp = fit_arr[j]; |
|---|
| 336 | for ( unsigned int k = 0; k < fit_length[j]; k++ ) { |
|---|
| 337 | std::cout << "[FileManager::list] Adding file number " << k << "\n"; |
|---|
| 338 | *fit = *tmp; |
|---|
| 339 | fit ++; |
|---|
| 340 | tmp ++; |
|---|
| 341 | } |
|---|
| 342 | } |
|---|
| 343 | fit = beg; |
|---|
| 344 | CF::FileSystem::FileInformationSequence_var result = |
|---|
| 345 | new CF::FileSystem::FileInformationSequence(result_length, result_length, fit); |
|---|
| 346 | DEBUG(4, FileManager, "About to return from list"); |
|---|
| 347 | return result._retn(); |
|---|
| 348 | } catch (const fs::filesystem_error &ex) { |
|---|
| 349 | #if BOOST_VERSION < 103400 |
|---|
| 350 | DEBUG(9, FileManager, "Caught exception in list, error_code " << ex.error()); |
|---|
| 351 | if (ex.error() == fs::other_error) |
|---|
| 352 | #elif BOOST_VERSION < 103500 |
|---|
| 353 | DEBUG(9, FileManager, "Caught exception in list, error_code " << ex.system_error()); |
|---|
| 354 | if (ex.system_error() == EINVAL) |
|---|
| 355 | #else |
|---|
| 356 | DEBUG(9, FileManager, "Caught exception in list, error_code " << ex.code().value()); |
|---|
| 357 | if (ex.code().value() == EINVAL) |
|---|
| 358 | #endif |
|---|
| 359 | throw CF::InvalidFileName(CF::CFEINVAL, ex.what()); |
|---|
| 360 | throw CF::FileException(CF::CFNOTSET, ex.what()); |
|---|
| 361 | } |
|---|
| 362 | } |
|---|
| 363 | |
|---|
| 364 | |
|---|
| 365 | CF::File_ptr FileManager_impl::create (const char *fileName) throw (CORBA:: |
|---|
| 366 | SystemException, |
|---|
| 367 | CF:: |
|---|
| 368 | InvalidFileName, |
|---|
| 369 | CF:: |
|---|
| 370 | FileException) |
|---|
| 371 | { |
|---|
| 372 | DEBUG(4, FileManager, "Entering create with " << fileName) |
|---|
| 373 | |
|---|
| 374 | if (!ossieSupport::isValidFileName(fileName)) { |
|---|
| 375 | DEBUG(7, FileManager, "create passed bad filename, throwing exception."); |
|---|
| 376 | throw CF::InvalidFileName (CF::CFEINVAL, "[FileManager::create] Invalid file name"); |
|---|
| 377 | } |
|---|
| 378 | |
|---|
| 379 | unsigned int fileFS(0); |
|---|
| 380 | std::string filePath; |
|---|
| 381 | |
|---|
| 382 | getFSandFSPath(fileName, fileFS, filePath); |
|---|
| 383 | |
|---|
| 384 | CF::File_var file_var; |
|---|
| 385 | try { |
|---|
| 386 | file_var = mount_table[fileFS].fs->create (filePath.c_str()); |
|---|
| 387 | } catch ( CORBA::SystemException& se ) { |
|---|
| 388 | std::cout << "[FileManager::create] \"fs->create\" " << filePath << " failed with CORBA::SystemException\n"; |
|---|
| 389 | exit(EXIT_FAILURE); |
|---|
| 390 | } catch ( ... ) { |
|---|
| 391 | std::cout << "[FileManager::create] \"fs->create\" " << filePath << " failed with Unknown Exception\n"; |
|---|
| 392 | exit(EXIT_FAILURE); |
|---|
| 393 | } |
|---|
| 394 | |
|---|
| 395 | return file_var._retn(); |
|---|
| 396 | } |
|---|
| 397 | |
|---|
| 398 | |
|---|
| 399 | CF::File_ptr |
|---|
| 400 | FileManager_impl::open (const char *fileName, CORBA::Boolean read_Only) |
|---|
| 401 | throw (CORBA::SystemException,CF::InvalidFileName, CF::FileException) |
|---|
| 402 | { |
|---|
| 403 | DEBUG(4, FileManager, "Entering open with " << fileName) |
|---|
| 404 | |
|---|
| 405 | if (!ossieSupport::isValidFileName(fileName)) { |
|---|
| 406 | DEBUG(7, FileManager, "open passed bad filename, throwing exception."); |
|---|
| 407 | throw CF::InvalidFileName (CF::CFEINVAL, "[FileManager::open] Invalid file name"); |
|---|
| 408 | } |
|---|
| 409 | |
|---|
| 410 | // unsigned int fileFS(0); |
|---|
| 411 | // std::string filePath; |
|---|
| 412 | |
|---|
| 413 | // getFSandFSPath(fileName, fileFS, filePath); |
|---|
| 414 | CF::File_var file_var; |
|---|
| 415 | for ( unsigned int i = 0; i < numMounts; i++ ) { |
|---|
| 416 | if ( mount_table[i].fs->exists(fileName) ) { |
|---|
| 417 | try { |
|---|
| 418 | file_var = mount_table[i].fs->open (fileName, read_Only); |
|---|
| 419 | } catch ( CF::InvalidFileName &_ex ) { |
|---|
| 420 | std::cout << "[FileManager::open] While opening file " << fileName << ": " << _ex.msg << "\n"; |
|---|
| 421 | throw CF::FileException(CF::CFNOTSET, "[FileManager::open] File not found in any mounted File Systems"); |
|---|
| 422 | } catch ( CF::FileException &_ex ) { |
|---|
| 423 | std::cout << "[FileManager::open] While opening file " << fileName << ": " << _ex.msg << "\n"; |
|---|
| 424 | throw CF::FileException(CF::CFNOTSET, "[FileManager::open] File not found in any mounted File Systems"); |
|---|
| 425 | } catch ( ... ) { |
|---|
| 426 | std::cout << "[FileManager::open] While opening file " << fileName << ": Unknown Exception\n"; |
|---|
| 427 | exit(EXIT_FAILURE); |
|---|
| 428 | } |
|---|
| 429 | break; |
|---|
| 430 | } |
|---|
| 431 | } |
|---|
| 432 | |
|---|
| 433 | return file_var._retn(); |
|---|
| 434 | } |
|---|
| 435 | |
|---|
| 436 | |
|---|
| 437 | void |
|---|
| 438 | FileManager_impl::mkdir (const char *directoryName) |
|---|
| 439 | throw (CORBA::SystemException, CF::FileException, CF::InvalidFileName) |
|---|
| 440 | { |
|---|
| 441 | DEBUG(4, FileManager, "Entering mkdir with " << directoryName) |
|---|
| 442 | |
|---|
| 443 | if (!ossieSupport::isValidFileName(directoryName)) { |
|---|
| 444 | DEBUG(7, FileManager, "mkdir passed bad filename, throwing exception."); |
|---|
| 445 | throw CF::InvalidFileName (CF::CFEINVAL, "[FileManager::mkdir] Invalid directory name"); |
|---|
| 446 | } |
|---|
| 447 | |
|---|
| 448 | unsigned int fileFS(0); |
|---|
| 449 | std::string filePath; |
|---|
| 450 | |
|---|
| 451 | getFSandFSPath(directoryName, fileFS, filePath); |
|---|
| 452 | |
|---|
| 453 | try { |
|---|
| 454 | mount_table[fileFS].fs->mkdir(filePath.c_str()); |
|---|
| 455 | } catch ( CORBA::SystemException& se ) { |
|---|
| 456 | std::cout << "[FileManager::mkdir] \"fs->mkdir\" " << filePath << " failed with CORBA::SystemException\n"; |
|---|
| 457 | exit(EXIT_FAILURE); |
|---|
| 458 | } catch ( ... ) { |
|---|
| 459 | std::cout << "[FileManager::mkdir] \"fs->mkdir\" " << filePath << " failed with Unknown Exception\n"; |
|---|
| 460 | exit(EXIT_FAILURE); |
|---|
| 461 | } |
|---|
| 462 | |
|---|
| 463 | DEBUG(4, FileManager, "Leaving mkdir.") |
|---|
| 464 | } |
|---|
| 465 | |
|---|
| 466 | |
|---|
| 467 | void |
|---|
| 468 | FileManager_impl::rmdir (const char *directoryName) |
|---|
| 469 | throw (CORBA::SystemException, CF::FileException, CF::InvalidFileName) |
|---|
| 470 | { |
|---|
| 471 | DEBUG(4, FileManager, "Entering rmdir with " << directoryName) |
|---|
| 472 | |
|---|
| 473 | if (!ossieSupport::isValidFileName(directoryName)) { |
|---|
| 474 | DEBUG(7, FileManager, "rmdir passed bad filename, throwing exception."); |
|---|
| 475 | throw CF::InvalidFileName (CF::CFEINVAL, "[FileManager::rmdir] Invalid directory name"); |
|---|
| 476 | } |
|---|
| 477 | |
|---|
| 478 | unsigned int fileFS(0); |
|---|
| 479 | std::string filePath; |
|---|
| 480 | |
|---|
| 481 | getFSandFSPath(directoryName, fileFS, filePath); |
|---|
| 482 | |
|---|
| 483 | try { |
|---|
| 484 | mount_table[fileFS].fs->rmdir (filePath.c_str()); |
|---|
| 485 | } catch ( CORBA::SystemException& se ) { |
|---|
| 486 | std::cout << "[FileManager::rmdir] \"fs->rmdir\" " << filePath << " failed with CORBA::SystemException\n"; |
|---|
| 487 | exit(EXIT_FAILURE); |
|---|
| 488 | } catch ( ... ) { |
|---|
| 489 | std::cout << "[FileManager::rmdir] \"fs->rmdir\" " << filePath << " failed with Unknown Exception\n"; |
|---|
| 490 | exit(EXIT_FAILURE); |
|---|
| 491 | } |
|---|
| 492 | |
|---|
| 493 | DEBUG(4, FileManager, "Leaving rmdir.") |
|---|
| 494 | } |
|---|
| 495 | |
|---|
| 496 | |
|---|
| 497 | void |
|---|
| 498 | FileManager_impl::query (CF::Properties & fileSysProperties) |
|---|
| 499 | throw (CORBA::SystemException, CF::FileSystem::UnknownFileSystemProperties) |
|---|
| 500 | { |
|---|
| 501 | DEBUG(4, FileManager, "Entering query.") |
|---|
| 502 | |
|---|
| 503 | #if 0 |
|---|
| 504 | unsigned int fileFS(0); |
|---|
| 505 | char *filePath = new char[MAXPATHLEN]; |
|---|
| 506 | |
|---|
| 507 | getFSandFSPath(fileName, fileFS, filePath); |
|---|
| 508 | #endif |
|---|
| 509 | |
|---|
| 510 | bool check; |
|---|
| 511 | |
|---|
| 512 | for (unsigned int i = 0; i < fileSysProperties.length (); i++) { |
|---|
| 513 | check = false; |
|---|
| 514 | |
|---|
| 515 | if (strcmp (fileSysProperties[i].id, CF::FileSystem::SIZE) == 0) { |
|---|
| 516 | CORBA::Long totalSize, temp; |
|---|
| 517 | totalSize = 0; |
|---|
| 518 | |
|---|
| 519 | for (unsigned int j = 0; j < mount_table->length(); j++) { |
|---|
| 520 | CF::DataType dt; |
|---|
| 521 | dt.id = CORBA::string_dup ("SIZE"); |
|---|
| 522 | CF::Properties pr (2, 1, &dt, 0); |
|---|
| 523 | |
|---|
| 524 | try { |
|---|
| 525 | mount_table[j].fs->query (pr); |
|---|
| 526 | } catch ( CORBA::SystemException& se ) { |
|---|
| 527 | std::cout << "[FileManager::query] \"fs->query\" failed with CORBA::SystemException\n"; |
|---|
| 528 | exit(EXIT_FAILURE); |
|---|
| 529 | } catch ( ... ) { |
|---|
| 530 | std::cout << "[FileManager::query] \"fs->query\" failed with Unknown Exception\n"; |
|---|
| 531 | exit(EXIT_FAILURE); |
|---|
| 532 | } |
|---|
| 533 | |
|---|
| 534 | CF::DataType * _dt = pr.get_buffer (); |
|---|
| 535 | |
|---|
| 536 | for (unsigned int k = 0; k < pr.length (); k++) { |
|---|
| 537 | _dt->value >>= temp; |
|---|
| 538 | totalSize = totalSize + temp; |
|---|
| 539 | _dt++; |
|---|
| 540 | } |
|---|
| 541 | |
|---|
| 542 | fileSysProperties[i].value >>= temp; |
|---|
| 543 | fileSysProperties[i].value <<= totalSize + temp; |
|---|
| 544 | |
|---|
| 545 | check = true; |
|---|
| 546 | } |
|---|
| 547 | } |
|---|
| 548 | |
|---|
| 549 | if (strcmp (fileSysProperties[i].id, |
|---|
| 550 | CF::FileSystem::AVAILABLE_SIZE) == 0) { |
|---|
| 551 | CORBA::Long totalSize; |
|---|
| 552 | totalSize = 0; |
|---|
| 553 | |
|---|
| 554 | for (unsigned int i = 0; i < mount_table->length(); i++) { |
|---|
| 555 | } |
|---|
| 556 | |
|---|
| 557 | check = true; |
|---|
| 558 | } |
|---|
| 559 | |
|---|
| 560 | if (!check) |
|---|
| 561 | throw CF::FileSystem::UnknownFileSystemProperties (); |
|---|
| 562 | |
|---|
| 563 | ///\todo Add functionality to query ALL FileManager properties |
|---|
| 564 | } |
|---|
| 565 | DEBUG(4, FileManager, "Leaving query.") |
|---|
| 566 | } |
|---|
| 567 | |
|---|
| 568 | |
|---|
| 569 | CF::FileManager::MountSequence* |
|---|
| 570 | FileManager_impl::getMounts ()throw (CORBA::SystemException) |
|---|
| 571 | { |
|---|
| 572 | DEBUG(4, FileManager, "Entering MountSequence."); |
|---|
| 573 | CF::FileManager::MountSequence_var result = new CF::FileManager::MountSequence(mount_table); |
|---|
| 574 | return result._retn(); |
|---|
| 575 | } |
|---|
| 576 | |
|---|
| 577 | void FileManager_impl::getFSandFSPath(const char *path, unsigned int &mountTableIndex, std::string &FSPath) |
|---|
| 578 | { |
|---|
| 579 | DEBUG(5, FileManager, "Entering getFSandFSPath with path " << path) |
|---|
| 580 | |
|---|
| 581 | fs::path fullPath(path); |
|---|
| 582 | fs::path::iterator fullItr(fullPath.begin()); |
|---|
| 583 | std::string tmpFSPath; |
|---|
| 584 | |
|---|
| 585 | unsigned int lastMatchLength(0); |
|---|
| 586 | |
|---|
| 587 | for (unsigned int i(0); i < numMounts; ++i) { |
|---|
| 588 | unsigned int matchLength = pathMatches(path,mount_table[i].mountPoint, tmpFSPath); |
|---|
| 589 | if ( matchLength > lastMatchLength) { |
|---|
| 590 | mountTableIndex = i; |
|---|
| 591 | lastMatchLength = matchLength; |
|---|
| 592 | FSPath = tmpFSPath; |
|---|
| 593 | } |
|---|
| 594 | } |
|---|
| 595 | DEBUG(5, FileManager, "Found mountIndex " << mountTableIndex << " and local path " << FSPath) |
|---|
| 596 | std::cout << "[FileManager::getFSandFSPath] Found mountIndex " << mountTableIndex << " and local path " << FSPath << "\n"; |
|---|
| 597 | |
|---|
| 598 | } |
|---|
| 599 | |
|---|
| 600 | unsigned int FileManager_impl::pathMatches(const char * path, const char *mPoint, std::string &FSPath) |
|---|
| 601 | { |
|---|
| 602 | DEBUG(5, FileManager, "Entering pathMatches with path " << path << " mount point " << mPoint) |
|---|
| 603 | |
|---|
| 604 | fs::path fullPath(path); |
|---|
| 605 | fs::path::iterator fullItr(fullPath.begin()); |
|---|
| 606 | |
|---|
| 607 | fs::path mPath(mPoint); |
|---|
| 608 | fs::path::iterator mItr(mPath.begin()); |
|---|
| 609 | |
|---|
| 610 | unsigned int commonElements(0); |
|---|
| 611 | |
|---|
| 612 | while (fullItr != fullPath.end()) { |
|---|
| 613 | if (*fullItr != *mItr) |
|---|
| 614 | break; |
|---|
| 615 | ++fullItr; |
|---|
| 616 | ++mItr; |
|---|
| 617 | ++commonElements; |
|---|
| 618 | } |
|---|
| 619 | |
|---|
| 620 | |
|---|
| 621 | fs::path localPath("/"); |
|---|
| 622 | |
|---|
| 623 | while (fullItr != fullPath.end()) { |
|---|
| 624 | localPath /= *fullItr; |
|---|
| 625 | ++fullItr; |
|---|
| 626 | } |
|---|
| 627 | FSPath = localPath.string(); |
|---|
| 628 | |
|---|
| 629 | DEBUG(5, FileManager, "Path matches with " << commonElements << " path elements in common and remaining path " << FSPath) |
|---|
| 630 | |
|---|
| 631 | return commonElements; |
|---|
| 632 | } |
|---|
| 633 | |
|---|