root/ossiedev/branches/jsnyder/trunk/system/ossie/framework/FileManager_impl.cpp @ 11064

Revision 11064, 15.3 KB (checked in by Snyder.Jason, 2 years ago)

committing patches from opencpi

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