| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221 |
- #include"fileManager.h"
- #include<pthread.h>
- #include <atomic>
- using namespace std;
- static pthread_mutex_t recvDelLock;
- static atomic<bool> recvDelPrelock(false);
- static atomic<int> recvUseCount(0);
- static pthread_mutex_t sendDelLock;
- static atomic<bool>sendDelPrelock(false);
- static atomic<int>sendUseCount(0);
- const char *(filetype_str[])={
- "<Unknown>","<CommonFile>","<Directory>",
- "<RtnParent>","<SoftLink>","<CharDev>",
- "<BlockDev>","<FIFO>","<Unknown>",
- "<Unknown>","<ResFork>"};
- FileManager* FileManager::instance=new FileManager();
- FileManager* FileManager::getInstance(){
- static bool flgfirst=true;
- if(flgfirst){
- pthread_mutex_init(&recvDelLock,NULL);
- pthread_mutex_init(&sendDelLock,NULL);
- }
- return instance;
- }
- int FileManager::Addrecv(recvfile_t& tar){
- bool correctTime=false;
- while(correctTime==false){
- while(recvDelPrelock==true);
- recvUseCount++;
- if(recvDelPrelock==true){
- recvUseCount--;
- }else{
- correctTime=true;
- }
- }
- auto i=recvls.begin();
- auto z=recvls.end();
- bool nofound=true;
- for(;i!=z;i++){
- if((i->fileno==tar.fileno)&&(i->tarIp==tar.tarIp)){
- nofound=false;
- break;
- }
- }
- if(nofound){
- recvUseCount--;
- recvls.push_back(tar);
- return FUNCSUCCEED;
- }else{
- *i=tar;
- recvUseCount--;
- return FUNCFAILED;
- }
- }
- int FileManager::Delrecv(unsigned tarno,unsigned tarIp){
- pthread_mutex_lock(&recvDelLock);
- recvDelPrelock=true;
- while(recvUseCount!=0);
- auto i=recvls.begin();
- auto z=recvls.end();
- bool nofound=true;
- for(;i!=z;i++){
- if((i->fileno==tarno)&&(i->tarIp==tarIp)){
- nofound=false;
- break;
- }
- }
- if(nofound==false){
- i=recvls.erase(i);
- }
- recvDelPrelock=false;
- pthread_mutex_unlock(&recvDelLock);
- return nofound==false?FUNCSUCCEED:FUNCFAILED;
- }
- int FileManager::Findrecv(unsigned tarno,unsigned tarIp,recvfile_t& tar){
- bool correctTime=false;
- while(correctTime==false){
- while(recvDelPrelock==true);
- recvUseCount++;
- if(recvDelPrelock==true){
- recvUseCount--;
- }else{
- correctTime=true;
- }
- }
- auto i=recvls.begin();
- auto z=recvls.end();
- bool nofound=true;
- for(;i!=z;i++){
- if((i->fileno==tarno)&&(i->tarIp==tarIp)){
- nofound=false;
- break;
- }
- }
- if(nofound){
- recvUseCount--;
- return FUNCFAILED;
- }else{
- tar=*i;
- recvUseCount--;
- return FUNCSUCCEED;
- }
- }
- int FileManager::GetrecvCopy(list<recvfile_t>& src){
- bool correctTime=false;
- while(correctTime==false){
- while(recvDelPrelock==true);
- recvUseCount++;
- if(recvDelPrelock==true){
- recvUseCount--;
- }else{
- correctTime=true;
- }
- }
- std::copy(recvls.begin(),recvls.end(),std::back_inserter(src));
- recvUseCount--;
- }
- int FileManager::Addsend(sendfile_t& tar){
- bool correctTime=false;
- while(correctTime==false){
- while(sendDelPrelock==true);
- sendUseCount++;
- if(sendDelPrelock==true){
- sendUseCount--;
- }else{
- correctTime=true;
- }
- }
- auto i=sendls.begin();
- auto z=sendls.end();
- bool nofound=true;
- for(;i!=z;i++){
- if((i->fileno==tar.fileno)&&(i->tarIp==tar.tarIp)){
- nofound=false;
- break;
- }
- }
- if(nofound){
- sendUseCount--;
- sendls.push_back(tar);
- return FUNCSUCCEED;
- }else{
- sendUseCount--;
- return FUNCFAILED;
- }
- }
- int FileManager::Delsend(unsigned tarno,unsigned tarIp){
- pthread_mutex_lock(&sendDelLock);
- sendDelPrelock=true;
- while(sendUseCount!=0);
- auto i=sendls.begin();
- auto z=sendls.end();
- bool nofound=true;
- for(;i!=z;i++){
- if((i->fileno==tarno)&&(i->tarIp==tarIp)){
- nofound=false;
- break;
- }
- }
- if(nofound==false){
- i=sendls.erase(i);
- }
- sendDelPrelock=false;
- pthread_mutex_unlock(&sendDelLock);
- return nofound==false?FUNCSUCCEED:FUNCFAILED;
- }
- int FileManager::Findsend(unsigned tarno,unsigned tarIp,sendfile_t& tar){
- bool correctTime=false;
- while(correctTime==false){
- while(sendDelPrelock==true);
- sendUseCount++;
- if(sendDelPrelock==true){
- sendUseCount--;
- }else{
- correctTime=true;
- }
- }
- auto i=sendls.begin();
- auto z=sendls.end();
- bool nofound=true;
- for(;i!=z;i++){
- if((i->fileno==tarno)&&(i->tarIp==tarIp)){
- nofound=false;
- break;
- }
- }
- if(nofound){
- sendUseCount--;
- return FUNCFAILED;
- }else{
- tar=*i;
- sendUseCount--;
- return FUNCSUCCEED;
- }
- }
- int FileManager::GetsendCopy(list<sendfile_t>& src){
- bool correctTime=false;
- while(correctTime==false){
- while(sendDelPrelock==true);
- sendUseCount++;
- if(sendDelPrelock==true){
- sendUseCount--;
- }else{
- correctTime=true;
- }
- }
- std::copy(sendls.begin(),sendls.end(),std::back_inserter(src));
- sendUseCount--;
- }
|