1 /*
2 * Copyright (C) 2021 Huawei Device Co., Ltd.
3 * Licensed under the Apache License, Version 2.0 (the "License");
4 * you may not use this file except in compliance with the License.
5 * You may obtain a copy of the License at
6 *
7 * http://www.apache.org/licenses/LICENSE-2.0
8 *
9 * Unless required by applicable law or agreed to in writing, software
10 * distributed under the License is distributed on an "AS IS" BASIS,
11 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12 * See the License for the specific language governing permissions and
13 * limitations under the License.
14 */
15
16 #include "nstackx_file_manager.h"
17 #include "nstackx_dfile_config.h"
18 #include "nstackx_dfile_session.h"
19 #include "nstackx_error.h"
20 #include "nstackx_event.h"
21 #include "nstackx_file_manager_client.h"
22 #include "nstackx_log.h"
23 #ifdef MBEDTLS_INCLUDED
24 #include "nstackx_mbedtls.h"
25 #else
26 #include "nstackx_openssl.h"
27 #endif
28 #include "nstackx_util.h"
29 #include "nstackx_dev.h"
30 #include "securec.h"
31
32 #define TAG "nStackXDFile"
33
34 static FileRecvState FileGetRecvStatus(FileInfo *fileInfo);
35
36 typedef struct {
37 FileManagerMsgReceiver msgReceiver;
38 FileManagerMsgType msgType;
39 int32_t errCode;
40 void *context;
41 } FileManagerMsgCtx;
42
43 typedef struct {
44 FileListMsgReceiver msgReceiver;
45 FileManagerMsgType msgType;
46 uint16_t fileId;
47 FileManagerMsg msg;
48 void *context;
49 uint16_t transId;
50 } FileListMsgCtx;
51
NotifyFileManagerMsgInner(void * arg)52 static void NotifyFileManagerMsgInner(void *arg)
53 {
54 FileManagerMsgCtx *ctx = arg;
55 ctx->msgReceiver(ctx->msgType, ctx->errCode, ctx->context);
56 free(ctx);
57 return;
58 }
59
NotifyFileListMsgInner(void * arg)60 static void NotifyFileListMsgInner(void *arg)
61 {
62 FileListMsgCtx *ctx = arg;
63 ctx->msgReceiver(ctx->fileId, ctx->msgType, &ctx->msg, ctx->context, ctx->transId);
64 free(ctx);
65 return;
66 }
67
NotifyFileManagerMsg(const FileManager * fileManager,FileManagerMsgType msgType)68 void NotifyFileManagerMsg(const FileManager *fileManager, FileManagerMsgType msgType)
69 {
70 FileManagerMsgCtx *ctx = NULL;
71 if (fileManager->msgReceiver == NULL || !IsEpollDescValid(fileManager->epollfd)) {
72 return;
73 }
74 ctx = (FileManagerMsgCtx *)calloc(1, sizeof(FileManagerMsgCtx));
75 if (ctx == NULL) {
76 return;
77 }
78 ctx->msgReceiver = fileManager->msgReceiver;
79 ctx->msgType = msgType;
80 ctx->context = fileManager->context;
81 ctx->errCode = fileManager->errCode;
82 if (PostEvent(fileManager->eventNodeChain, fileManager->epollfd, NotifyFileManagerMsgInner, ctx) != NSTACKX_EOK) {
83 free(ctx);
84 return;
85 }
86 }
87
NotifyFileListMsg(const FileListTask * fileList,FileManagerMsgType msgType)88 void NotifyFileListMsg(const FileListTask *fileList, FileManagerMsgType msgType)
89 {
90 FileListMsgCtx *ctx = NULL;
91 uint64_t bytesTransferred;
92 if (fileList == NULL) {
93 LOGE(TAG, "NotifyFileListMsg fileList error");
94 return;
95 }
96
97 if (fileList->msgReceiver == NULL || !IsEpollDescValid(fileList->epollfd)) {
98 return;
99 }
100 ctx = (FileListMsgCtx *)calloc(1, sizeof(FileListMsgCtx));
101 if (ctx == NULL) {
102 return;
103 }
104 if (msgType == FILE_MANAGER_TRANS_IN_PROGRESS) {
105 if (fileList->bytesTransferredLastRecord >= fileList->totalBytes) {
106 free(ctx);
107 return;
108 }
109 bytesTransferred = fileList->bytesTransferredLastRecord;
110 ctx->msg.transferUpdate.bytesTransferred = bytesTransferred;
111 ctx->msg.transferUpdate.totalBytes = fileList->totalBytes;
112 ctx->msg.transferUpdate.transId = fileList->transId;
113 }
114
115 ctx->msgReceiver = fileList->msgReceiver;
116 ctx->fileId = NSTACKX_RESERVED_FILE_ID;
117 ctx->msgType = msgType;
118 ctx->msg.errorCode = fileList->errCode;
119 ctx->context = fileList->context;
120 ctx->transId = fileList->transId;
121 if (PostEvent(fileList->eventNodeChain, fileList->epollfd, NotifyFileListMsgInner, ctx) != NSTACKX_EOK) {
122 free(ctx);
123 return;
124 }
125 }
126
NotifyFileMsg(const FileListTask * fileList,uint16_t fileId,FileManagerMsgType msgType)127 void NotifyFileMsg(const FileListTask *fileList, uint16_t fileId, FileManagerMsgType msgType)
128 {
129 FileListMsgCtx *ctx = NULL;
130 if (fileList->msgReceiver == NULL || !IsEpollDescValid(fileList->epollfd) || fileId == 0 ||
131 fileId > fileList->fileNum) {
132 return;
133 }
134 ctx = (FileListMsgCtx *)calloc(1, sizeof(FileListMsgCtx));
135 if (ctx == NULL) {
136 return;
137 }
138 ctx->msgReceiver = fileList->msgReceiver;
139 ctx->fileId = fileId;
140 ctx->msgType = msgType;
141 ctx->msg.errorCode = fileList->fileInfo[fileId - 1].errCode;
142 ctx->context = fileList->context;
143 ctx->transId = fileList->transId;
144 if (PostEvent(fileList->eventNodeChain, fileList->epollfd, NotifyFileListMsgInner, ctx) != NSTACKX_EOK) {
145 free(ctx);
146 return;
147 }
148 }
149
ConvertErrCode(int32_t error)150 int32_t ConvertErrCode(int32_t error)
151 {
152 switch (error) {
153 case ENOSPC:
154 return FILE_MANAGER_FILE_ENOSPC;
155 case ENOMEM:
156 return FILE_MANAGER_FILE_ENOMEM;
157 case ENFILE:
158 return FILE_MANAGER_FILE_ENFILE;
159 case EACCES:
160 return FILE_MANAGER_FILE_EACCES;
161 case ENAMETOOLONG:
162 return FILE_MANAGER_FILE_ENAMETOOLONG;
163 case ETXTBSY:
164 return FILE_MANAGER_FILE_ETXTBSY;
165 default:
166 return FILE_MANAGER_FILE_EOTHER;
167 }
168 }
169
MutexListInit(MutexList * mutexList,uint32_t maxSize)170 int32_t MutexListInit(MutexList *mutexList, uint32_t maxSize)
171 {
172 if (mutexList == NULL || maxSize == 0) {
173 LOGE(TAG, "list with lock dosn't exist of maxSize if zero");
174 return NSTACKX_EINVAL;
175 }
176 (void)memset_s(mutexList, sizeof(MutexList), 0, sizeof(MutexList));
177 if (PthreadMutexInit(&mutexList->lock, NULL) != 0) {
178 LOGE(TAG, "PthreadMutexInit error");
179 return NSTACKX_EFAILED;
180 }
181 ListInitHead(&mutexList->head);
182 mutexList->maxSize = maxSize;
183 return NSTACKX_EOK;
184 }
185
MutexListDestory(MutexList * mutexList)186 void MutexListDestory(MutexList *mutexList)
187 {
188 PthreadMutexDestroy(&mutexList->lock);
189 }
190
MutexListAddNode(MutexList * mutexList,List * element,uint8_t isFront)191 int32_t MutexListAddNode(MutexList *mutexList, List *element, uint8_t isFront)
192 {
193 int32_t ret;
194 if (PthreadMutexLock(&mutexList->lock) != 0) {
195 LOGE(TAG, "pthread mutex lock error");
196 return NSTACKX_EFAILED;
197 }
198 if (mutexList->size == mutexList->maxSize) {
199 LOGE(TAG, "list is full");
200 ret = NSTACKX_EFAILED;
201 } else {
202 if (isFront) {
203 ListInsertHead(&mutexList->head, element);
204 } else {
205 ListInsertTail(&mutexList->head, element);
206 }
207 ret = NSTACKX_EOK;
208 mutexList->size++;
209 }
210 if (PthreadMutexUnlock(&mutexList->lock) != 0) {
211 LOGE(TAG, "pthread mutex unlock error");
212 return NSTACKX_EFAILED;
213 }
214 return ret;
215 }
216
MutexListPopFront(MutexList * mutexList,List ** curFront,uint8_t * isPoped)217 int32_t MutexListPopFront(MutexList *mutexList, List **curFront, uint8_t *isPoped)
218 {
219 int32_t ret;
220 *isPoped = NSTACKX_FALSE;
221 if (PthreadMutexLock(&mutexList->lock) != 0) {
222 LOGE(TAG, "pthread mutex lock error");
223 return NSTACKX_EFAILED;
224 }
225 if (mutexList->size == 0) {
226 ret = NSTACKX_EFAILED;
227 } else {
228 *curFront = ListPopFront(&mutexList->head);
229 mutexList->size--;
230 *isPoped = NSTACKX_TRUE;
231 ret = NSTACKX_EOK;
232 }
233 if (PthreadMutexUnlock(&mutexList->lock) != 0) {
234 LOGE(TAG, "pthread mutex unlock error");
235 return NSTACKX_EFAILED;
236 }
237 return ret;
238 }
239
PrepareOneTaskByStatus(FileManager * fileManager,uint32_t runStatus,uint8_t * isErrorOccured)240 static FileListTask *PrepareOneTaskByStatus(FileManager *fileManager, uint32_t runStatus, uint8_t *isErrorOccured)
241 {
242 List *pos = NULL;
243 List *tmp = NULL;
244 FileListTask *fileList = NULL;
245 uint8_t isFound = NSTACKX_FALSE;
246 *isErrorOccured = NSTACKX_FALSE;
247
248 if (fileManager == NULL) {
249 return NULL;
250 }
251 if (PthreadMutexLock(&fileManager->taskList.lock) != 0) {
252 LOGE(TAG, "pthread mutex lock error");
253 *isErrorOccured = NSTACKX_TRUE;
254 return NULL;
255 }
256 LIST_FOR_EACH_SAFE(pos, tmp, &fileManager->taskList.head) {
257 fileList = (FileListTask *)pos;
258 if (fileList->runStatus == runStatus) {
259 if (fileList->isOccupied == NSTACKX_TRUE) {
260 continue;
261 }
262 if (runStatus == FILE_LIST_STATUS_IDLE) {
263 fileList->runStatus = FILE_LIST_STATUS_RUN;
264 fileList->isOccupied = NSTACKX_TRUE;
265 } else if (runStatus == FILE_LIST_STATUS_STOP) {
266 ListRemoveNode(&fileList->list);
267 fileManager->taskList.size--;
268 }
269 isFound = NSTACKX_TRUE;
270 break;
271 }
272 }
273 if (PthreadMutexUnlock(&fileManager->taskList.lock) != 0) {
274 LOGE(TAG, "pthread mutex unlock error");
275 *isErrorOccured = NSTACKX_TRUE;
276 if (runStatus != FILE_LIST_STATUS_STOP) {
277 return NULL;
278 }
279 }
280
281 if (isFound) {
282 return fileList;
283 }
284 return NULL;
285 }
286
SetFileOffset(FileInfo * fileInfo,uint64_t fileOffset)287 int32_t SetFileOffset(FileInfo *fileInfo, uint64_t fileOffset)
288 {
289 if (fileInfo->fileOffset == fileOffset) {
290 return NSTACKX_EOK;
291 }
292 #ifdef BUILD_FOR_WINDOWS
293 if (fseek(fileInfo->fd, (int64_t)fileOffset, SEEK_SET) != 0) {
294 LOGE(TAG, "fseek error");
295 return NSTACKX_EFAILED;
296 }
297 #else
298 #endif
299 fileInfo->fileOffset = fileOffset;
300 return NSTACKX_EOK;
301 }
CloseFile(FileInfo * fileInfo)302 void CloseFile(FileInfo *fileInfo)
303 {
304 if (fileInfo == NULL) {
305 return;
306 }
307 if (fileInfo->fd != NSTACKX_INVALID_FD) {
308 #ifdef BUILD_FOR_WINDOWS
309 if (fclose(fileInfo->fd) != 0) {
310 LOGE(TAG, "fclose error");
311 }
312 #else
313 if (close(fileInfo->fd) != 0) {
314 LOGE(TAG, "close error");
315 }
316 #endif
317 fileInfo->fileOffset = 0;
318 fileInfo->fd = NSTACKX_INVALID_FD;
319 }
320 }
321
FileGetBytesTransferred(const FileInfo * fileInfo,uint8_t isSender)322 static uint64_t FileGetBytesTransferred(const FileInfo *fileInfo, uint8_t isSender)
323 {
324 uint64_t lastBlockSize;
325 uint64_t ret;
326 if (fileInfo == NULL || fileInfo->fileSize == 0) {
327 return 0;
328 }
329 if (isSender) {
330 if (fileInfo->maxSequenceSend < 0) {
331 return 0;
332 }
333 if (fileInfo->maxSequenceSend + 1 == fileInfo->totalBlockNum) {
334 ret = fileInfo->fileSize;
335 } else {
336 ret = ((uint64_t)fileInfo->standardBlockSize) * ((uint64_t)fileInfo->maxSequenceSend + 1);
337 }
338 } else {
339 if (!fileInfo->isEndBlockReceived) {
340 ret = ((uint64_t)fileInfo->standardBlockSize) * ((uint64_t)fileInfo->receivedBlockNum);
341 } else {
342 lastBlockSize = fileInfo->fileSize % ((uint64_t)fileInfo->standardBlockSize);
343 ret = ((uint64_t)fileInfo->standardBlockSize) * ((uint64_t)fileInfo->receivedBlockNum - 1) + lastBlockSize;
344 }
345 }
346
347 if (ret > fileInfo->fileSize) {
348 ret = fileInfo->fileSize;
349 }
350 return ret;
351 }
352
FileListGetBytesTransferred(const FileListTask * fileList,uint8_t isSender)353 uint64_t FileListGetBytesTransferred(const FileListTask *fileList, uint8_t isSender)
354 {
355 uint32_t i;
356 uint64_t ret = 0;
357 if ((fileList->tarFlag == NSTACKX_TRUE) && (isSender == NSTACKX_TRUE)) {
358 if (fileList->tarFileInfo.maxSequenceSend < 0) {
359 return 0;
360 }
361 if ((fileList->tarFileInfo.maxSequenceSend + 1) == (int32_t)fileList->tarFileInfo.totalBlockNum) {
362 ret = fileList->tarFileInfo.fileSize;
363 } else {
364 ret = ((uint64_t)fileList->tarFileInfo.standardBlockSize) *
365 ((uint64_t)fileList->tarFileInfo.maxSequenceSend + 1);
366 }
367 return ret;
368 }
369
370 for (i = 0; i < fileList->fileNum; i++) {
371 ret += FileGetBytesTransferred(&fileList->fileInfo[i], isSender);
372 }
373 return ret;
374 }
375
ClearRecvFileList(FileListTask * fileList)376 static void ClearRecvFileList(FileListTask *fileList)
377 {
378 BlockFrame *blockFrame = NULL;
379 for (uint32_t i = 0; i < fileList->fileNum; i++) {
380 CloseFile(&fileList->fileInfo[i]);
381 free(fileList->fileInfo[i].fileName);
382 fileList->fileInfo[i].fileName = NULL;
383 }
384 SemDestroy(&fileList->semStop);
385 if (PthreadMutexLock(&fileList->recvBlockList.lock) != 0) {
386 LOGE(TAG, "pthread mutex lock error");
387 }
388 while (fileList->recvBlockList.size > 0) {
389 blockFrame = (BlockFrame *)ListPopFront(&fileList->recvBlockList.head);
390 fileList->recvBlockList.size--;
391 if (blockFrame != NULL) {
392 free(blockFrame->fileDataFrame);
393 free(blockFrame);
394 blockFrame = NULL;
395 }
396 }
397 if (PthreadMutexUnlock(&fileList->recvBlockList.lock) != 0) {
398 LOGE(TAG, "pthread mutex unlock error");
399 }
400 MutexListDestory(&fileList->recvBlockList);
401 while (!ListIsEmpty(&fileList->innerRecvBlockHead)) {
402 blockFrame = (BlockFrame *)ListPopFront(&fileList->innerRecvBlockHead);
403 if (blockFrame == NULL) {
404 continue;
405 }
406 free(blockFrame->fileDataFrame);
407 free(blockFrame);
408 blockFrame = NULL;
409 }
410 #ifndef MBEDTLS_INCLUDED
411 ClearCryptCtx(fileList->cryptPara.ctx);
412 #endif
413 (void)memset_s(fileList, sizeof(FileListTask), 0, sizeof(FileListTask));
414 free(fileList);
415 }
416
GetFullFilePath(const char * path,const char * fileName)417 char *GetFullFilePath(const char *path, const char *fileName)
418 {
419 int32_t ret;
420 char *fullPath = NULL;
421 uint32_t fullPathLength;
422 if (path == NULL || fileName == NULL) {
423 return NULL;
424 }
425
426 if ((CheckPathSeprator(path) == NSTACKX_TRUE) || (CheckFilenameSeprator(fileName) == NSTACKX_TRUE)) {
427 fullPathLength = (uint32_t)(strlen(path) + strlen(fileName) + sizeof('\0'));
428 } else {
429 fullPathLength = (uint32_t)(strlen(path) + sizeof(PATH_SEPARATOR) + strlen(fileName) + sizeof('\0'));
430 }
431
432 if (fullPathLength == 0) {
433 return NULL;
434 }
435 fullPath = (char *)calloc(fullPathLength, sizeof(char));
436 if (fullPath == NULL) {
437 LOGE(TAG, "full path calloc error");
438 return NULL;
439 }
440
441 if ((CheckPathSeprator(path) == NSTACKX_TRUE) || (CheckFilenameSeprator(fileName) == NSTACKX_TRUE)) {
442 ret = sprintf_s(fullPath, fullPathLength, "%s%s", path, fileName);
443 } else {
444 ret = sprintf_s(fullPath, fullPathLength, "%s%c%s", path, PATH_SEPARATOR, fileName);
445 }
446
447 if (ret == -1) {
448 LOGE(TAG, "splice path and file name error");
449 free(fullPath);
450 return NULL;
451 }
452 return fullPath;
453 }
454
ClearIncompleteRecvFiles(const char * path,FileListTask * fileList)455 static void ClearIncompleteRecvFiles(const char *path, FileListTask *fileList)
456 {
457 char *fullPath = NULL;
458 for (uint32_t i = 0; i < fileList->fileNum; i++) {
459 if (fileList->fileInfo[i].errCode == FILE_MANAGER_EOK &&
460 fileList->fileInfo[i].receivedBlockNum == fileList->fileInfo[i].totalBlockNum &&
461 fileList->stopType == FILE_LIST_TRANSFER_FINISH) {
462 continue;
463 }
464 CloseFile(&fileList->fileInfo[i]);
465 fullPath = GetFullFilePath(path, fileList->fileInfo[i].fileName);
466 if (fullPath != NULL) {
467 LOGE(TAG, "going to remove incomplete file %s", fileList->fileInfo[i].fileName);
468 if (remove(fullPath) != 0) {
469 LOGE(TAG, "remove file failed. errno %d", errno);
470 }
471 free(fullPath);
472 }
473 }
474 return;
475 }
476
FileInfoWriteInit(FileInfo * fileInfo,const char * path,uint8_t isTruncate)477 static void FileInfoWriteInit(FileInfo *fileInfo, const char *path, uint8_t isTruncate)
478 {
479 char *fullPath = GetFullFilePath(path, fileInfo->fileName);
480 if (fullPath == NULL) {
481 LOGE(TAG, "Can't get full path");
482 fileInfo->errCode = FILE_MANAGER_ENOMEM;
483 return;
484 }
485 if (TestAndCreateDirectory(fullPath) != NSTACKX_EOK) {
486 free(fullPath);
487 fileInfo->errCode = FILE_MANAGER_FILE_EOTHER;
488 LOGE(TAG, "create directory failed");
489 return;
490 }
491 #ifdef BUILD_FOR_WINDOWS
492 fileInfo->fd = fopen(fullPath, "wb");
493 #else
494 if (isTruncate) {
495 fileInfo->fd = open(fullPath, O_WRONLY | O_CREAT | O_TRUNC, S_IRUSR | S_IWUSR);
496 } else {
497 fileInfo->fd = open(fullPath, O_WRONLY | O_CREAT, S_IRUSR | S_IWUSR);
498 }
499 #endif
500 free(fullPath);
501 if (fileInfo->fd == NSTACKX_INVALID_FD) {
502 LOGE(TAG, "can't open file, error(%d)", errno);
503 fileInfo->errCode = ConvertErrCode(errno);
504 return;
505 }
506 fileInfo->fileOffset = 0;
507 }
508
WriteToFile(FileInfo * fileInfo,uint32_t blockSequence,uint16_t length,uint8_t * payLoad,FileListTask * fileList)509 static int32_t WriteToFile(FileInfo *fileInfo, uint32_t blockSequence, uint16_t length, uint8_t *payLoad,
510 FileListTask *fileList)
511 {
512 uint64_t fileOffset;
513 uint16_t ret = 0;
514 int32_t pRet = 0;
515 DFileSession *session = fileList->context;
516 if (fileInfo->fd == NSTACKX_INVALID_FD) {
517 FileInfoWriteInit(fileInfo, fileList->storagePath, NSTACKX_TRUE);
518 if (fileInfo->fd == NSTACKX_INVALID_FD) {
519 return NSTACKX_EFAILED;
520 }
521 }
522 if (fileInfo->fileSize == 0 || payLoad == NULL || length == 0) {
523 return NSTACKX_EOK;
524 }
525 fileOffset = ((uint64_t)fileInfo->standardBlockSize) * ((uint64_t)blockSequence);
526 fileOffset += fileInfo->startOffset;
527 if (SetFileOffset(fileInfo, fileOffset) != NSTACKX_EOK) {
528 fileInfo->errCode = FILE_MANAGER_FILE_EOTHER;
529 LOGE(TAG, "set file offset failed");
530 return NSTACKX_EFAILED;
531 }
532 if (CapsNoRW(session)) {
533 ret = length;
534 } else {
535 #ifdef BUILD_FOR_WINDOWS
536 ret = (uint16_t)fwrite(payLoad, 1, length, fileInfo->fd);
537 #else
538 /* use pwrite because fseek have multi-thread issue in case of multi-path handle same file scenario */
539 pRet = (int32_t)pwrite(fileInfo->fd, payLoad, length, (int64_t)fileOffset);
540 if (pRet >= 0) {
541 ret = (uint16_t)pRet;
542 }
543 #endif
544 }
545 if ((pRet < 0) || (ret < length)) {
546 LOGE(TAG, "fwrite error %d write %hu target %hu pRet:%d", GetErrno(), ret, length, pRet);
547 fileInfo->errCode = FILE_MANAGER_FILE_EOTHER;
548 return NSTACKX_EFAILED;
549 }
550 fileInfo->fileOffset += ret;
551 if (++fileInfo->receivedBlockNum == fileInfo->totalBlockNum) {
552 fileInfo->isEndBlockReceived = NSTACKX_TRUE;
553 }
554 return NSTACKX_EOK;
555 }
556
GetFrameHearderInfo(FileListTask * fileList,BlockFrame * blockFrame,uint16_t * fileId,uint32_t * blockSequence,uint16_t * payloadLength)557 static int32_t GetFrameHearderInfo(FileListTask *fileList, BlockFrame *blockFrame, uint16_t *fileId,
558 uint32_t *blockSequence, uint16_t *payloadLength)
559 {
560 uint16_t transId, length;
561 transId = ntohs(blockFrame->fileDataFrame->header.transId);
562 *fileId = ntohs(blockFrame->fileDataFrame->fileId);
563 *blockSequence = ntohl(blockFrame->fileDataFrame->blockSequence);
564 length = ntohs(blockFrame->fileDataFrame->header.length);
565 if (transId != fileList->transId || *fileId > fileList->fileNum || *fileId == 0) {
566 LOGE(TAG, "illegal transId (%hu) or fileId (%hu)", transId, *fileId);
567 return NSTACKX_EFAILED;
568 } else {
569 FileInfo *info = &fileList->fileInfo[*fileId - 1];
570 if (info->receivedBlockNum == info->totalBlockNum) {
571 LOGI(TAG, "fileId (%hu) has already finished written totalBlockNum %u", *fileId, info->totalBlockNum);
572 *payloadLength = 0;
573 return NSTACKX_EOK;
574 }
575 }
576
577 if (*blockSequence >= fileList->fileInfo[*fileId - 1].totalBlockNum ||
578 length <= sizeof(FileDataFrame) - sizeof(DFileFrameHeader) || length > NSTACKX_MAX_FRAME_SIZE) {
579 LOGE(TAG, "block sequence or length is illegal");
580 fileList->errCode = FILE_MANAGER_LIST_EBLOCK;
581 return NSTACKX_EFAILED;
582 }
583
584 *payloadLength = length + sizeof(DFileFrameHeader) - sizeof(FileDataFrame);
585 return NSTACKX_EOK;
586 }
587
UpdateFileListRecvStatus(FileManager * fileManager,FileListTask * fileList,FileInfo * fileInfo,int32_t ret)588 static void UpdateFileListRecvStatus(FileManager *fileManager, FileListTask *fileList, FileInfo *fileInfo, int32_t ret)
589 {
590 if (ret != NSTACKX_EOK) {
591 LOGE(TAG, "WriteToFile error:transId %u, fileId %u", fileList->transId, fileInfo->fileId);
592 CloseFile(fileInfo);
593 fileList->recvFileProcessed++;
594 NotifyFileMsg(fileList, fileInfo->fileId, FILE_MANAGER_RECEIVE_FAIL);
595 return;
596 }
597 if (fileInfo->receivedBlockNum < fileInfo->totalBlockNum) {
598 return;
599 }
600 CloseFile(fileInfo);
601 fileList->recvFileProcessed++;
602 if (fileList->recvFileProcessed == fileList->fileNum) {
603 NotifyFileManagerMsg(fileManager, FILE_MANAGER_IN_PROGRESS);
604 }
605 NotifyFileMsg(fileList, fileInfo->fileId, FILE_MANAGER_RECEIVE_SUCCESS);
606 }
607
WriteSingleBlockFrame(FileManager * fileManager,FileListTask * fileList,BlockFrame * blockFrame)608 static int32_t WriteSingleBlockFrame(FileManager *fileManager, FileListTask *fileList, BlockFrame *blockFrame)
609 {
610 FileInfo *fileInfo = NULL;
611 uint16_t fileId, payloadLength;
612 uint32_t blockSequence;
613 uint8_t *payLoad = NULL;
614 uint8_t *buffer = NULL;
615 int32_t ret = NSTACKX_EFAILED;
616
617 if (GetFrameHearderInfo(fileList, blockFrame, &fileId, &blockSequence, &payloadLength) != NSTACKX_EOK) {
618 fileList->errCode = FILE_MANAGER_LIST_EBLOCK;
619 return NSTACKX_EFAILED;
620 }
621
622 if (payloadLength == 0) {
623 return NSTACKX_EOK;
624 }
625
626 fileInfo = &fileList->fileInfo[fileId - 1];
627 if (fileInfo->errCode != FILE_MANAGER_EOK) {
628 return NSTACKX_EOK;
629 }
630
631 payLoad = blockFrame->fileDataFrame->blockPayload;
632 uint32_t dataLen;
633 if (fileList->cryptPara.keylen > 0 && payloadLength > 0) {
634 buffer = (uint8_t *)calloc(payloadLength, 1);
635 if (buffer == NULL) {
636 fileList->errCode = FILE_MANAGER_ENOMEM;
637 return NSTACKX_EFAILED;
638 }
639 dataLen = AesGcmDecrypt(payLoad, payloadLength, &fileList->cryptPara, buffer, payloadLength);
640 if (dataLen == 0) {
641 fileInfo->errCode = FILE_MANAGER_FILE_EOTHER;
642 payLoad = NULL;
643 LOGE(TAG, "data decrypt error");
644 } else {
645 payLoad = buffer;
646 payloadLength = (uint16_t)dataLen;
647 }
648 }
649 if (payLoad != NULL) {
650 ret = WriteToFile(fileInfo, blockSequence, payloadLength, payLoad, fileList);
651 if (ret == NSTACKX_EOK) {
652 fileManager->iowBytes += (uint64_t)payloadLength;
653 }
654 }
655 /*
656 * When all blocks are received, fsync should be called before refreshing the receivedBlockNum.
657 */
658 if (fileList->noSyncFlag == NSTACKX_FALSE && fileInfo->isEndBlockReceived) {
659 FileSync(fileInfo);
660 }
661 free(buffer);
662 UpdateFileListRecvStatus(fileManager, fileList, fileInfo, ret);
663 return ret;
664 }
665
WriteBlockFrame(FileManager * fileManager,FileListTask * fileList)666 static int32_t WriteBlockFrame(FileManager *fileManager, FileListTask *fileList)
667 {
668 BlockFrame *blockFrame = NULL;
669 while (!ListIsEmpty(&fileList->innerRecvBlockHead)) {
670 if (CheckManager(fileManager) != NSTACKX_EOK || CheckFilelist(fileList) != NSTACKX_EOK) {
671 break;
672 }
673 blockFrame = (BlockFrame *)ListPopFront(&fileList->innerRecvBlockHead);
674 if (blockFrame == NULL) {
675 LOGE(TAG, "get a null block");
676 continue;
677 }
678 if (WriteSingleBlockFrame(fileManager, fileList, blockFrame) != NSTACKX_EOK) {
679 LOGE(TAG, "write block frame failed");
680 if (fileList->errCode != NSTACKX_EOK) {
681 goto L_ERR_FILE_MANAGER;
682 }
683 }
684
685 free(blockFrame->fileDataFrame);
686 free(blockFrame);
687 blockFrame = NULL;
688
689 if (fileList->innerRecvSize > 0) {
690 fileList->innerRecvSize--;
691 }
692 }
693 return NSTACKX_EOK;
694 L_ERR_FILE_MANAGER:
695 free(blockFrame->fileDataFrame);
696 free(blockFrame);
697 return NSTACKX_EFAILED;
698 }
699
SwapRecvBlockListHead(MutexList * mutexList,uint8_t * isEmpty,List * newHead,uint32_t * size)700 static int32_t SwapRecvBlockListHead(MutexList *mutexList, uint8_t *isEmpty, List *newHead, uint32_t *size)
701 {
702 List *front = NULL;
703 List *back = NULL;
704 *isEmpty = NSTACKX_FALSE;
705 if (PthreadMutexLock(&mutexList->lock) != 0) {
706 LOGE(TAG, "pthread mutex lock error");
707 return NSTACKX_EFAILED;
708 }
709 if (mutexList->size == 0) {
710 *isEmpty = NSTACKX_TRUE;
711 } else {
712 front = mutexList->head.next;
713 back = mutexList->head.prev;
714 newHead->next = front;
715 newHead->prev = back;
716 front->prev = newHead;
717 back->next = newHead;
718 ListInitHead(&mutexList->head);
719 *size = mutexList->size;
720 mutexList->size = 0;
721 }
722 if (PthreadMutexUnlock(&mutexList->lock) != 0) {
723 LOGE(TAG, "pthread mutex unlock error");
724 return NSTACKX_EFAILED;
725 }
726 return NSTACKX_EOK;
727 }
728
GenerateAllEmptyFiles(FileListTask * fileList)729 static void GenerateAllEmptyFiles(FileListTask *fileList)
730 {
731 FileInfo *fileInfo = NULL;
732 int32_t ret;
733 for (uint32_t i = 0; i < fileList->fileNum; i++) {
734 if (fileList->fileInfo[i].fileSize > 0) {
735 continue;
736 }
737 fileInfo = &fileList->fileInfo[i];
738 ret = WriteToFile(fileInfo, 0, 0, NULL, fileList);
739 CloseFile(fileInfo);
740 fileList->recvFileProcessed++;
741
742 if (ret != NSTACKX_EOK) {
743 LOGE(TAG, "Create empty file error: transId %u, fileId %u", fileList->transId, fileInfo->fileId);
744 NotifyFileMsg(fileList, fileInfo->fileId, FILE_MANAGER_RECEIVE_FAIL);
745 } else {
746 LOGI(TAG, "Create empty file successfully: transId %u, fileId %u", fileList->transId, fileInfo->fileId);
747 NotifyFileMsg(fileList, fileList->fileInfo[i].fileId, FILE_MANAGER_RECEIVE_SUCCESS);
748 }
749 }
750 }
751
FileListRefreshFileRecvStatus(FileListTask * fileList)752 static void FileListRefreshFileRecvStatus(FileListTask *fileList)
753 {
754 if (fileList->recvFileProcessed >= fileList->fileNum) {
755 return;
756 }
757
758 for (uint16_t i = 0; i < fileList->fileNum; i++) {
759 if (FileGetRecvStatus(&fileList->fileInfo[i]) == STATE_RECEIVE_ONGOING) {
760 fileList->fileInfo[i].errCode = FILE_MANAGER_FILE_EOTHER;
761 LOGE(TAG, "file list will be stopped and set incompleted file %u to fail", fileList->fileInfo[i].fileId);
762 NotifyFileMsg(fileList, fileList->fileInfo[i].fileId, FILE_MANAGER_RECEIVE_FAIL);
763 }
764 }
765 fileList->recvFileProcessed = fileList->fileNum;
766 }
767
RecvTaskProcess(FileManager * fileManager,FileListTask * fileList)768 static void RecvTaskProcess(FileManager *fileManager, FileListTask *fileList)
769 {
770 uint8_t isEmpty = NSTACKX_FALSE;
771
772 while (NSTACKX_TRUE) {
773 if (CheckManager(fileManager) != NSTACKX_EOK || CheckFilelist(fileList) != NSTACKX_EOK ||
774 fileList->recvFileProcessed >= fileList->fileNum) {
775 break;
776 }
777
778 if (!fileList->isRecvEmptyFilesCreated) {
779 GenerateAllEmptyFiles(fileList);
780 fileList->isRecvEmptyFilesCreated = NSTACKX_TRUE;
781 continue;
782 }
783
784 SemWait(&fileList->semStop);
785 if (CheckManager(fileManager) != NSTACKX_EOK || CheckFilelist(fileList) != NSTACKX_EOK) {
786 break;
787 }
788 if (SwapRecvBlockListHead(&fileList->recvBlockList, &isEmpty, &fileList->innerRecvBlockHead,
789 &(fileList->innerRecvSize)) != NSTACKX_EOK) {
790 LOGE(TAG, "Swap receive block list head error:transId %u", fileList->transId);
791 fileList->errCode = FILE_MANAGER_EMUTEX;
792 break;
793 }
794 if (isEmpty) {
795 if (fileList->allFileDataReceived) {
796 fileList->dataWriteTimeoutCnt++;
797 }
798 if (fileList->dataWriteTimeoutCnt > NSTACKX_MAX_DATA_FWRITE_TIMEOUT_COUNT) {
799 LOGE(TAG, "some frames may lost or illegal and stop this file list");
800 break;
801 }
802 continue;
803 } else {
804 fileList->dataWriteTimeoutCnt = 0;
805 }
806 if (WriteBlockFrame(fileManager, fileList) != NSTACKX_EOK) {
807 LOGE(TAG, "WriteBlockFrame error");
808 continue;
809 }
810 }
811 FileListRefreshFileRecvStatus(fileList);
812 if (fileList->errCode != FILE_MANAGER_EOK) {
813 NotifyFileListMsg(fileList, FILE_MANAGER_RECEIVE_FAIL);
814 LOGE(TAG, "recv task process failed");
815 }
816 }
817
ClearFileList(FileManager * fileManager,FileListTask * fileList)818 static void ClearFileList(FileManager *fileManager, FileListTask *fileList)
819 {
820 if (fileManager->isSender) {
821 ClearSendFileList(fileManager, fileList);
822 } else {
823 ClearIncompleteRecvFiles(fileList->storagePath, fileList);
824 ClearRecvFileList(fileList);
825 }
826 }
827
BindFileManagerThreadToTargetCpu(FileManager * fileManager,uint32_t idx)828 static void BindFileManagerThreadToTargetCpu(FileManager *fileManager, uint32_t idx)
829 {
830 int32_t cpu;
831 int32_t cpus = GetCpuNum();
832 if (cpus >= FIRST_CPU_NUM_LEVEL) {
833 return;
834 } else if (cpus >= SECOND_CPU_NUM_LEVEL) {
835 if (fileManager->isSender) {
836 cpu = CPU_IDX_0;
837 } else {
838 cpu = CPU_IDX_2 + (int32_t)idx % FILE_MANAGER_THREADS_BINDED_CPU_NUM;
839 }
840 } else if (cpus >= THIRD_CPU_NUM_LEVEL) {
841 if (fileManager->isSender) {
842 cpu = CPU_IDX_0;
843 } else {
844 cpu = CPU_IDX_1;
845 }
846 } else {
847 return;
848 }
849 StartThreadBindCore(cpu);
850 }
851
852 typedef struct {
853 FileManager *fileManager;
854 uint32_t threadIdx;
855 } FileManagerThreadCtx;
856
SetIOThreadName(uint32_t threadIdx)857 static void SetIOThreadName(uint32_t threadIdx)
858 {
859 char name[MAX_THREAD_NAME_LEN] = {0};
860 if (sprintf_s(name, sizeof(name), "%s%u", DFFILE_IO_THREAD_NAME_PREFIX, threadIdx) < 0) {
861 LOGE(TAG, "sprintf io thead name failed");
862 }
863 SetThreadName(name);
864 LOGI(TAG, "IO thread %u start", threadIdx);
865 }
866
DoTaskProcess(FileManager * fileManager,FileListTask * fileList)867 static void DoTaskProcess(FileManager *fileManager, FileListTask *fileList)
868 {
869 if (fileManager->isSender) {
870 SendTaskProcess(fileManager, fileList);
871 } else {
872 RecvTaskProcess(fileManager, fileList);
873 }
874 }
875
FileManagerPre(FileManager * fileManager,uint32_t threadIdx)876 static void FileManagerPre(FileManager *fileManager, uint32_t threadIdx)
877 {
878 SetIOThreadName(threadIdx);
879 SetMaximumPriorityForThread();
880 SetTidToBindInfo(fileManager->context, threadIdx);
881 }
882
AfterTaskProcess(FileManager * fileManager,FileListTask * fileList)883 static void AfterTaskProcess(FileManager *fileManager, FileListTask *fileList)
884 {
885 fileList->isOccupied = NSTACKX_FALSE;
886 SemPost(&fileManager->semTaskListNotEmpty);
887 }
888
FileManagerThread(void * arg)889 static void *FileManagerThread(void *arg)
890 {
891 FileManagerThreadCtx *ctx = (FileManagerThreadCtx *)arg;
892 FileManager *fileManager = ctx->fileManager;
893 uint32_t threadIdx = ctx->threadIdx;
894 free(ctx);
895 uint8_t isErrorOccured = NSTACKX_FALSE;
896 FileListTask *fileList = NULL;
897 uint8_t isBind = NSTACKX_FALSE;
898 FileManagerPre(fileManager, threadIdx);
899 while (fileManager->runStatus == FILE_MANAGE_RUN) {
900 SemWait(&fileManager->semTaskListNotEmpty);
901 if (fileManager->runStatus != FILE_MANAGE_RUN) {
902 break;
903 }
904 uint8_t isStopTaskDetached = NSTACKX_FALSE;
905 fileList = PrepareOneTaskByStatus(fileManager, FILE_LIST_STATUS_STOP, &isErrorOccured);
906 if (isErrorOccured) {
907 fileManager->errCode = FILE_MANAGER_EMUTEX;
908 NotifyFileManagerMsg(fileManager, FILE_MANAGER_INNER_ERROR);
909 LOGE(TAG, "error occuerd when get stop file list");
910 }
911 if (fileList != NULL) {
912 isStopTaskDetached = NSTACKX_TRUE;
913 LOGI(TAG, "Thread %u is clearing fileList %u", threadIdx, fileList->transId);
914 ClearFileList(fileManager, fileList);
915 }
916 if (isErrorOccured || isStopTaskDetached) {
917 continue;
918 }
919
920 fileList = PrepareOneTaskByStatus(fileManager, FILE_LIST_STATUS_IDLE, &isErrorOccured);
921 if (isErrorOccured) {
922 fileManager->errCode = FILE_MANAGER_EMUTEX;
923 NotifyFileManagerMsg(fileManager, FILE_MANAGER_INNER_ERROR);
924 LOGE(TAG, "error occuerd when get idle file list");
925 continue;
926 }
927 if (fileList == NULL || fileList->errCode != FILE_MANAGER_EOK) {
928 continue;
929 }
930 LOGI(TAG, "Thread %u is processing for fileList %u", threadIdx, fileList->transId);
931 if (isBind == NSTACKX_FALSE && fileManager->transFlag == NSTACKX_TRUE) {
932 BindFileManagerThreadToTargetCpu(fileManager, threadIdx);
933 isBind = NSTACKX_TRUE;
934 }
935 DoTaskProcess(fileManager, fileList);
936 AfterTaskProcess(fileManager, fileList);
937 }
938 return NULL;
939 }
940
WakeAllThread(FileManager * fileManager)941 static void WakeAllThread(FileManager *fileManager)
942 {
943 uint32_t i;
944 List *list = NULL;
945 FileListTask *fileList = NULL;
946 SendBlockFrameListPara *para = NULL;
947 if (PthreadMutexLock(&fileManager->taskList.lock) != 0) {
948 LOGE(TAG, "pthread mutex lock error");
949 return;
950 }
951 LIST_FOR_EACH(list, &fileManager->taskList.head) {
952 fileList = (FileListTask *)list;
953 SemPost(&fileList->semStop);
954 para = &fileManager->sendBlockFrameListPara[fileList->bindedSendBlockListIdx];
955 SemPost(¶->semBlockListNotFull);
956 }
957 if (PthreadMutexUnlock(&fileManager->taskList.lock) != 0) {
958 LOGE(TAG, "pthread mutex unlock error");
959 return;
960 }
961 for (i = 0; i < NSTACKX_FILE_MANAGER_THREAD_NUM; i++) {
962 SemPost(&fileManager->semTaskListNotEmpty);
963 }
964 }
965
StopFileManagerThreads(FileManager * fileManager)966 void StopFileManagerThreads(FileManager *fileManager)
967 {
968 uint32_t i;
969 uint32_t tryNum;
970
971 if (fileManager == NULL || fileManager->runStatus == FILE_MANAGE_DESTROY) {
972 return;
973 }
974
975 fileManager->runStatus = FILE_MANAGE_DESTROY;
976 for (tryNum = 0; tryNum < THREAD_QUIT_TRY_TIMES; tryNum++) {
977 WakeAllThread(fileManager);
978 }
979
980 for (i = 0; i < NSTACKX_FILE_MANAGER_THREAD_NUM; i++) {
981 PthreadJoin(fileManager->fileManagerTid[i], NULL);
982 LOGI(TAG, "Total thread %u: %u quit", NSTACKX_FILE_MANAGER_THREAD_NUM, i + 1);
983 fileManager->fileManagerTid[i] = INVALID_TID;
984 }
985 }
986
CreateFMThread(FileManager * fileManager)987 static int32_t CreateFMThread(FileManager *fileManager)
988 {
989 uint32_t i;
990 FileManagerThreadCtx *ctx = NULL;
991
992 for (i = 0; i < NSTACKX_FILE_MANAGER_THREAD_NUM; i++) {
993 ctx = (FileManagerThreadCtx *)calloc(1, sizeof(FileManagerThreadCtx));
994 if (ctx == NULL) {
995 LOGE(TAG, "the %u ctx create failed", i + 1);
996 goto L_ERR_FILEMANAGER;
997 }
998 ctx->fileManager = fileManager;
999 ctx->threadIdx = i;
1000 if ((PthreadCreate(&fileManager->fileManagerTid[i], NULL, FileManagerThread, ctx)) != 0) {
1001 LOGE(TAG, "the %u thread create failed", i + 1);
1002 free(ctx);
1003 goto L_ERR_FILEMANAGER;
1004 }
1005 }
1006 return NSTACKX_EOK;
1007
1008 L_ERR_FILEMANAGER:
1009 fileManager->runStatus = FILE_MANAGE_DESTROY;
1010 for (int32_t j = 0; j < NSTACKX_FILE_MANAGER_THREAD_NUM; j++) {
1011 SemPost(&fileManager->semTaskListNotEmpty);
1012 }
1013 while (i > 0) {
1014 PthreadJoin(fileManager->fileManagerTid[i - 1], NULL);
1015 i--;
1016 }
1017 return NSTACKX_EFAILED;
1018 }
1019
GetStandardBlockSize(const FileManager * fileManager)1020 uint16_t GetStandardBlockSize(const FileManager *fileManager)
1021 {
1022 uint32_t standardBlockSize;
1023
1024 if (fileManager->maxFrameLength <= offsetof(FileDataFrame, blockPayload)) {
1025 return 0;
1026 }
1027
1028 standardBlockSize = fileManager->maxFrameLength - offsetof(FileDataFrame, blockPayload);
1029
1030 if (fileManager->keyLen > 0) {
1031 if (standardBlockSize <= GCM_ADDED_LEN) {
1032 return 0;
1033 }
1034 standardBlockSize -= GCM_ADDED_LEN;
1035 }
1036 return (uint16_t)standardBlockSize;
1037 }
1038
SetCryptPara(FileListTask * fileList,const uint8_t key[],uint32_t keyLen)1039 int32_t SetCryptPara(FileListTask *fileList, const uint8_t key[], uint32_t keyLen)
1040 {
1041 uint32_t aadLen;
1042
1043 if (memcpy_s(fileList->cryptPara.key, sizeof(fileList->cryptPara.key), key, keyLen) != EOK) {
1044 LOGE(TAG, "memcpy key failed");
1045 return NSTACKX_EFAILED;
1046 }
1047 fileList->cryptPara.keylen = keyLen;
1048
1049 aadLen = sizeof(fileList->cryptPara.aad);
1050 (void)memset_s(fileList->cryptPara.aad, aadLen, GCM_AAD_CHAR, aadLen);
1051 fileList->cryptPara.aadLen = aadLen;
1052 #ifndef MBEDTLS_INCLUDED
1053 fileList->cryptPara.ctx = CreateCryptCtx();
1054 if (fileList->cryptPara.ctx == NULL) {
1055 LOGE(TAG, "failed to create crypt ctx");
1056 return NSTACKX_EFAILED;
1057 }
1058 #endif
1059 return NSTACKX_EOK;
1060 }
1061
1062 /*
1063 * Note that this interface is only called by dfile main thread now. If other thread wants to call it, must be very
1064 * careful about thread-safety.
1065 */
GetFileListById(MutexList * taskList,uint16_t transId,uint8_t * isErrorOccured)1066 FileListTask *GetFileListById(MutexList *taskList, uint16_t transId, uint8_t *isErrorOccured)
1067 {
1068 List *list = NULL;
1069 FileListTask *fileList = NULL;
1070 uint8_t isFound = NSTACKX_FALSE;
1071 if (isErrorOccured != NULL) {
1072 *isErrorOccured = NSTACKX_FALSE;
1073 }
1074 if (taskList == NULL) {
1075 return NULL;
1076 }
1077 if (PthreadMutexLock(&taskList->lock) != 0) {
1078 LOGE(TAG, "pthread mutex lock error");
1079 goto L_ERR_FILE_MANAGER;
1080 }
1081 LIST_FOR_EACH(list, &taskList->head) {
1082 fileList = (FileListTask *)list;
1083 /* If the target filelist has been stopped, it will not be accessable by other thread. */
1084 if (fileList->transId == transId && fileList->runStatus != FILE_LIST_STATUS_STOP) {
1085 isFound = NSTACKX_TRUE;
1086 break;
1087 }
1088 }
1089 if (PthreadMutexUnlock(&taskList->lock) != 0) {
1090 LOGE(TAG, "pthread mutex unlock error");
1091 goto L_ERR_FILE_MANAGER;
1092 }
1093 if (isFound) {
1094 return fileList;
1095 }
1096 return NULL;
1097 L_ERR_FILE_MANAGER:
1098 if (isErrorOccured != NULL) {
1099 *isErrorOccured = NSTACKX_TRUE;
1100 }
1101 return NULL;
1102 }
1103
GetFileBlockListSize(MutexList * taskList,uint32_t * recvListAllSize,uint32_t * recvInnerAllSize)1104 int32_t GetFileBlockListSize(MutexList *taskList, uint32_t *recvListAllSize, uint32_t *recvInnerAllSize)
1105 {
1106 List *list = NULL;
1107 FileListTask *fileList = NULL;
1108 uint32_t sum = 0;
1109 uint32_t innerSum = 0;
1110
1111 if (taskList == NULL) {
1112 *recvListAllSize = 0;
1113 *recvInnerAllSize = 0;
1114 return NSTACKX_EOK;
1115 }
1116 if (PthreadMutexLock(&taskList->lock) != 0) {
1117 LOGE(TAG, "pthread mutex lock error");
1118 goto L_ERR_FILE_MANAGER;
1119 }
1120 LIST_FOR_EACH(list, &taskList->head) {
1121 fileList = (FileListTask *)list;
1122 /* If the target filelist has been stopped, it will not be accessable by other thread. */
1123 if (fileList->runStatus != FILE_LIST_STATUS_STOP) {
1124 sum += fileList->recvBlockList.size;
1125 innerSum += fileList->innerRecvSize;
1126 }
1127 }
1128 if (PthreadMutexUnlock(&taskList->lock) != 0) {
1129 LOGE(TAG, "pthread mutex unlock error");
1130 goto L_ERR_FILE_MANAGER;
1131 }
1132 *recvListAllSize = sum;
1133 *recvInnerAllSize = innerSum;
1134 return NSTACKX_EOK;
1135 L_ERR_FILE_MANAGER:
1136 return NSTACKX_EFAILED;
1137 }
1138
RefreshBytesTransFerred(FileManager * fileManager,BlockFrame * block)1139 void RefreshBytesTransFerred(FileManager *fileManager, BlockFrame *block)
1140 {
1141 uint32_t len = 0;
1142 DFileFrameHeader *header = NULL;
1143
1144 while (block) {
1145 header = (DFileFrameHeader *)(void *)block->fileDataFrame;
1146 len += ntohs(header->length) + sizeof(DFileFrameHeader);
1147 len -= sizeof(FileDataFrame);
1148 block = (BlockFrame *)(void *)(block->list.next);
1149 }
1150 if (len == 0) {
1151 return;
1152 }
1153 if (NSTACKX_ATOM_ADD_RETURN(&fileManager->bytesTransferredInCurPeriod, (int32_t)len) >=
1154 (NSTACKX_MEGA_BYTES * MEGA_BYTES_TRANSFER_NOTICE_THRESHOLD)) {
1155 NSTACKX_ATOM_SET(&(fileManager->bytesTransferredInCurPeriod), 0);
1156 NotifyFileManagerMsg(fileManager, FILE_MANAGER_IN_PROGRESS);
1157 }
1158 }
1159
FileManagerStopTask(FileManager * fileManager,uint16_t transId,TaskStopType stopType)1160 int32_t FileManagerStopTask(FileManager *fileManager, uint16_t transId, TaskStopType stopType)
1161 {
1162 FileListTask *fileList = NULL;
1163 List *list = NULL;
1164 uint8_t isFound = NSTACKX_FALSE;
1165 if (fileManager == NULL) {
1166 return NSTACKX_EINVAL;
1167 }
1168 if (PthreadMutexLock(&fileManager->taskList.lock) != 0) {
1169 LOGE(TAG, "pthread mutex lock error");
1170 goto L_ERR_FILE_MANAGER;
1171 }
1172 LIST_FOR_EACH(list, &fileManager->taskList.head) {
1173 fileList = (FileListTask *)list;
1174 if (fileList != NULL && fileList->transId == transId) {
1175 isFound = NSTACKX_TRUE;
1176 break;
1177 }
1178 }
1179 if (isFound) {
1180 if (stopType == FILE_LIST_TRANSFER_FINISH) {
1181 fileManager->stoppedTasksBytesTransferred += fileList->totalBytes;
1182 } else {
1183 fileManager->stoppedTasksBytesTransferred += FileListGetBytesTransferred(fileList, fileManager->isSender);
1184 }
1185 if (fileManager->isSender && fileList->bindedSendBlockListIdx < NSTACKX_MAX_CLIENT_SEND_THREAD_NUM) {
1186 if (fileManager->sendBlockFrameListPara[fileList->bindedSendBlockListIdx].bandingTransNum > 0) {
1187 fileManager->sendBlockFrameListPara[fileList->bindedSendBlockListIdx].bandingTransNum--;
1188 }
1189 }
1190 fileList->stopType = stopType;
1191 fileList->runStatus = FILE_LIST_STATUS_STOP;
1192 SemPost(&fileList->semStop);
1193 SemPost(&fileManager->semTaskListNotEmpty);
1194 }
1195
1196 if (PthreadMutexUnlock(&fileManager->taskList.lock) != 0) {
1197 LOGE(TAG, "pthread mutex unlock error");
1198 goto L_ERR_FILE_MANAGER;
1199 }
1200
1201 if (isFound) {
1202 return NSTACKX_EOK;
1203 }
1204 LOGE(TAG, "can't find target trans %u to stop", transId);
1205 return NSTACKX_EFAILED;
1206 L_ERR_FILE_MANAGER:
1207 fileManager->errCode = FILE_MANAGER_EMUTEX;
1208 NotifyFileManagerMsg(fileManager, FILE_MANAGER_INNER_ERROR);
1209 return NSTACKX_EFAILED;
1210 }
1211
FileManagerSetMaxFrameLength(FileManager * fileManager,uint16_t maxFrameLength)1212 int32_t FileManagerSetMaxFrameLength(FileManager *fileManager, uint16_t maxFrameLength)
1213 {
1214 uint32_t standardDataLength;
1215 if (CheckManager(fileManager) != NSTACKX_EOK) {
1216 LOGE(TAG, "Invalid input");
1217 return NSTACKX_EINVAL;
1218 }
1219 if (maxFrameLength <= offsetof(FileDataFrame, blockPayload) || maxFrameLength > NSTACKX_MAX_FRAME_SIZE) {
1220 LOGE(TAG, "max frame length is illegal");
1221 return NSTACKX_EINVAL;
1222 }
1223 if (fileManager->keyLen > 0) {
1224 standardDataLength = maxFrameLength - offsetof(FileDataFrame, blockPayload);
1225 if (standardDataLength <= GCM_ADDED_LEN) {
1226 LOGE(TAG, "max frame length is too small");
1227 return NSTACKX_EINVAL;
1228 }
1229 }
1230
1231 /* different peerInfo->dataFrameSize in two connection, choose a small one for fileManager */
1232 if (fileManager->maxFrameLength == 0) {
1233 fileManager->maxFrameLength = maxFrameLength;
1234 return NSTACKX_EOK;
1235 }
1236
1237 if (fileManager->maxFrameLength > maxFrameLength) {
1238 fileManager->maxFrameLength = maxFrameLength;
1239 }
1240
1241 return NSTACKX_EOK;
1242 }
1243
FileManagerSetRecvParaWithConnType(FileManager * fileManager,uint16_t connectType)1244 int32_t FileManagerSetRecvParaWithConnType(FileManager *fileManager, uint16_t connectType)
1245 {
1246 int32_t ret = NSTACKX_EOK;
1247 if (CheckReceiverManager(fileManager) != NSTACKX_EOK) {
1248 LOGE(TAG, "Invalid input");
1249 return NSTACKX_EINVAL;
1250 }
1251 if (connectType == CONNECT_TYPE_WLAN) {
1252 fileManager->maxRecvBlockListSize = NSTACKX_WLAN_RECV_BLOCK_QUEUE_MAX_LEN * NSTACKX_FILE_MANAGER_THREAD_NUM;
1253 } else if (connectType == CONNECT_TYPE_P2P) {
1254 fileManager->maxRecvBlockListSize = NSTACKX_P2P_RECV_BLOCK_QUEUE_MAX_LEN * NSTACKX_FILE_MANAGER_THREAD_NUM;
1255 } else {
1256 LOGE(TAG, "Invalid connect type");
1257 ret = NSTACKX_EFAILED;
1258 }
1259 LOGI(TAG, "connect type is %u and max recv list size is %u", connectType, fileManager->maxRecvBlockListSize);
1260 return ret;
1261 }
1262
FileManagerSetWritePath(FileManager * fileManager,const char * storagePath)1263 int32_t FileManagerSetWritePath(FileManager *fileManager, const char *storagePath)
1264 {
1265 if (CheckReceiverManager(fileManager) != NSTACKX_EOK || storagePath == NULL) {
1266 LOGE(TAG, "Invalid input");
1267 return NSTACKX_EINVAL;
1268 }
1269
1270 if (fileManager->typedPathNum > 0) {
1271 LOGE(TAG, "typed storage paths has been set and can't set the common storage path");
1272 return NSTACKX_EINVAL;
1273 }
1274
1275 fileManager->commonStoragePath = realpath(storagePath, NULL);
1276 if (fileManager->commonStoragePath == NULL) {
1277 LOGE(TAG, "can't get canonicalized absolute pathname, error(%d)", errno);
1278 return NSTACKX_EFAILED;
1279 }
1280
1281 if (!IsAccessiblePath(storagePath, W_OK, S_IFDIR)) {
1282 LOGE(TAG, "storage path is not a valid writable folder");
1283 free(fileManager->commonStoragePath);
1284 fileManager->commonStoragePath = NULL;
1285 return NSTACKX_EFAILED;
1286 }
1287
1288 return NSTACKX_EOK;
1289 }
1290
FileManagerSetWritePathList(FileManager * fileManager,char * path[],uint16_t * pathType,uint16_t pathNum)1291 int32_t FileManagerSetWritePathList(FileManager *fileManager, char *path[], uint16_t *pathType, uint16_t pathNum)
1292 {
1293 if (CheckReceiverManager(fileManager) != NSTACKX_EOK || path == NULL || pathType == NULL || pathNum == 0 ||
1294 pathNum > NSTACKX_MAX_STORAGE_PATH_NUM) {
1295 LOGE(TAG, "Invalid input");
1296 return NSTACKX_EINVAL;
1297 }
1298
1299 if (fileManager->commonStoragePath != NULL) {
1300 LOGE(TAG, "common storage paths has been set and can't set the typed storage path");
1301 return NSTACKX_EFAILED;
1302 }
1303
1304 for (uint16_t i = 0; i < pathNum; i++) {
1305 fileManager->pathList[i].storagePath = path[i];
1306 fileManager->pathList[i].pathType = pathType[i];
1307 LOGI(TAG, "the %uth path, type %u", i, fileManager->pathList[i].pathType);
1308 }
1309 fileManager->typedPathNum = pathNum;
1310 return NSTACKX_EOK;
1311 }
1312
AtomicParameterInit(FileManager * fileManager)1313 static int32_t AtomicParameterInit(FileManager *fileManager)
1314 {
1315 uint32_t i = 0;
1316 SendBlockFrameListPara *para = NULL;
1317 uint32_t sendListSize = fileManager->maxSendBlockListSize;
1318
1319 if (SemInit(&fileManager->semTaskListNotEmpty, 0, 0) != 0) {
1320 LOGE(TAG, "semTaskListNotEmpty SemInit error");
1321 return NSTACKX_EFAILED;
1322 }
1323
1324 if (fileManager->isSender) {
1325 for (i = 0; i < fileManager->sendFrameListNum; i++) {
1326 para = &fileManager->sendBlockFrameListPara[i];
1327 if (SemInit(¶->semBlockListNotFull, 0, sendListSize) != 0) {
1328 LOGE(TAG, "semTaskListNotEmpty SemInit error");
1329 goto L_ERR_FILE_MANAGER;
1330 }
1331 }
1332 }
1333 return NSTACKX_EOK;
1334 L_ERR_FILE_MANAGER:
1335 SemDestroy(&fileManager->semTaskListNotEmpty);
1336 while (i > 0) {
1337 para = &fileManager->sendBlockFrameListPara[i - 1];
1338 SemDestroy(¶->semBlockListNotFull);
1339 i--;
1340 }
1341 return NSTACKX_EFAILED;
1342 }
1343
AtomicParameterDestory(FileManager * fileManager)1344 static void AtomicParameterDestory(FileManager *fileManager)
1345 {
1346 SemDestroy(&fileManager->semTaskListNotEmpty);
1347 if (fileManager->isSender) {
1348 for (uint32_t i = 0; i < fileManager->sendFrameListNum; i++) {
1349 SemDestroy(&fileManager->sendBlockFrameListPara[i].semBlockListNotFull);
1350 }
1351 }
1352 }
1353
InitAllCacheList(FileManager * fileManager)1354 static int32_t InitAllCacheList(FileManager *fileManager)
1355 {
1356 if (MutexListInit(&fileManager->taskList, NSTACKX_MAX_PROCESSING_TASK_NUM) != NSTACKX_EOK) {
1357 LOGE(TAG, "taskList InitList error");
1358 return NSTACKX_EFAILED;
1359 }
1360
1361 if (fileManager->isSender) {
1362 if (InitSendBlockLists(fileManager) != NSTACKX_EOK) {
1363 MutexListDestory(&fileManager->taskList);
1364 LOGE(TAG, "sendBlockFrameList InitList error");
1365 return NSTACKX_EFAILED;
1366 }
1367 }
1368 return NSTACKX_EOK;
1369 }
1370
FileManagerInit(FileManager * fileManager,FileManagerMsgPara * msgPara,const uint8_t * key,uint32_t keyLen,uint16_t connType)1371 static int32_t FileManagerInit(FileManager *fileManager, FileManagerMsgPara *msgPara, const uint8_t *key,
1372 uint32_t keyLen, uint16_t connType)
1373 {
1374 fileManager->runStatus = FILE_MANAGE_RUN;
1375 fileManager->errCode = FILE_MANAGER_EOK;
1376 fileManager->transFlag = NSTACKX_FALSE;
1377 if (fileManager->isSender) {
1378 fileManager->sendFrameListNum = GetSendListNum();
1379 fileManager->maxSendBlockListSize = GetMaxSendListSize(connType);
1380 if (fileManager->maxSendBlockListSize == 0 || fileManager->sendFrameListNum == 0) {
1381 LOGE(TAG, "can't get valid send frame list num or size");
1382 return NSTACKX_EFAILED;
1383 }
1384 LOGI(TAG, "connect type is %u and send frame list number is %u max send list size is %u",
1385 connType, fileManager->sendFrameListNum, fileManager->maxSendBlockListSize);
1386 }
1387 if (IsEpollDescValid(msgPara->epollfd) && msgPara->msgReceiver != NULL) {
1388 fileManager->msgReceiver = msgPara->msgReceiver;
1389 fileManager->context = msgPara->context;
1390 fileManager->epollfd = msgPara->epollfd;
1391 fileManager->eventNodeChain = msgPara->eventNodeChain;
1392 }
1393
1394 if (keyLen > 0) {
1395 if (keyLen != AES_128_KEY_LENGTH || key == NULL ||
1396 memcpy_s(fileManager->key, sizeof(fileManager->key), key, keyLen) != EOK) {
1397 LOGE(TAG, "can't get valid key info.");
1398 return NSTACKX_EFAILED;
1399 }
1400 fileManager->keyLen = keyLen;
1401 }
1402 return NSTACKX_EOK;
1403 }
1404
FileManagerCreate(uint8_t isSender,FileManagerMsgPara * msgPara,const uint8_t * key,uint32_t keyLen,uint16_t connType)1405 FileManager *FileManagerCreate(uint8_t isSender, FileManagerMsgPara *msgPara, const uint8_t *key,
1406 uint32_t keyLen, uint16_t connType)
1407 {
1408 FileManager *fileManager = NULL;
1409 if (isSender && (connType != CONNECT_TYPE_P2P && connType != CONNECT_TYPE_WLAN)) {
1410 LOGE(TAG, "connType for sender is illagal");
1411 return NULL;
1412 }
1413 fileManager = (FileManager *)calloc(1, sizeof(FileManager));
1414 if (fileManager == NULL) {
1415 LOGE(TAG, "fileManager calloc error");
1416 return NULL;
1417 }
1418 fileManager->isSender = isSender;
1419 if (FileManagerInit(fileManager, msgPara, key, keyLen, connType) != NSTACKX_EOK) {
1420 (void)memset_s(fileManager, sizeof(FileManager), 0, sizeof(FileManager));
1421 free(fileManager);
1422 return NULL;
1423 }
1424
1425 if (InitAllCacheList(fileManager) != NSTACKX_EOK) {
1426 (void)memset_s(fileManager, sizeof(FileManager), 0, sizeof(FileManager));
1427 free(fileManager);
1428 return NULL;
1429 }
1430
1431 if (AtomicParameterInit(fileManager) != NSTACKX_EOK) {
1432 goto L_ERR_FILE_MANAGER;
1433 }
1434
1435 if (CreateFMThread(fileManager) != NSTACKX_EOK) {
1436 AtomicParameterDestory(fileManager);
1437 goto L_ERR_FILE_MANAGER;
1438 }
1439
1440 return fileManager;
1441
1442 L_ERR_FILE_MANAGER:
1443 MutexListDestory(&fileManager->taskList);
1444 if (fileManager->isSender) {
1445 for (uint32_t i = 0; i < fileManager->sendFrameListNum; i++) {
1446 MutexListDestory(&fileManager->sendBlockFrameListPara[i].sendBlockFrameList);
1447 fileManager->sendBlockFrameListPara[i].sendRetranListTail = NULL;
1448 }
1449 }
1450 (void)memset_s(fileManager, sizeof(FileManager), 0, sizeof(FileManager));
1451 free(fileManager);
1452 return NULL;
1453 }
1454
AddRecvFileInfo(FileBaseInfo * fileBasicInfo,FileListTask * fmFileList,uint16_t standardBlockSize)1455 static int32_t AddRecvFileInfo(FileBaseInfo *fileBasicInfo, FileListTask *fmFileList, uint16_t standardBlockSize)
1456 {
1457 uint32_t i;
1458 char *fileName = NULL;
1459 FileInfo *fileInfo = NULL;
1460 for (i = 0; i < fmFileList->fileNum; i++) {
1461 if (fileBasicInfo[i].fileName == NULL || !IsFileNameLegal(fileBasicInfo[i].fileName)) {
1462 LOGE(TAG, "the %uth input fileName is NULL", i);
1463 goto L_ERR_FILE_MANAGER;
1464 }
1465 fileName = fileBasicInfo[i].fileName;
1466 fileInfo = &fmFileList->fileInfo[i];
1467 fileInfo->fileName = (char *)calloc(strlen(fileName) + 1, sizeof(char));
1468 if (fileInfo->fileName == NULL) {
1469 LOGE(TAG, "fileName calloc error");
1470 goto L_ERR_FILE_MANAGER;
1471 }
1472
1473 if (strcpy_s(fileInfo->fileName, strlen(fileName) + 1, fileName) != NSTACKX_EOK) {
1474 LOGE(TAG, "%uth fileName copy failed", i);
1475 goto L_ERR_FILE_MANAGER;
1476 }
1477
1478 fileInfo->fileSize = fileBasicInfo[i].fileSize;
1479 fileInfo->startOffset = fileBasicInfo[i].startOffset;
1480 fileInfo->fileId = fileBasicInfo[i].fileId;
1481 fileInfo->standardBlockSize = standardBlockSize;
1482 fileInfo->totalBlockNum = (uint32_t)(fileInfo->fileSize / standardBlockSize);
1483 if (fileInfo->fileSize % standardBlockSize != 0) {
1484 fileInfo->totalBlockNum++;
1485 }
1486 fmFileList->totalBytes += fileInfo->fileSize;
1487 fileInfo->receivedBlockNum = 0;
1488 fileInfo->fd = NSTACKX_INVALID_FD;
1489 fileInfo->errCode = FILE_MANAGER_EOK;
1490 fileInfo->fileOffset = 0;
1491 }
1492 return NSTACKX_EOK;
1493 L_ERR_FILE_MANAGER:
1494 for (i = 0; i < fmFileList->fileNum; i++) {
1495 free(fmFileList->fileInfo[i].fileName);
1496 fmFileList->fileInfo[i].fileName = NULL;
1497 }
1498 return NSTACKX_EFAILED;
1499 }
1500
CreateRecvFileList(RecvFileListInfo * fileListInfo,const char * storagePath,uint16_t standardBlockSize,FileListMsgPara * msgPara,uint32_t maxRecvBlockListSize)1501 static FileListTask *CreateRecvFileList(RecvFileListInfo *fileListInfo, const char *storagePath,
1502 uint16_t standardBlockSize, FileListMsgPara *msgPara, uint32_t maxRecvBlockListSize)
1503 {
1504 FileListTask *fmFileList = NULL;
1505 fmFileList = (FileListTask *)calloc(1, sizeof(FileListTask));
1506 if (fmFileList == NULL) {
1507 LOGE(TAG, "file list calloc error");
1508 return NULL;
1509 }
1510 fmFileList->transId = fileListInfo->transId;
1511 fmFileList->fileNum = fileListInfo->fileNum;
1512 fmFileList->storagePath = storagePath;
1513 fmFileList->noSyncFlag = fileListInfo->noSyncFlag;
1514 if (SemInit(&fmFileList->semStop, 0, 0) != 0) {
1515 LOGE(TAG, "SemInit error");
1516 free(fmFileList);
1517 return NULL;
1518 }
1519 fmFileList->runStatus = FILE_LIST_STATUS_IDLE;
1520 fmFileList->stopType = FILE_LIST_TRANSFER_FINISH;
1521 fmFileList->isOccupied = NSTACKX_FALSE;
1522 fmFileList->errCode = FILE_MANAGER_EOK;
1523 ListInitHead(&fmFileList->innerRecvBlockHead);
1524 if (MutexListInit(&fmFileList->recvBlockList, maxRecvBlockListSize) != NSTACKX_EOK) {
1525 LOGE(TAG, "receive block list init error");
1526 goto L_ERR_FILE_MANAGER;
1527 }
1528 fmFileList->recvFileProcessed = 0;
1529 fmFileList->isRecvEmptyFilesCreated = NSTACKX_FALSE;
1530 if (AddRecvFileInfo(fileListInfo->fileBasicInfo, fmFileList, standardBlockSize) != NSTACKX_EOK) {
1531 MutexListDestory(&fmFileList->recvBlockList);
1532 goto L_ERR_FILE_MANAGER;
1533 }
1534 if (msgPara != NULL) {
1535 fmFileList->msgReceiver = msgPara->msgReceiver;
1536 fmFileList->context = msgPara->context;
1537 }
1538 return fmFileList;
1539 L_ERR_FILE_MANAGER:
1540 SemDestroy(&fmFileList->semStop);
1541 (void)memset_s(fmFileList, sizeof(FileListTask), 0, sizeof(FileListTask));
1542 free(fmFileList);
1543 return NULL;
1544 }
1545
GetStoragePathByType(FileManager * fileManager,uint16_t pathType)1546 char *GetStoragePathByType(FileManager *fileManager, uint16_t pathType)
1547 {
1548 if (pathType == 0) {
1549 return fileManager->commonStoragePath;
1550 }
1551 for (uint16_t i = 0; i < fileManager->typedPathNum; i++) {
1552 if (fileManager->pathList[i].pathType == pathType) {
1553 return fileManager->pathList[i].storagePath;
1554 }
1555 }
1556 return NULL;
1557 }
1558
FileManagerRecvFileTask(FileManager * fileManager,RecvFileListInfo * fileListInfo,FileListMsgPara * msgPara)1559 int32_t FileManagerRecvFileTask(FileManager *fileManager, RecvFileListInfo *fileListInfo, FileListMsgPara *msgPara)
1560 {
1561 FileListTask *fmFileList = NULL;
1562 uint16_t standardBlockSize;
1563 const char *storagePath = NULL;
1564 if (CheckReceiverManager(fileManager) != NSTACKX_EOK || fileManager->maxRecvBlockListSize == 0) {
1565 return NSTACKX_EINVAL;
1566 }
1567
1568 if (fileManager->taskList.size == fileManager->taskList.maxSize) {
1569 LOGE(TAG, "task list is full");
1570 return NSTACKX_EFAILED;
1571 }
1572
1573 storagePath = GetStoragePathByType(fileManager, fileListInfo->pathType);
1574 if (storagePath == NULL) {
1575 LOGE(TAG, "can't get storage path for pathType %u", fileListInfo->pathType);
1576 return NSTACKX_EFAILED;
1577 }
1578
1579 standardBlockSize = GetStandardBlockSize(fileManager);
1580 if (standardBlockSize == 0) {
1581 LOGE(TAG, "max frame length is too small");
1582 return NSTACKX_EFAILED;
1583 }
1584 fmFileList = CreateRecvFileList(fileListInfo, storagePath, standardBlockSize, msgPara,
1585 fileManager->maxRecvBlockListSize);
1586 if (fmFileList == NULL) {
1587 LOGE(TAG, "Creat file list error");
1588 return NSTACKX_EFAILED;
1589 }
1590 fmFileList->epollfd = fileManager->epollfd;
1591 fmFileList->eventNodeChain = fileManager->eventNodeChain;
1592 fmFileList->maxFrameLength = fileManager->maxFrameLength;
1593 if (fileManager->keyLen > 0 && SetCryptPara(fmFileList, fileManager->key, fileManager->keyLen) != NSTACKX_EOK) {
1594 ClearRecvFileList(fmFileList);
1595 LOGE(TAG, "fail to set crypto para");
1596 return NSTACKX_EFAILED;
1597 }
1598
1599 if (MutexListAddNode(&fileManager->taskList, &fmFileList->list, NSTACKX_FALSE) != NSTACKX_EOK) {
1600 LOGE(TAG, "Add tast to list error");
1601 ClearRecvFileList(fmFileList);
1602 fileManager->errCode = FILE_MANAGER_EMUTEX;
1603 NotifyFileManagerMsg(fileManager, FILE_MANAGER_INNER_ERROR);
1604 return NSTACKX_EFAILED;
1605 }
1606 fileManager->totalBytes += fmFileList->totalBytes;
1607 SemPost(&fileManager->semTaskListNotEmpty);
1608 return NSTACKX_EOK;
1609 }
1610
PushRecvBlockFrame(FileListTask * fileList,FileDataFrame * frame)1611 static int32_t PushRecvBlockFrame(FileListTask *fileList, FileDataFrame *frame)
1612 {
1613 BlockFrame *blockFrame = NULL;
1614 int32_t ret;
1615 uint8_t isRetran;
1616
1617 blockFrame = (BlockFrame *)calloc(1, sizeof(BlockFrame));
1618 if (blockFrame == NULL) {
1619 LOGE(TAG, "memory calloc failed");
1620 return FILE_MANAGER_ENOMEM;
1621 }
1622 blockFrame->fileDataFrame = frame;
1623 isRetran = frame->header.flag & NSTACKX_DFILE_DATA_FRAME_RETRAN_FLAG;
1624 if (isRetran) {
1625 ret = MutexListAddNode(&fileList->recvBlockList, &blockFrame->list, NSTACKX_TRUE);
1626 } else {
1627 ret = MutexListAddNode(&fileList->recvBlockList, &blockFrame->list, NSTACKX_FALSE);
1628 }
1629 if (ret != NSTACKX_EOK) {
1630 free(blockFrame);
1631 LOGE(TAG, "add node to recv block list failed");
1632 return FILE_MANAGER_EMUTEX;
1633 }
1634 SemPost(&fileList->semStop);
1635 return FILE_MANAGER_EOK;
1636 }
1637
CheckFileBlockListOverflow(FileManager * fileManager)1638 static int32_t CheckFileBlockListOverflow(FileManager *fileManager)
1639 {
1640 uint32_t recvListAllSize;
1641 uint32_t recvInnerAllSize;
1642
1643 if (GetFileBlockListSize(&fileManager->taskList, &recvListAllSize, &recvInnerAllSize) != NSTACKX_EOK) {
1644 fileManager->errCode = FILE_MANAGER_EMUTEX;
1645 NotifyFileManagerMsg(fileManager, FILE_MANAGER_INNER_ERROR);
1646 LOGE(TAG, "failed to get GetFileBlockListSize");
1647 return NSTACKX_EFAILED;
1648 }
1649 if (recvListAllSize >= fileManager->maxRecvBlockListSize) {
1650 return NSTACKX_EFAILED;
1651 }
1652
1653 return NSTACKX_EOK;
1654 }
1655
FileManagerFileWrite(FileManager * fileManager,FileDataFrame * frame)1656 int32_t FileManagerFileWrite(FileManager *fileManager, FileDataFrame *frame)
1657 {
1658 uint16_t transId;
1659 FileListTask *fileList = NULL;
1660 uint8_t isErrorOccured = NSTACKX_FALSE;
1661 BlockFrame block = {
1662 .list = {NULL, NULL},
1663 .fileDataFrame = frame,
1664 };
1665 if (CheckReceiverManager(fileManager) != NSTACKX_EOK) {
1666 LOGE(TAG, "invalid input");
1667 return NSTACKX_EINVAL;
1668 }
1669 transId = ntohs(frame->header.transId);
1670 fileList = GetFileListById(&fileManager->taskList, transId, &isErrorOccured);
1671 if (isErrorOccured || fileList == NULL) {
1672 fileManager->errCode = FILE_MANAGER_EMUTEX;
1673 NotifyFileManagerMsg(fileManager, FILE_MANAGER_INNER_ERROR);
1674 LOGE(TAG, "failed to get target fileList %u", transId);
1675 return NSTACKX_EFAILED;
1676 }
1677 if (CheckFilelistNotStop(fileList) != NSTACKX_EOK) {
1678 LOGE(TAG, "target file list %u is not available", transId);
1679 return NSTACKX_EFAILED;
1680 }
1681
1682 if (CheckFileBlockListOverflow(fileManager) != NSTACKX_EOK) {
1683 return NSTACKX_EFAILED;
1684 }
1685 RefreshBytesTransFerred(fileManager, &block);
1686 fileList->errCode = PushRecvBlockFrame(fileList, frame);
1687 if (fileList->errCode != FILE_MANAGER_EOK) {
1688 LOGE(TAG, "add frame to recv block list failed");
1689 NotifyFileListMsg(fileList, FILE_MANAGER_RECEIVE_FAIL);
1690 return NSTACKX_EFAILED;
1691 }
1692 return NSTACKX_EOK;
1693 }
1694
ClearTask(FileManager * fileManager)1695 static void ClearTask(FileManager *fileManager)
1696 {
1697 FileListTask *fileList = NULL;
1698
1699 while (fileManager->taskList.size > 0) {
1700 if (PthreadMutexLock(&fileManager->taskList.lock) != 0) {
1701 LOGE(TAG, "pthread mutex lock error");
1702 return;
1703 }
1704 fileList = (FileListTask *)ListPopFront(&fileManager->taskList.head);
1705 fileManager->taskList.size--;
1706 if (PthreadMutexUnlock(&fileManager->taskList.lock) != 0) {
1707 LOGE(TAG, "pthread mutex unlock error");
1708 }
1709 if (fileList == NULL) {
1710 continue;
1711 }
1712 ClearFileList(fileManager, fileList);
1713 fileList = NULL;
1714 }
1715 MutexListDestory(&fileManager->taskList);
1716 }
1717
FileManagerDestroy(FileManager * fileManager)1718 void FileManagerDestroy(FileManager *fileManager)
1719 {
1720 if (fileManager == NULL) {
1721 return;
1722 }
1723 if (fileManager->isSender) {
1724 ClearSendFrameList(fileManager);
1725 }
1726 ClearTask(fileManager);
1727 AtomicParameterDestory(fileManager);
1728 free(fileManager->commonStoragePath);
1729 fileManager->commonStoragePath = NULL;
1730 for (uint16_t i = 0; i < fileManager->typedPathNum; i++) {
1731 free(fileManager->pathList[i].storagePath);
1732 fileManager->pathList[i].storagePath = NULL;
1733 }
1734 (void)memset_s(fileManager, sizeof(FileManager), 0, sizeof(FileManager));
1735 free(fileManager);
1736 fileManager = NULL;
1737 LOGI(TAG, "Destroy successfully!");
1738 }
1739
FileManagerIsRecvBlockWritable(FileManager * fileManager,uint16_t transId)1740 uint8_t FileManagerIsRecvBlockWritable(FileManager *fileManager, uint16_t transId)
1741 {
1742 if (fileManager == NULL || fileManager->isSender || transId == 0) {
1743 return NSTACKX_FALSE;
1744 }
1745
1746 if (CheckFileBlockListOverflow(fileManager) != NSTACKX_EOK) {
1747 return NSTACKX_FALSE;
1748 }
1749
1750 return NSTACKX_TRUE;
1751 }
1752
FileManagerGetTotalBytes(FileManager * fileManager,uint64_t * totalBytes)1753 int32_t FileManagerGetTotalBytes(FileManager *fileManager, uint64_t *totalBytes)
1754 {
1755 if (fileManager == NULL) {
1756 return NSTACKX_EFAILED;
1757 }
1758 *totalBytes = fileManager->totalBytes;
1759 return NSTACKX_EOK;
1760 }
1761
FileManagerGetBytesTransferred(FileManager * fileManager,uint64_t * bytesTransferred)1762 int32_t FileManagerGetBytesTransferred(FileManager *fileManager, uint64_t *bytesTransferred)
1763 {
1764 FileListTask *fileList = NULL;
1765 uint64_t runningTaskBytesTransferred = 0;
1766 uint64_t ret;
1767 List *list = NULL;
1768 if (fileManager == NULL || bytesTransferred == NULL) {
1769 return 0;
1770 }
1771 if (PthreadMutexLock(&fileManager->taskList.lock) != 0) {
1772 LOGE(TAG, "pthread mutex lock error");
1773 goto L_ERR_FILE_MANAGER;
1774 }
1775 LIST_FOR_EACH(list, &fileManager->taskList.head) {
1776 fileList = (FileListTask *)list;
1777 if (fileList == NULL || fileList->runStatus == FILE_LIST_STATUS_STOP) {
1778 continue;
1779 }
1780 runningTaskBytesTransferred += FileListGetBytesTransferred(fileList, fileManager->isSender);
1781 }
1782 if (PthreadMutexUnlock(&fileManager->taskList.lock) != 0) {
1783 LOGE(TAG, "pthread mutex unlock error");
1784 goto L_ERR_FILE_MANAGER;
1785 }
1786
1787 ret = runningTaskBytesTransferred + fileManager->stoppedTasksBytesTransferred;
1788 if (ret > fileManager->totalBytes) {
1789 LOGE(TAG, "result is too large");
1790 return NSTACKX_EFAILED;
1791 }
1792 /* for sender, can't return all bytes before there is unstopped task. */
1793 if (ret == fileManager->totalBytes && fileManager->isSender && runningTaskBytesTransferred > 0) {
1794 if (ret > NSTACKX_DEFAULT_FRAME_SIZE) {
1795 ret -= NSTACKX_DEFAULT_FRAME_SIZE;
1796 } else {
1797 ret = 0;
1798 }
1799 }
1800 if (ret <= fileManager->bytesTransferredLastRecord) {
1801 ret = fileManager->bytesTransferredLastRecord;
1802 } else {
1803 fileManager->bytesTransferredLastRecord = ret;
1804 }
1805 *bytesTransferred = ret;
1806 return NSTACKX_EOK;
1807 L_ERR_FILE_MANAGER:
1808 fileManager->errCode = FILE_MANAGER_EMUTEX;
1809 NotifyFileManagerMsg(fileManager, FILE_MANAGER_INNER_ERROR);
1810 return NSTACKX_EFAILED;
1811 }
1812
FileManagerGetTransUpdateInfo(FileManager * fileManager,uint16_t transId,uint64_t * totalBytes,uint64_t * bytesTransferred)1813 int32_t FileManagerGetTransUpdateInfo(FileManager *fileManager, uint16_t transId, uint64_t *totalBytes,
1814 uint64_t *bytesTransferred)
1815 {
1816 FileListTask *fileList = NULL;
1817 uint8_t isFound = NSTACKX_FALSE;
1818 List *list = NULL;
1819 uint64_t totalBytesPtr, bytesTransPtr;
1820 if (fileManager == NULL || totalBytes == NULL || bytesTransferred == NULL) {
1821 return NSTACKX_EFAILED;
1822 }
1823 if (PthreadMutexLock(&fileManager->taskList.lock) != 0) {
1824 LOGE(TAG, "pthread mutex lock error");
1825 goto L_ERR_FILE_MANAGER;
1826 }
1827 LIST_FOR_EACH(list, &fileManager->taskList.head) {
1828 fileList = (FileListTask *)list;
1829 if (fileList != NULL && fileList->transId == transId) {
1830 bytesTransPtr = FileListGetBytesTransferred(fileList, fileManager->isSender);
1831 totalBytesPtr = fileList->totalBytes;
1832 isFound = NSTACKX_TRUE;
1833 break;
1834 }
1835 }
1836 if (PthreadMutexUnlock(&fileManager->taskList.lock) != 0) {
1837 LOGE(TAG, "pthread mutex unlock error");
1838 goto L_ERR_FILE_MANAGER;
1839 }
1840
1841 if (!isFound || bytesTransPtr > totalBytesPtr) {
1842 LOGE(TAG, "can't find target trans %u or the result is illegal", transId);
1843 return NSTACKX_EFAILED;
1844 }
1845 *totalBytes = totalBytesPtr;
1846 *bytesTransferred = bytesTransPtr;
1847 return NSTACKX_EOK;
1848
1849 L_ERR_FILE_MANAGER:
1850 fileManager->errCode = FILE_MANAGER_EMUTEX;
1851 NotifyFileManagerMsg(fileManager, FILE_MANAGER_INNER_ERROR);
1852 return NSTACKX_EFAILED;
1853 }
1854
GetBlockHeadFlag(uint8_t isStartFrame,uint8_t isEndFrame)1855 uint8_t GetBlockHeadFlag(uint8_t isStartFrame, uint8_t isEndFrame)
1856 {
1857 uint8_t headFlag;
1858
1859 if (isEndFrame) {
1860 headFlag = NSTACKX_DFILE_DATA_FRAME_END_FLAG;
1861 } else if (isStartFrame) {
1862 headFlag = NSTACKX_DFILE_DATA_FRAME_START_FLAG;
1863 } else {
1864 headFlag = NSTACKX_DFILE_DATA_FRAME_CONTINUE_FLAG;
1865 }
1866 return headFlag;
1867 }
1868
FileManagerCLearReadOutSet(FileListTask * fileList)1869 void FileManagerCLearReadOutSet(FileListTask *fileList)
1870 {
1871 if (PthreadMutexLock(&fileList->newReadOutSet.lock) != 0) {
1872 LOGE(TAG, "pthread mutex lock error");
1873 return;
1874 }
1875
1876 fileList->newReadOutSet.blockSequence = 0;
1877 fileList->newReadOutSet.fileId = 0;
1878
1879 if (PthreadMutexUnlock(&fileList->newReadOutSet.lock) != 0) {
1880 LOGE(TAG, "pthread mutex unlock error");
1881 return;
1882 }
1883 }
1884
FileGetRecvStatus(FileInfo * fileInfo)1885 static FileRecvState FileGetRecvStatus(FileInfo *fileInfo)
1886 {
1887 if (fileInfo->errCode != FILE_MANAGER_EOK) {
1888 return STATE_RECEIVE_DONE_FAIL;
1889 } else {
1890 if (fileInfo->fileSize == 0 || fileInfo->receivedBlockNum == fileInfo->totalBlockNum) {
1891 return STATE_RECEIVE_DONE_SUCCESSFULLY;
1892 } else {
1893 return STATE_RECEIVE_ONGOING;
1894 }
1895 }
1896 }
1897
TaskGetReceivedFiles(FileListTask * fileList,uint16_t fileIdList[],uint8_t fileIdSuccessFlag[],uint32_t * fileNum)1898 static int32_t TaskGetReceivedFiles(FileListTask *fileList, uint16_t fileIdList[],
1899 uint8_t fileIdSuccessFlag[], uint32_t *fileNum)
1900 {
1901 uint32_t count = 0;
1902 FileRecvState state;
1903
1904 if (fileNum == NULL || *fileNum == 0) {
1905 return NSTACKX_EFAILED;
1906 }
1907
1908 if (fileList == NULL) {
1909 *fileNum = 0;
1910 return NSTACKX_EFAILED;
1911 }
1912
1913 for (uint32_t i = 0; i < fileList->fileNum; i++) {
1914 state = FileGetRecvStatus(&fileList->fileInfo[i]);
1915 if (state == STATE_RECEIVE_ONGOING) {
1916 continue;
1917 }
1918 fileIdList[count] = fileList->fileInfo[i].fileId;
1919 if (state == STATE_RECEIVE_DONE_SUCCESSFULLY) {
1920 fileIdSuccessFlag[count] = NSTACKX_TRUE;
1921 } else {
1922 fileIdSuccessFlag[count] = NSTACKX_FALSE;
1923 }
1924 count++;
1925 if (count >= *fileNum) {
1926 return NSTACKX_EOK;
1927 }
1928 }
1929 *fileNum = count;
1930 return NSTACKX_EOK;
1931 }
1932
FileManagerGetReceivedFiles(FileManager * fileManager,uint16_t transId,uint16_t fileIdList[],uint8_t fileIdSuccessFlag[],uint32_t * fileNum)1933 int32_t FileManagerGetReceivedFiles(FileManager *fileManager, uint16_t transId, uint16_t fileIdList[],
1934 uint8_t fileIdSuccessFlag[], uint32_t *fileNum)
1935 {
1936 FileListTask *fileList = NULL;
1937 uint8_t isErrorOccured;
1938
1939 if (fileNum == NULL || *fileNum == 0) {
1940 return NSTACKX_EFAILED;
1941 }
1942
1943 if (fileManager == NULL || fileManager->isSender) {
1944 *fileNum = 0;
1945 return NSTACKX_EFAILED;
1946 }
1947
1948 fileList = GetFileListById(&fileManager->taskList, transId, &isErrorOccured);
1949 if (isErrorOccured) {
1950 fileManager->errCode = FILE_MANAGER_EMUTEX;
1951 NotifyFileManagerMsg(fileManager, FILE_MANAGER_INNER_ERROR);
1952 *fileNum = 0;
1953 return NSTACKX_EFAILED;
1954 }
1955 if (fileList == NULL) {
1956 *fileNum = 0;
1957 return NSTACKX_EFAILED;
1958 }
1959 return TaskGetReceivedFiles(fileList, fileIdList, fileIdSuccessFlag, fileNum);
1960 }
1961
FileManagerSetAllDataReceived(FileManager * fileManager,uint16_t transId)1962 int32_t FileManagerSetAllDataReceived(FileManager *fileManager, uint16_t transId)
1963 {
1964 FileListTask *fileList = NULL;
1965 int32_t ret = NSTACKX_EFAILED;
1966 List *list = NULL;
1967 if (CheckReceiverManager(fileManager) != NSTACKX_EOK) {
1968 LOGE(TAG, "invalid input");
1969 return NSTACKX_EINVAL;
1970 }
1971 if (PthreadMutexLock(&fileManager->taskList.lock) != 0) {
1972 LOGE(TAG, "pthread mutex lock error");
1973 goto L_ERR_FILE_MANAGER;
1974 }
1975 LIST_FOR_EACH(list, &fileManager->taskList.head) {
1976 fileList = (FileListTask *)list;
1977 if (fileList->transId == transId) {
1978 fileList->allFileDataReceived = NSTACKX_TRUE;
1979 SemPost(&fileList->semStop);
1980 ret = NSTACKX_EOK;
1981 break;
1982 }
1983 }
1984 if (PthreadMutexUnlock(&fileManager->taskList.lock) != 0) {
1985 LOGE(TAG, "pthread mutex unlock error");
1986 goto L_ERR_FILE_MANAGER;
1987 }
1988 return ret;
1989 L_ERR_FILE_MANAGER:
1990 fileManager->errCode = FILE_MANAGER_EMUTEX;
1991 NotifyFileManagerMsg(fileManager, FILE_MANAGER_INNER_ERROR);
1992 return NSTACKX_EFAILED;
1993 }
1994
ClearTransStateList(DFileSession * session)1995 void ClearTransStateList(DFileSession *session)
1996 {
1997 List *tmp = NULL;
1998 List *pos = NULL;
1999 if (session == NULL || ListIsEmpty(&session->tranIdStateList.head))
2000 return;
2001
2002 if (PthreadMutexLock(&session->tranIdStateList.lock) != 0) {
2003 LOGE(TAG, "pthread mutex lock error");
2004 return;
2005 }
2006
2007 LIST_FOR_EACH_SAFE(pos, tmp, &session->tranIdStateList.head) {
2008 TransStateNode *node = (TransStateNode *)pos;
2009 ListRemoveNode(&node->list);
2010 free(node);
2011 session->tranIdStateList.size--;
2012 }
2013
2014 if (PthreadMutexUnlock(&session->tranIdStateList.lock) != 0) {
2015 LOGE(TAG, "pthread mutex unlock error");
2016 return;
2017 }
2018
2019 return;
2020 }
2021
SetTransIdState(DFileSession * session,uint16_t transId,TransIdState state)2022 int32_t SetTransIdState(DFileSession *session, uint16_t transId, TransIdState state)
2023 {
2024 List *curFront = NULL;
2025 uint8_t errorFlag = NSTACKX_FALSE;
2026 uint8_t isPoped;
2027
2028 if (session == NULL)
2029 return NSTACKX_EFAILED;
2030
2031 if (session->sessionType == DFILE_SESSION_TYPE_CLIENT) {
2032 return NSTACKX_EOK;
2033 }
2034 TransIdState curState = STATE_TRANS_INIT;
2035 TransStateNode *node = GetTransIdState(session, transId, &curState);
2036 if (node == NULL) {
2037 node = (TransStateNode *)calloc(1, sizeof(TransStateNode));
2038 if (node == NULL) {
2039 return NSTACKX_EFAILED;
2040 }
2041 node->transId = transId;
2042 node->transIdState = state;
2043 if (MutexListAddNode(&session->tranIdStateList, &node->list, 0) != NSTACKX_EOK) {
2044 free(node);
2045 return NSTACKX_EFAILED;
2046 }
2047 if (session->tranIdStateList.size == session->tranIdStateList.maxSize) {
2048 if (MutexListPopFront(&session->tranIdStateList, &curFront, &isPoped) != NSTACKX_EOK) {
2049 LOGE(TAG, "Pop tranIdStateList head error");
2050 errorFlag = NSTACKX_TRUE;
2051 }
2052 if (isPoped) {
2053 TransStateNode *tmp = (TransStateNode *)curFront;
2054 free(tmp);
2055 tmp = NULL;
2056 }
2057 }
2058 return (errorFlag ? NSTACKX_EFAILED : NSTACKX_EOK);
2059 }
2060
2061 if (curState == state) {
2062 return NSTACKX_EOK;
2063 }
2064
2065 node->transIdState = state;
2066 return NSTACKX_EOK;
2067 }
2068
GetTransIdState(DFileSession * session,uint16_t transId,TransIdState * state)2069 TransStateNode *GetTransIdState(DFileSession *session, uint16_t transId, TransIdState *state)
2070 {
2071 List *pos = NULL;
2072 TransStateNode *node = NULL;
2073 uint8_t find = NSTACKX_FALSE;
2074
2075 if (session == NULL || ListIsEmpty(&session->tranIdStateList.head))
2076 return NULL;
2077
2078 if (PthreadMutexLock(&session->tranIdStateList.lock) != 0) {
2079 LOGE(TAG, "pthread mutex lock error");
2080 return NULL;
2081 }
2082 LIST_FOR_EACH(pos, &session->tranIdStateList.head) {
2083 node = (TransStateNode *)pos;
2084 if (node->transId == transId) {
2085 *state = node->transIdState;
2086 find = NSTACKX_TRUE;
2087 break;
2088 }
2089 }
2090
2091 if (PthreadMutexUnlock(&session->tranIdStateList.lock) != 0) {
2092 LOGE(TAG, "pthread mutex unlock error");
2093 return NULL;
2094 }
2095 return (find ? node : NULL);
2096 }
2097
IsTransIdDone(DFileSession * session,uint16_t transId)2098 int32_t IsTransIdDone(DFileSession *session, uint16_t transId)
2099 {
2100 TransIdState state = STATE_TRANS_INIT;
2101 TransStateNode *node = NULL;
2102
2103 if (session != NULL && session->sessionType == DFILE_SESSION_TYPE_CLIENT) {
2104 return NSTACKX_EOK;
2105 }
2106 node = GetTransIdState(session, transId, &state);
2107 if (node == NULL) {
2108 return NSTACKX_EFAILED;
2109 }
2110
2111 if (state == STATE_TRANS_DONE) {
2112 LOGE(TAG, "trans %u is transfer done already", transId);
2113 return NSTACKX_EOK;
2114 }
2115
2116 return NSTACKX_EFAILED;
2117 }
2118