OpenFiles.cpp

См. документацию.
00001 /*
00002  * OpenFiles.cpp
00003  *
00004  *  Created on: 11.06.2011
00005  *      Author: nick
00006  */
00007 
00008 #include "OpenFiles.h"
00009 
00010 
00011 OpenFiles::OpenFiles(Server &protocol, ServerConfig* sc) {
00012     // TODO Auto-generated constructor stub
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                 /*(((fi.flags | O_RDWR) == O_RDWR) ||
00054                 ((fi.flags | O_WRONLY) == O_WRONLY) || ((fi.flags | O_RDWR) == O_APPEND)
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)) {//TO DOD разобр
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     // TODO Auto-generated destructor stub
00107     this->protocol->removeIQHandler(this);
00108 }