| 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 | /* SCA */ |
|---|
| 24 | |
|---|
| 25 | #include <iostream> |
|---|
| 26 | #include <string> |
|---|
| 27 | |
|---|
| 28 | #include <boost/filesystem/operations.hpp> |
|---|
| 29 | #include <boost/filesystem/path.hpp> |
|---|
| 30 | |
|---|
| 31 | #include "ossie/FileSystem_impl.h" |
|---|
| 32 | #include "ossie/debug.h" |
|---|
| 33 | |
|---|
| 34 | FileSystem_impl::FileSystem_impl () |
|---|
| 35 | { |
|---|
| 36 | DEBUG(6, FileSystem, "In constructor."); |
|---|
| 37 | |
|---|
| 38 | root = boost::filesystem::initial_path(); |
|---|
| 39 | |
|---|
| 40 | init (); |
|---|
| 41 | } |
|---|
| 42 | |
|---|
| 43 | |
|---|
| 44 | FileSystem_impl::FileSystem_impl (char *_root) |
|---|
| 45 | { |
|---|
| 46 | DEBUG(6, FileSystem, "In constructor with " << _root); |
|---|
| 47 | |
|---|
| 48 | root = _root; |
|---|
| 49 | |
|---|
| 50 | init (); |
|---|
| 51 | } |
|---|
| 52 | |
|---|
| 53 | |
|---|
| 54 | void |
|---|
| 55 | FileSystem_impl::init () |
|---|
| 56 | { |
|---|
| 57 | DEBUG(6, FileSystem, "In init()"); |
|---|
| 58 | } |
|---|
| 59 | |
|---|
| 60 | FileSystem_impl::~FileSystem_impl () |
|---|
| 61 | { |
|---|
| 62 | DEBUG(6, FileSystem, "In destructor."); |
|---|
| 63 | } |
|---|
| 64 | |
|---|
| 65 | void FileSystem_impl::remove (const char *fileName) throw (CORBA::SystemException, CF::FileException, CF::InvalidFileName) |
|---|
| 66 | { |
|---|
| 67 | DEBUG(6, FileSystem, "In remove with " << fileName); |
|---|
| 68 | |
|---|
| 69 | if (!ossieSupport::isValidFileName(fileName)) { |
|---|
| 70 | DEBUG(7, FileSystem, "remove passed bad filename, throwing exception."); |
|---|
| 71 | throw CF::InvalidFileName (CF::CFEINVAL, "[FileSystem::remove] Invalid file name"); |
|---|
| 72 | } |
|---|
| 73 | |
|---|
| 74 | boost::filesystem::path fname(root / fileName); |
|---|
| 75 | |
|---|
| 76 | DEBUG(6, FileSystem, "About to remove file " << fname.string()); |
|---|
| 77 | bool exists = boost::filesystem::remove(fname); |
|---|
| 78 | |
|---|
| 79 | if (!exists) { |
|---|
| 80 | DEBUG(6, FileSystem, "Attempt to remove non-existant file."); |
|---|
| 81 | throw (CF::FileException (CF::CFEEXIST, "[FileSystem_impl::remove] Error removing file from file system")); |
|---|
| 82 | } |
|---|
| 83 | } |
|---|
| 84 | |
|---|
| 85 | void FileSystem_impl::copy (const char *sourceFileName, const char *destinationFileName) throw (CORBA::SystemException, CF::InvalidFileName, CF::FileException) |
|---|
| 86 | { |
|---|
| 87 | DEBUG(6, FileSystem, "In copy from " << sourceFileName << " to " << destinationFileName); |
|---|
| 88 | |
|---|
| 89 | if (!ossieSupport::isValidFileName(sourceFileName) || !ossieSupport::isValidFileName(destinationFileName)) { |
|---|
| 90 | DEBUG(7, FileSystem, "copy passed bad filename, throwing exception."); |
|---|
| 91 | throw CF::InvalidFileName (CF::CFEINVAL, "[FileSystem::copy] Invalid file name"); |
|---|
| 92 | } |
|---|
| 93 | |
|---|
| 94 | |
|---|
| 95 | boost::filesystem::path sFile(root / sourceFileName); |
|---|
| 96 | boost::filesystem::path dFile(root / destinationFileName); |
|---|
| 97 | |
|---|
| 98 | ///\todo Add proper exception handling |
|---|
| 99 | |
|---|
| 100 | boost::filesystem::copy_file(sFile, dFile); |
|---|
| 101 | } |
|---|
| 102 | |
|---|
| 103 | CORBA::Boolean FileSystem_impl::exists (const char *fileName) |
|---|
| 104 | throw (CORBA::SystemException, CF::InvalidFileName) |
|---|
| 105 | { |
|---|
| 106 | DEBUG(6, FileSystem, "In exists with " << fileName); |
|---|
| 107 | |
|---|
| 108 | if (!ossieSupport::isValidFileName(fileName)) { |
|---|
| 109 | DEBUG(7, FileSystem, "exists passed bad filename, throwing exception."); |
|---|
| 110 | throw CF::InvalidFileName (CF::CFEINVAL, "[FileSystem::exists] Invalid file name"); |
|---|
| 111 | } |
|---|
| 112 | |
|---|
| 113 | boost::filesystem::path fname(root / fileName); |
|---|
| 114 | |
|---|
| 115 | return(boost::filesystem::exists(fname)); |
|---|
| 116 | } |
|---|
| 117 | |
|---|
| 118 | |
|---|
| 119 | /// \todo: modify to search the pattern as a regular expression |
|---|
| 120 | CF::FileSystem::FileInformationSequence* FileSystem_impl::list (const char *pattern) throw (CORBA::SystemException, CF::FileException, CF::InvalidFileName) |
|---|
| 121 | { |
|---|
| 122 | DEBUG(6, FileSystem, "In list with " << pattern); |
|---|
| 123 | |
|---|
| 124 | boost::filesystem::path filePath(pattern); |
|---|
| 125 | std::string searchName(filePath.leaf()); |
|---|
| 126 | boost::filesystem::path dirPath(root / filePath.branch_path()); |
|---|
| 127 | |
|---|
| 128 | CF::FileSystem::FileInformationSequence_var result; |
|---|
| 129 | unsigned int idx(0); |
|---|
| 130 | |
|---|
| 131 | boost::filesystem::directory_iterator end_itr; // past the end |
|---|
| 132 | for (boost::filesystem::directory_iterator itr(dirPath); itr != end_itr; ++itr) { |
|---|
| 133 | if (itr->leaf().find(searchName) == std::string::npos) { |
|---|
| 134 | result->length(idx + 1); |
|---|
| 135 | boost::filesystem::path foundPath(filePath.branch_path() / itr->leaf()); |
|---|
| 136 | result[idx].name = CORBA::string_dup(foundPath.root_directory().c_str()); |
|---|
| 137 | if (boost::filesystem::is_directory(*itr)) |
|---|
| 138 | result[idx].kind = CF::FileSystem::DIRECTORY; |
|---|
| 139 | else |
|---|
| 140 | result[idx].kind = CF::FileSystem::PLAIN; |
|---|
| 141 | |
|---|
| 142 | result[idx].size = boost::filesystem::file_size(*itr); |
|---|
| 143 | |
|---|
| 144 | /// \todo fix file creation time and last access time |
|---|
| 145 | CF::Properties prop; |
|---|
| 146 | prop.length(3); |
|---|
| 147 | prop[0].id = CORBA::string_dup(CF::FileSystem::CREATED_TIME_ID); |
|---|
| 148 | prop[0].value <<= boost::filesystem::last_write_time(*itr); |
|---|
| 149 | prop[1].id = CORBA::string_dup(CF::FileSystem::MODIFIED_TIME_ID); |
|---|
| 150 | prop[1].value <<= boost::filesystem::last_write_time(*itr); |
|---|
| 151 | prop[2].id = CORBA::string_dup(CF::FileSystem::LAST_ACCESS_TIME_ID); |
|---|
| 152 | prop[2].value <<= boost::filesystem::last_write_time(*itr); |
|---|
| 153 | |
|---|
| 154 | result[idx].fileProperties = prop; |
|---|
| 155 | ++idx; |
|---|
| 156 | } |
|---|
| 157 | } |
|---|
| 158 | return result._retn(); |
|---|
| 159 | } |
|---|
| 160 | |
|---|
| 161 | |
|---|
| 162 | CF::File_ptr FileSystem_impl::create (const char *fileName) throw (CORBA::SystemException, CF::InvalidFileName, CF::FileException) |
|---|
| 163 | { |
|---|
| 164 | DEBUG(6, FileSystem, "In create with " << fileName); |
|---|
| 165 | |
|---|
| 166 | if (!ossieSupport::isValidFileName(fileName)) { |
|---|
| 167 | DEBUG(7, FileSystem, "create passed bad filename, throwing exception."); |
|---|
| 168 | throw CF::InvalidFileName (CF::CFEINVAL, "[FileSystem::create] Invalid file name"); |
|---|
| 169 | } |
|---|
| 170 | |
|---|
| 171 | if (exists(fileName)) { |
|---|
| 172 | DEBUG(6, FileSystem, "FileName exists in create, throwing exception."); |
|---|
| 173 | throw CF::FileException(CF::CFEEXIST, "File exists."); |
|---|
| 174 | } |
|---|
| 175 | |
|---|
| 176 | File_impl *file = new File_impl (fileName, root, false, true); |
|---|
| 177 | CF::File_var fileServant = file->_this(); |
|---|
| 178 | |
|---|
| 179 | fileInfo newFile; |
|---|
| 180 | newFile.fileName = fileName; |
|---|
| 181 | newFile.servant = fileServant; |
|---|
| 182 | files.push_back(newFile); |
|---|
| 183 | |
|---|
| 184 | return fileServant._retn(); |
|---|
| 185 | } |
|---|
| 186 | |
|---|
| 187 | CF::File_ptr FileSystem_impl::open (const char *fileName, CORBA::Boolean read_Only) throw (CORBA::SystemException, CF::InvalidFileName, CF::FileException) |
|---|
| 188 | { |
|---|
| 189 | DEBUG(6, FileSystem, "In open with " << fileName); |
|---|
| 190 | |
|---|
| 191 | if (!ossieSupport::isValidFileName(fileName)) { |
|---|
| 192 | DEBUG(7, FileSystem, "open passed bad filename, throwing exception."); |
|---|
| 193 | throw CF::InvalidFileName (CF::CFEINVAL, "[FileSystem::open] Invalid file name"); |
|---|
| 194 | } |
|---|
| 195 | |
|---|
| 196 | boost::filesystem::path filePath(root / fileName); |
|---|
| 197 | |
|---|
| 198 | File_impl *file = new File_impl (fileName, root, read_Only, false); |
|---|
| 199 | CF::File_var fileServant = file->_this(); |
|---|
| 200 | |
|---|
| 201 | fileInfo newFile; |
|---|
| 202 | newFile.fileName = fileName; |
|---|
| 203 | newFile.servant = fileServant; |
|---|
| 204 | files.push_back(newFile); |
|---|
| 205 | |
|---|
| 206 | return fileServant._retn(); |
|---|
| 207 | } |
|---|
| 208 | |
|---|
| 209 | |
|---|
| 210 | void FileSystem_impl::mkdir (const char *directoryName) throw (CORBA::SystemException, CF::FileException, CF::InvalidFileName) |
|---|
| 211 | { |
|---|
| 212 | DEBUG(6, FileSystem, "In mkdir with " << directoryName); |
|---|
| 213 | |
|---|
| 214 | if (!ossieSupport::isValidFileName(directoryName)) { |
|---|
| 215 | DEBUG(7, FileSystem, "mkdir passed bad filename, throwing exception."); |
|---|
| 216 | throw CF::InvalidFileName (CF::CFEINVAL, "[FileSystem::mkdir] Invalid file name"); |
|---|
| 217 | } |
|---|
| 218 | |
|---|
| 219 | boost::filesystem::path dirPath(root / directoryName); |
|---|
| 220 | |
|---|
| 221 | ///\todo Create entire path when needed. |
|---|
| 222 | try { |
|---|
| 223 | boost::filesystem::create_directory(dirPath); |
|---|
| 224 | } catch (...) { |
|---|
| 225 | throw CF::FileException (CF::CFENFILE, "[FileSystem::mkdir] Failed to create directory"); |
|---|
| 226 | } |
|---|
| 227 | |
|---|
| 228 | } |
|---|
| 229 | |
|---|
| 230 | |
|---|
| 231 | void FileSystem_impl::rmdir (const char *directoryName) throw (CORBA::SystemException, CF::FileException, CF::InvalidFileName) |
|---|
| 232 | { |
|---|
| 233 | DEBUG(6, FileSystem, "In rmdir with " << directoryName); |
|---|
| 234 | |
|---|
| 235 | if (!ossieSupport::isValidFileName(directoryName)) { |
|---|
| 236 | DEBUG(7, FileSystem, "rmdir passed bad directory name, throwing exception."); |
|---|
| 237 | throw CF::InvalidFileName (CF::CFEINVAL, "[FileSystem::rmdir] Invalid directory name"); |
|---|
| 238 | } |
|---|
| 239 | |
|---|
| 240 | boost::filesystem::path dirPath(root / directoryName); |
|---|
| 241 | |
|---|
| 242 | boost::filesystem::remove(dirPath); |
|---|
| 243 | |
|---|
| 244 | #if 0///\todo Generate proper exceptions |
|---|
| 245 | if (check != 0) |
|---|
| 246 | throw CF::FileException (CF::CFENFILE, |
|---|
| 247 | "[FileSystem_impl::rmdir] Failed to remove directory\n"); |
|---|
| 248 | #endif |
|---|
| 249 | } |
|---|
| 250 | |
|---|
| 251 | |
|---|
| 252 | void FileSystem_impl::query (CF::Properties & fileSysProperties) throw (CORBA::SystemException, CF::FileSystem::UnknownFileSystemProperties) |
|---|
| 253 | { |
|---|
| 254 | DEBUG(6, FileSystem, "In query"); |
|---|
| 255 | #if 0 ///\todo Implement query operations |
|---|
| 256 | bool check; |
|---|
| 257 | |
|---|
| 258 | for (unsigned int i = 0; i < fileSysProperties.length (); i++) |
|---|
| 259 | { |
|---|
| 260 | check = false; |
|---|
| 261 | if (strcmp (fileSysProperties[i].id, CF::FileSystem::SIZE) == 0) |
|---|
| 262 | { |
|---|
| 263 | struct stat fileStat; |
|---|
| 264 | stat (root, &fileStat); |
|---|
| 265 | // fileSysProperties[i].value <<= fileStat.st_size; /// \bug FIXME |
|---|
| 266 | check = true; |
|---|
| 267 | } |
|---|
| 268 | if (strcmp (fileSysProperties[i].id, |
|---|
| 269 | CF::FileSystem::AVAILABLE_SIZE) == 0) |
|---|
| 270 | { |
|---|
| 271 | //to complete |
|---|
| 272 | } |
|---|
| 273 | if (!check) |
|---|
| 274 | throw CF::FileSystem::UnknownFileSystemProperties (); |
|---|
| 275 | } |
|---|
| 276 | #endif |
|---|
| 277 | } |
|---|
| 278 | |
|---|
| 279 | ///\todo Implement File object reference clean up. |
|---|