См. документацию.00001
00002
00003
00004
00005
00006
00007
00008 #include "OpenFiles.h"
00009
00010
00011 OpenFiles::OpenFiles(Server &protocol, ServerConfig* sc) {
00012
00013 this->sc = sc;
00014 this->protocol = &protocol;
00015 protocol.addIQHandler(this, OpenFile_xmlns);
00016 }
00017
00018 void OpenFiles::handleTag(TagIQ &tag) {
00019 if (tag.getType() == TagIQ::Get) {
00020 OpenFile t(tag);
00021 string dir;
00022 fuse_file_info fi;
00023 #ifdef debug_output
00024 cout << "GET FILE!!!" << endl;
00025 #endif
00026 mode_t mode;
00027 t.getFile(dir, fi,mode);
00028 string d(dir);
00029 #ifdef debug_output
00030 cout << "GET FILE OK!!!" << endl;
00031 #endif
00032 pthread_mutex_t* openedFiles_mutex=GlobalData::get_Instance()->get_openedFiles_mutex();
00033 pthread_mutex_lock(openedFiles_mutex);
00034
00035 map<string,OpenedFilesInfo>* openedFiles=GlobalData::get_Instance()->get_OpenedFiles();
00036 map<string, OpenedFilesInfo>::iterator it = openedFiles->find(d);
00037 if (it == openedFiles->end()) {
00038 cout<<"Открываем новый файл и вставляем его в список "<<d<<endl;
00039 OpenedFilesInfo oi;
00040 oi.flags = 0;
00041 it = openedFiles->insert(make_pair(d, oi)).first;
00042 }
00043 bool is_write=(((fi.flags & O_RDWR) == O_RDWR) || ((fi.flags & O_WRONLY) == O_WRONLY) || ((fi.flags & O_RDWR) == O_APPEND));
00044 if(is_write){
00045 cout<<"Открываем файл на запись"<<endl;
00046 }
00047 int resultO;
00048 if (
00049 (((it->second.flags & O_RDWR) == O_RDWR) || ((it->second.flags & O_WRONLY) == O_WRONLY) ||
00050 ((it->second.flags & O_APPEND) == O_APPEND)) ||
00051
00052 ((it->second.file_info.begin() != it->second.file_info.end()) && is_write)
00053
00054
00055
00056
00057 ) {
00058 cout<<"Файл уже открыт для записи, и мы пытаемся открыть еще раз - нельзя"<<endl;
00059 resultO = -1;
00060 } else {
00061 cout<<"Открываем файл 1"<<endl;
00062 int fd=0;
00063 if (is_write){
00064 cout<<"Собираемся писать в файл, но ранее он открыт небыл 1"<<endl;
00065 string path=sc->getSharedDir()+dir;
00066 fd = open(path.data(), fi.flags,mode);
00067 if (fd > 0) {
00068 cout<<"Файл открыть на запись удалось"<<endl;
00069 close(fd);
00070 }else{
00071 resultO=fd;
00072 cout<<"Файл открыть на запись не удалось"<<endl;
00073
00074 if (it->second.flags==0){
00075 cout<<"Файл открыть на запись не удалось, Открыт первый раз, удаляем файл из списка открытых"<<endl;
00076 openedFiles->erase(it);
00077 }
00078 }
00079 }
00080 if ((fd > 0)||(!is_write)) {
00081 cout<<"Файл удалось открыть, для записи, либо открытие для чтения"<<endl;
00082 resultO = 0;
00083 map<unsigned int, unsigned int>::iterator itf = it->second.file_info.find(protocol->getClientID());
00084 if (itf == it->second.file_info.end()) {
00085 itf = it->second.file_info.insert(make_pair(protocol->getClientID(), 0)).first;
00086 }
00087 string path = sc->getSharedDir() + dir;
00088 if (it->second.flags == 0) {
00089 cout<<"Если флаги 0, то открытие в первый раз присваиваем флаги"<<endl;
00090 it->second.flags = fi.flags;
00091 }
00092 cout<<"Увеличиваем счетчик файлов"<<endl;
00093 itf->second++;
00094 }
00095
00096 }
00097 cout<<"Результат открыия"<<resultO<<endl;
00098 pthread_mutex_unlock(openedFiles_mutex);
00099 OpenFile resof(protocol->getCurNodeSend(), "server", tag.getFrom(), TagIQ::Result, tag.getID());
00100 resof.setResult(resultO);
00101 protocol->sendNode(resof);
00102 }
00103 }
00104
00105 OpenFiles::~OpenFiles() {
00106
00107 this->protocol->removeIQHandler(this);
00108 }