fileManager.cpp 5.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221
  1. #include"fileManager.h"
  2. #include<pthread.h>
  3. #include <atomic>
  4. using namespace std;
  5. static pthread_mutex_t recvDelLock;
  6. static atomic<bool> recvDelPrelock(false);
  7. static atomic<int> recvUseCount(0);
  8. static pthread_mutex_t sendDelLock;
  9. static atomic<bool>sendDelPrelock(false);
  10. static atomic<int>sendUseCount(0);
  11. const char *(filetype_str[])={
  12. "<Unknown>","<CommonFile>","<Directory>",
  13. "<RtnParent>","<SoftLink>","<CharDev>",
  14. "<BlockDev>","<FIFO>","<Unknown>",
  15. "<Unknown>","<ResFork>"};
  16. FileManager* FileManager::instance=new FileManager();
  17. FileManager* FileManager::getInstance(){
  18. static bool flgfirst=true;
  19. if(flgfirst){
  20. pthread_mutex_init(&recvDelLock,NULL);
  21. pthread_mutex_init(&sendDelLock,NULL);
  22. }
  23. return instance;
  24. }
  25. int FileManager::Addrecv(recvfile_t& tar){
  26. bool correctTime=false;
  27. while(correctTime==false){
  28. while(recvDelPrelock==true);
  29. recvUseCount++;
  30. if(recvDelPrelock==true){
  31. recvUseCount--;
  32. }else{
  33. correctTime=true;
  34. }
  35. }
  36. auto i=recvls.begin();
  37. auto z=recvls.end();
  38. bool nofound=true;
  39. for(;i!=z;i++){
  40. if((i->fileno==tar.fileno)&&(i->tarIp==tar.tarIp)){
  41. nofound=false;
  42. break;
  43. }
  44. }
  45. if(nofound){
  46. recvUseCount--;
  47. recvls.push_back(tar);
  48. return FUNCSUCCEED;
  49. }else{
  50. *i=tar;
  51. recvUseCount--;
  52. return FUNCFAILED;
  53. }
  54. }
  55. int FileManager::Delrecv(unsigned tarno,unsigned tarIp){
  56. pthread_mutex_lock(&recvDelLock);
  57. recvDelPrelock=true;
  58. while(recvUseCount!=0);
  59. auto i=recvls.begin();
  60. auto z=recvls.end();
  61. bool nofound=true;
  62. for(;i!=z;i++){
  63. if((i->fileno==tarno)&&(i->tarIp==tarIp)){
  64. nofound=false;
  65. break;
  66. }
  67. }
  68. if(nofound==false){
  69. i=recvls.erase(i);
  70. }
  71. recvDelPrelock=false;
  72. pthread_mutex_unlock(&recvDelLock);
  73. return nofound==false?FUNCSUCCEED:FUNCFAILED;
  74. }
  75. int FileManager::Findrecv(unsigned tarno,unsigned tarIp,recvfile_t& tar){
  76. bool correctTime=false;
  77. while(correctTime==false){
  78. while(recvDelPrelock==true);
  79. recvUseCount++;
  80. if(recvDelPrelock==true){
  81. recvUseCount--;
  82. }else{
  83. correctTime=true;
  84. }
  85. }
  86. auto i=recvls.begin();
  87. auto z=recvls.end();
  88. bool nofound=true;
  89. for(;i!=z;i++){
  90. if((i->fileno==tarno)&&(i->tarIp==tarIp)){
  91. nofound=false;
  92. break;
  93. }
  94. }
  95. if(nofound){
  96. recvUseCount--;
  97. return FUNCFAILED;
  98. }else{
  99. tar=*i;
  100. recvUseCount--;
  101. return FUNCSUCCEED;
  102. }
  103. }
  104. int FileManager::GetrecvCopy(list<recvfile_t>& src){
  105. bool correctTime=false;
  106. while(correctTime==false){
  107. while(recvDelPrelock==true);
  108. recvUseCount++;
  109. if(recvDelPrelock==true){
  110. recvUseCount--;
  111. }else{
  112. correctTime=true;
  113. }
  114. }
  115. std::copy(recvls.begin(),recvls.end(),std::back_inserter(src));
  116. recvUseCount--;
  117. }
  118. int FileManager::Addsend(sendfile_t& tar){
  119. bool correctTime=false;
  120. while(correctTime==false){
  121. while(sendDelPrelock==true);
  122. sendUseCount++;
  123. if(sendDelPrelock==true){
  124. sendUseCount--;
  125. }else{
  126. correctTime=true;
  127. }
  128. }
  129. auto i=sendls.begin();
  130. auto z=sendls.end();
  131. bool nofound=true;
  132. for(;i!=z;i++){
  133. if((i->fileno==tar.fileno)&&(i->tarIp==tar.tarIp)){
  134. nofound=false;
  135. break;
  136. }
  137. }
  138. if(nofound){
  139. sendUseCount--;
  140. sendls.push_back(tar);
  141. return FUNCSUCCEED;
  142. }else{
  143. sendUseCount--;
  144. return FUNCFAILED;
  145. }
  146. }
  147. int FileManager::Delsend(unsigned tarno,unsigned tarIp){
  148. pthread_mutex_lock(&sendDelLock);
  149. sendDelPrelock=true;
  150. while(sendUseCount!=0);
  151. auto i=sendls.begin();
  152. auto z=sendls.end();
  153. bool nofound=true;
  154. for(;i!=z;i++){
  155. if((i->fileno==tarno)&&(i->tarIp==tarIp)){
  156. nofound=false;
  157. break;
  158. }
  159. }
  160. if(nofound==false){
  161. i=sendls.erase(i);
  162. }
  163. sendDelPrelock=false;
  164. pthread_mutex_unlock(&sendDelLock);
  165. return nofound==false?FUNCSUCCEED:FUNCFAILED;
  166. }
  167. int FileManager::Findsend(unsigned tarno,unsigned tarIp,sendfile_t& tar){
  168. bool correctTime=false;
  169. while(correctTime==false){
  170. while(sendDelPrelock==true);
  171. sendUseCount++;
  172. if(sendDelPrelock==true){
  173. sendUseCount--;
  174. }else{
  175. correctTime=true;
  176. }
  177. }
  178. auto i=sendls.begin();
  179. auto z=sendls.end();
  180. bool nofound=true;
  181. for(;i!=z;i++){
  182. if((i->fileno==tarno)&&(i->tarIp==tarIp)){
  183. nofound=false;
  184. break;
  185. }
  186. }
  187. if(nofound){
  188. sendUseCount--;
  189. return FUNCFAILED;
  190. }else{
  191. tar=*i;
  192. sendUseCount--;
  193. return FUNCSUCCEED;
  194. }
  195. }
  196. int FileManager::GetsendCopy(list<sendfile_t>& src){
  197. bool correctTime=false;
  198. while(correctTime==false){
  199. while(sendDelPrelock==true);
  200. sendUseCount++;
  201. if(sendDelPrelock==true){
  202. sendUseCount--;
  203. }else{
  204. correctTime=true;
  205. }
  206. }
  207. std::copy(sendls.begin(),sendls.end(),std::back_inserter(src));
  208. sendUseCount--;
  209. }