root/ossie/branches/philip-work/framework/FileManager_impl.cpp @ 3269

Revision 3269, 11.4 KB (checked in by balister, 6 years ago)

Pass some more test.

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