io_utils.cpp 5.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224
  1. // Copyright (c) 2021 PaddlePaddle Authors. All Rights Reserved.
  2. //
  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. #ifdef linux
  15. #include <unistd.h>
  16. #include <dirent.h>
  17. #endif
  18. #ifdef WIN32
  19. #include <windows.h>
  20. #include <io.h>
  21. #endif
  22. #include <string.h>
  23. #include <sys/stat.h>
  24. #include <sys/types.h>
  25. #include <iostream>
  26. #include "encryption/util/include/io_utils.h"
  27. #include "encryption/include/model_code.h"
  28. #include "encryption/util/include/log.h"
  29. namespace ioutil {
  30. int read_file(const char* file_path, unsigned char** dataptr, size_t* sizeptr) {
  31. FILE* fp = NULL;
  32. fp = fopen(file_path, "rb");
  33. if (fp == NULL) {
  34. LOGD("[M]open file(%s) failed", file_path);
  35. return CODE_OPEN_FAILED;
  36. }
  37. fseek(fp, 0, SEEK_END);
  38. *sizeptr = ftell(fp);
  39. *dataptr = (unsigned char*)malloc(sizeof(unsigned char) * (*sizeptr));
  40. fseek(fp, 0, SEEK_SET);
  41. fread(*dataptr, 1, *sizeptr, fp);
  42. fclose(fp);
  43. return CODE_OK;
  44. }
  45. int read_with_pos_and_length(const char* file_path, unsigned char* dataptr,
  46. size_t pos, size_t length) {
  47. if (dataptr == NULL) {
  48. LOGD("Read file pos dataptr = NULL");
  49. return CODE_READ_FILE_PTR_IS_NULL;
  50. }
  51. FILE* fp = NULL;
  52. if ((fp = fopen(file_path, "rb")) == NULL) {
  53. LOGD("[M]open file(%s) failed", file_path);
  54. return CODE_OPEN_FAILED;
  55. }
  56. fseek(fp, pos, SEEK_SET);
  57. fread(dataptr, 1, length, fp);
  58. fclose(fp);
  59. return CODE_OK;
  60. }
  61. int read_with_pos(const char* file_path, size_t pos, unsigned char** dataptr,
  62. size_t* sizeptr) {
  63. FILE* fp = NULL;
  64. if ((fp = fopen(file_path, "rb")) == NULL) {
  65. LOGD("[M]open file(%s) failed when read_with_pos", file_path);
  66. return CODE_OPEN_FAILED;
  67. }
  68. fseek(fp, 0, SEEK_END);
  69. size_t filesize = ftell(fp);
  70. *sizeptr = filesize - pos;
  71. *dataptr = (unsigned char*)malloc(sizeof(unsigned char) * (filesize - pos));
  72. fseek(fp, pos, SEEK_SET);
  73. fread(*dataptr, 1, filesize - pos, fp);
  74. fclose(fp);
  75. return CODE_OK;
  76. }
  77. int write_file(const char* file_path, const unsigned char* dataptr,
  78. size_t sizeptr) {
  79. FILE* fp = NULL;
  80. if ((fp = fopen(file_path, "wb")) == NULL) {
  81. LOGD("[M]open file(%s) failed", file_path);
  82. return CODE_OPEN_FAILED;
  83. }
  84. fwrite(dataptr, 1, sizeptr, fp);
  85. fclose(fp);
  86. return CODE_OK;
  87. }
  88. int append_file(const char* file_path, const unsigned char* data, size_t len) {
  89. FILE* fp = fopen(file_path, "ab+");
  90. if (fp == NULL) {
  91. LOGD("[M]open file(%s) failed when append_file", file_path);
  92. return CODE_OPEN_FAILED;
  93. }
  94. fwrite(data, sizeof(char), len, fp);
  95. fclose(fp);
  96. return CODE_OK;
  97. }
  98. size_t read_file_size(const char* file_path) {
  99. FILE* fp = NULL;
  100. fp = fopen(file_path, "rb");
  101. if (fp == NULL) {
  102. LOGD("[M]open file(%s) failed when read_file_size", file_path);
  103. return 0;
  104. }
  105. fseek(fp, 0, SEEK_END);
  106. size_t filesize = ftell(fp);
  107. fclose(fp);
  108. return filesize;
  109. }
  110. int read_file_to_file(const char* src_path, const char* dst_path) {
  111. FILE* infp = NULL;
  112. if ((infp = fopen(src_path, "rb")) == NULL) {
  113. LOGD("[M]read src file failed when read_file_to_file");
  114. return CODE_OPEN_FAILED;
  115. }
  116. fseek(infp, 0, SEEK_END);
  117. size_t insize = ftell(infp);
  118. char* content = reinterpret_cast<char*>(malloc(sizeof(char) * insize));
  119. fseek(infp, 0, SEEK_SET);
  120. fread(content, 1, insize, infp);
  121. fclose(infp);
  122. FILE* outfp = NULL;
  123. if ((outfp = fopen(dst_path, "wb")) == NULL) {
  124. LOGD("[M]open dst file failed when read_file_to_file");
  125. return CODE_OPEN_FAILED;
  126. }
  127. fwrite(content, 1, insize, outfp);
  128. fclose(outfp);
  129. free(content);
  130. return CODE_OK;
  131. }
  132. int read_dir_files(const char* dir_path,
  133. std::vector<std::string>& files) { // NOLINT
  134. #ifdef linux
  135. struct dirent* ptr;
  136. DIR* dir = NULL;
  137. dir = opendir(dir_path);
  138. if (dir == NULL) {
  139. return -1; // CODE_NOT_EXIST_DIR
  140. }
  141. while ((ptr = readdir(dir)) != NULL) {
  142. if (strcmp(ptr->d_name, ".") != 0 && strcmp(ptr->d_name, "..") != 0) {
  143. files.push_back(ptr->d_name);
  144. }
  145. }
  146. closedir(dir);
  147. #endif
  148. #ifdef WIN32
  149. intptr_t handle;
  150. struct _finddata_t fileinfo;
  151. std::string tmp_dir(dir_path);
  152. std::string::size_type idx = tmp_dir.rfind("\\*");
  153. if (idx == std::string::npos || idx != tmp_dir.length() - 1) {
  154. tmp_dir.append("\\*");
  155. }
  156. handle = _findfirst(tmp_dir.c_str(), &fileinfo);
  157. if (handle == -1) {
  158. return -1;
  159. }
  160. do {
  161. std::cout << "File name = " << fileinfo.name << std::endl;
  162. if (strcmp(fileinfo.name, ".") != 0 && strcmp(fileinfo.name, "..") != 0) {
  163. files.push_back(fileinfo.name);
  164. }
  165. } while (!_findnext(handle, &fileinfo));
  166. std::cout << files.size() << std::endl;
  167. for (size_t i = 0; i < files.size(); i++) {
  168. std::cout << files[i] << std::endl;
  169. }
  170. _findclose(handle);
  171. #endif
  172. return files.size();
  173. }
  174. int dir_exist_or_mkdir(const char* dir) {
  175. #ifdef WIN32
  176. if (CreateDirectory(dir, NULL)) {
  177. // return CODE_OK;
  178. } else {
  179. return CODE_MKDIR_FAILED;
  180. }
  181. #endif
  182. #ifdef linux
  183. if (access(dir, 0) != 0) {
  184. mkdir(dir, S_IRWXU | S_IRWXG | S_IRWXO);
  185. }
  186. #endif
  187. return CODE_OK;
  188. }
  189. } // namespace ioutil