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