См. документацию.00001
00002
00003
00004
00005
00006
00007
00008 #include <errno.h>
00009
00010 #include "ReadFiles.h"
00011
00012 ReadFiles::ReadFiles(Protocol &protocol, ServerConfig* sc, BinaryProtocol* bp) {
00013 this->sc = sc;
00014 this->bp = bp;
00015 this->protocol = &protocol;
00016 this->max_readsize=sc->getModuleAttributeInt(L"lib_file_opertion",L"max_read_size",409600);
00017 cout<<"max_readsize"<<max_readsize<<endl;
00018 protocol.addIQHandler(this, ReadFile_xmlns);
00019 }
00020
00021 void ReadFiles::handleTag(TagIQ &tag) {
00022
00023 TagIQ::IQType t = tag.getType();
00024
00025 #ifdef debug_output
00026 cout << "STEP1 read" << endl;
00027 #endif
00028 if (t == TagIQ::Set) {
00029
00030 string path;
00031 size_t size;
00032 size_t offset;
00033 fuse_file_info fi;
00034
00035 int fDesc;
00036 struct stat fb;
00037
00038
00039
00040 ReadFile rf(tag);
00041 #ifdef debug_output
00042 cout << "STEP2 read" << endl;
00043 #endif
00044
00045 ReadFile rfResp(NULL, "server", "client", TagIQ::Get, tag.getID());
00046 rf.getFileData(path, offset, size, fi);
00047 #ifdef debug_output
00048 cout << "path:" << path << " offset:" << offset << "size:" << size << endl;
00049 #endif
00050 string dpath = sc->getSharedDir() + path;
00051
00052 fDesc = open(dpath.c_str(), O_RDONLY);
00053 if (fDesc == -1) {
00054 cout << "erroropenfile!" << errno << endl;
00055 cout << "filename" << dpath << endl;
00056 }
00057
00058 if (fstat(fDesc, &fb) == -1) {
00059 cout << "fstat error" << errno << endl;
00060 }
00061 size_t szforread = fb.st_size - offset;
00062 #ifdef debug_output
00063 cout << "szforread" << szforread << "file size" << fb.st_size << endl;
00064 #endif
00065 if (szforread < 0) {
00066 #ifdef debug_output
00067 cout << "ELSE1 READ" << endl;
00068 #endif
00069 szforread = 0;
00070 } else if (size < szforread) {
00071 #ifdef debug_output
00072 cout << "ELSE2 READ" << endl;
00073 #endif
00074 szforread = size;
00075 }
00076 #ifdef debug_output
00077 cout << "STEP3 read" << endl;
00078 #endif
00079 unsigned long int pktID = bp->getNextId();
00080 rfResp.setResult(pktID, szforread);
00081 if (szforread == 0) {
00082 protocol->sendNode(rfResp);
00083 } else {
00084
00085 #ifdef debug_output
00086 cout << "lalal1" << endl;
00087 #endif
00088 TagIQ rt = protocol->sendIQResp(rfResp, ReadFile_xmlns, QUERY_TAG);
00089 #ifdef debug_output
00090 cout << "!!!!lalalal!!! namespace:" << rt.getNamesapace() << "typeIQ:" << rt.getType() << endl;
00091 #endif
00092 if ((rt.getKind(ReadFile_xmlns) != NULL) && (rt.getType() == TagIQ::Result)) {
00093
00094
00095
00096
00097
00098
00099
00100
00101
00102
00103
00104
00105
00106
00107
00108
00109
00110
00111
00112 #ifdef debug_output
00113 cout << "OK, sending file data" << endl;
00114 #endif
00115
00116
00117
00118
00119 size_t rzize = szforread;
00120 if (rzize > max_readsize) {
00121 rzize = max_readsize;
00122 }
00123
00124
00125 void* buf = malloc(rzize);
00126
00127 lseek(fDesc, offset, SEEK_SET);
00128
00129
00130 size_t rzizeall = szforread;
00131 while (rzizeall > 0) {
00132
00133 char* buf1 = (char*) buf;
00134 ssize_t sr = 0;
00135 ssize_t sr1 = rzize;
00136 do {
00137 #ifdef debug_output
00138 cout << "цикл1" << endl;
00139 #endif
00140 sr = read(fDesc, buf1, sr1);
00141 if (sr != -1) {
00142 sr1 -= sr;
00143 buf1 += sr;
00144 }
00145 } while ((sr1 > 0) && (sr > 0));
00146
00147
00148 bp->setSendBufferData(buf, rzize - sr1, pktID);
00149 rzizeall -= (rzize - sr1);
00150 #ifdef debug_output
00151 cout << "sr1" << sr1 << endl;
00152 #endif
00153 if (sr == -1) {
00154 cout << "ОШИБКА ЧТЕНИЯ!" << errno << endl;
00155
00156 break;
00157 }
00158 if (sr == 0) {
00159 cout << "Конец файла!" << endl;
00160 break;
00161 }
00162 #ifdef debug_output
00163 cout << "цикл2" << endl;
00164 #endif
00165 }
00166 free(buf);
00167 } else {
00168 cout << "!!Нет ответа от клиента о подетверждении передачи содержимого!!!!" << endl;
00169 }
00170 }
00171 close(fDesc);
00172 #ifdef debug_output
00173 cout << "STEPFINAL read" << endl;
00174 #endif
00175 }
00176 #ifdef debug_output
00177 cout << "STEP END read" << endl;
00178 #endif
00179 }
00180
00181 ReadFiles::ReadFiles() {
00182
00183
00184 }
00185
00186 ReadFiles::~ReadFiles() {
00187
00188 this->protocol->removeIQHandler(this);
00189 }