00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022 #include "fileserver.h"
00023 #include <salt/fileclasses.h>
00024 #include <zeitgeist/logserver/logserver.h>
00025 #include <zeitgeist/core.h>
00026
00027 using namespace boost;
00028 using namespace salt;
00029 using namespace zeitgeist;
00030 using namespace std;
00031
00032 FileServer::FileServer() : Node(), mNextHandle(1)
00033 {
00034 }
00035
00036 FileServer::~FileServer()
00037 {
00038 }
00039
00040 shared_ptr<salt::RFile> FileServer::Open(const string& inName)
00041 {
00042 for (TLeafList::iterator i = mChildren.begin(); i != mChildren.end(); ++i)
00043 {
00044 shared_ptr<FileSystem> fileSys = shared_static_cast<FileSystem>(*i);
00045
00046 shared_ptr<salt::RFile> file(fileSys->Open(inName));
00047
00048
00049 if(file.get() != 0)
00050 {
00051 return file;
00052 }
00053 }
00054
00055
00056 shared_ptr<salt::RFile> file(new StdFile());
00057 if (! file->Open(inName.c_str()))
00058 {
00059 file.reset();
00060 }
00061
00062 return file;
00063 }
00064
00065 FileServer::THandle FileServer::Register(const string& inName)
00066 {
00067 shared_ptr<salt::RFile> file = Open(inName);
00068
00069 if (file.get() == 0)
00070 {
00071 return 0;
00072 }
00073
00074 mFileMap[mNextHandle] = file;
00075 THandle h = mNextHandle;
00076 ++mNextHandle;
00077
00078 return h;
00079 }
00080
00081 shared_ptr<salt::RFile> FileServer::Get(THandle handle) const
00082 {
00083 TFileMap::const_iterator iter = mFileMap.find(handle);
00084
00085 if (iter == mFileMap.end())
00086 {
00087 GetLog()->Warning()
00088 << "(FileServer::Get) Warning: Unknown file handle "
00089 << handle << "\n";
00090 return shared_ptr<salt::RFile>();
00091 }
00092
00093 return (*iter).second;
00094 }
00095
00096 void FileServer::Close(THandle handle)
00097 {
00098 TFileMap::iterator iter = mFileMap.find(handle);
00099
00100 if (iter == mFileMap.end())
00101 {
00102 GetLog()->Warning()
00103 << "(FileServer::Close) Warning: Unknown file handle "
00104 << handle << "\n";
00105 return;
00106 }
00107
00108 mFileMap.erase(iter);
00109
00110 if (mFileMap.empty())
00111 {
00112
00113 mNextHandle = 1;
00114 }
00115 }
00116
00117 void FileServer::OnUnlink()
00118 {
00119 if (! mFileMap.empty())
00120 {
00121 GetLog()->Warning() << "(FileServer) There are "
00122 << mFileMap.size()
00123 << " files left in the registry\n";
00124 }
00125 }
00126
00127 bool FileServer::Exist(const string& inName)
00128 {
00129 return (Open(inName).get() != 0);
00130 }
00131
00132
00133 bool FileServer::Mount(const string& inFileSysName, const string& inPath)
00134 {
00135 shared_ptr<FileSystem> fileSys =
00136 shared_static_cast<FileSystem>(GetChild(inPath));
00137
00138 if (fileSys)
00139 {
00140
00141 if (fileSys->GetClass()->GetName().compare(inFileSysName) == 0)
00142 {
00143
00144 return true;
00145 }
00146 else
00147 {
00148
00149 GetLog()->Error()
00150 << "(FileServer) ERROR: a FileSystem is already mounted a "
00151 << inPath << "\n";
00152 return false;
00153 }
00154 }
00155
00156
00157 fileSys = shared_static_cast<FileSystem>(GetCore()->New(inFileSysName));
00158
00159 if (
00160 (fileSys.get() == 0) ||
00161 (! fileSys->SetPath(inPath))
00162 )
00163 {
00164 return false;
00165 }
00166
00167
00168 AddChildReference(fileSys);
00169
00170 GetLog()->Normal() <<
00171 "(FileServer) successfully mounted a '"
00172 << inFileSysName << "' at '"
00173 << inPath << "'\n";
00174 return true;
00175 }
00176
00177 bool FileServer::Unmount(const string& inPath)
00178 {
00179
00180 if (Unmount ("FileSystemSTD", inPath))
00181 {
00182 return true;
00183 }
00184
00185 shared_ptr<Leaf> leaf = GetChild(inPath);
00186
00187 if(leaf)
00188 {
00189 leaf->Unlink();
00190 return true;
00191 }
00192
00193 return false;
00194 }
00195
00196 bool FileServer::Unmount(const string& inFileSysName, const string& inPath)
00197 {
00198 shared_ptr<FileSystem> fileSystem = shared_static_cast<FileSystem>(GetChild(inPath));
00199
00200 if(fileSystem)
00201 {
00202 if (fileSystem->GetClass()->GetName().compare(inFileSysName) == 0)
00203 {
00204 fileSystem->Unlink();
00205 return true;
00206 }
00207 }
00208
00209 return false;
00210 }
00211
00212 int FileServer::ForEachFile(const string& , const string& ,
00213 const string& ,
00214 FileSystem::TCallback , void* )
00215 {
00216 int count = 0;
00217
00218
00219
00220
00221
00222 return count;
00223 }
00224