cmd_file.c 5.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253
  1. /*
  2. *******************************************************************************
  3. \file cmd_file.c
  4. \brief Command-line interface to Bee2: file management
  5. \project bee2/cmd
  6. \created 2022.06.08
  7. \version 2024.06.14
  8. \copyright The Bee2 authors
  9. \license Licensed under the Apache License, Version 2.0 (see LICENSE.txt).
  10. *******************************************************************************
  11. */
  12. #include "../cmd.h"
  13. #include <bee2/core/blob.h>
  14. #include <bee2/core/err.h>
  15. #include <bee2/core/mem.h>
  16. #include <bee2/core/str.h>
  17. #include <bee2/core/util.h>
  18. #include <stdio.h>
  19. #include <stdlib.h>
  20. /*
  21. *******************************************************************************
  22. Размер файла
  23. *******************************************************************************
  24. */
  25. size_t cmdFileSize(const char* file)
  26. {
  27. FILE* fp;
  28. long size;
  29. ASSERT(strIsValid(file));
  30. if (!(fp = fopen(file, "rb")))
  31. return SIZE_MAX;
  32. if (fseek(fp, 0, SEEK_END))
  33. {
  34. fclose(fp);
  35. return SIZE_MAX;
  36. }
  37. size = ftell(fp);
  38. if (fclose(fp) != 0)
  39. size = -1;
  40. return (size < 0) ? SIZE_MAX : (size_t)size;
  41. }
  42. /*
  43. *******************************************************************************
  44. Чтение / запись
  45. *******************************************************************************
  46. */
  47. err_t cmdFileWrite(const char* file, const void* buf, size_t count)
  48. {
  49. FILE* fp;
  50. // pre
  51. ASSERT(strIsValid(file));
  52. ASSERT(memIsValid(buf, count));
  53. // записать
  54. if (!(fp = fopen(file, "wb")))
  55. return ERR_FILE_CREATE;
  56. if (count != fwrite(buf, 1, count, fp))
  57. {
  58. fclose(fp);
  59. return ERR_FILE_WRITE;
  60. }
  61. if (fclose(fp) != 0)
  62. return ERR_BAD_FILE;
  63. return ERR_OK;
  64. }
  65. err_t cmdFileAppend(const char* file, const void* buf, size_t count)
  66. {
  67. FILE* fp;
  68. // pre
  69. ASSERT(strIsValid(file));
  70. ASSERT(memIsValid(buf, count));
  71. // дописать
  72. if (!(fp = fopen(file, "ab")))
  73. return ERR_FILE_OPEN;
  74. if (count != fwrite(buf, 1, count, fp))
  75. {
  76. fclose(fp);
  77. return ERR_FILE_WRITE;
  78. }
  79. if (fclose(fp) != 0)
  80. return ERR_BAD_FILE;
  81. return ERR_OK;
  82. }
  83. err_t cmdFileReadAll(void* buf, size_t* count, const char* file)
  84. {
  85. // pre
  86. ASSERT(memIsValid(count, O_PER_S));
  87. ASSERT(strIsValid(file));
  88. // читать
  89. if (buf)
  90. {
  91. FILE* fp;
  92. ASSERT(memIsValid(buf, *count));
  93. if (!(fp = fopen(file, "rb")))
  94. return ERR_FILE_OPEN;
  95. if (fread(buf, 1, *count, fp) != *count || getc(fp) != EOF)
  96. {
  97. fclose(fp);
  98. return ERR_FILE_READ;
  99. }
  100. if (fclose(fp) != 0)
  101. return ERR_BAD_FILE;
  102. }
  103. // определить длину файла
  104. else
  105. {
  106. size_t size;
  107. if ((size = cmdFileSize(file)) == SIZE_MAX)
  108. return ERR_FILE_READ;
  109. *count = size;
  110. }
  111. return ERR_OK;
  112. }
  113. /*
  114. *******************************************************************************
  115. Дублирование
  116. *******************************************************************************
  117. */
  118. err_t cmdFileDup(const char* ofile, const char* ifile, size_t skip,
  119. size_t count)
  120. {
  121. const size_t buf_size = 4096;
  122. err_t code;
  123. FILE* ifp;
  124. FILE* ofp;
  125. void* buf;
  126. // pre
  127. ASSERT(strIsValid(ifile) && strIsValid(ofile));
  128. // переполнение?
  129. if ((size_t)(long)skip != skip)
  130. return ERR_OVERFLOW;
  131. // открыть входной файл
  132. ifp = fopen(ifile, "rb");
  133. if (!ifp)
  134. return ERR_FILE_OPEN;
  135. // пропустить skip октетов
  136. if (fseek(ifp, (long)skip, SEEK_SET))
  137. {
  138. fclose(ifp);
  139. return ERR_FILE_READ;
  140. }
  141. // открыть выходной файл
  142. ofp = fopen(ofile, "wb");
  143. if (!ofp)
  144. {
  145. fclose(ifp);
  146. return ERR_FILE_CREATE;
  147. }
  148. // подготовить память
  149. code = cmdBlobCreate(buf, buf_size);
  150. ERR_CALL_HANDLE(code, (fclose(ofp), fclose(ifp)));
  151. // дублировать count октетов
  152. if (count != SIZE_MAX)
  153. while (count && code == ERR_OK)
  154. {
  155. size_t c = MIN2(buf_size, count);
  156. if (fread(buf, 1, c, ifp) != c)
  157. code = ERR_FILE_READ;
  158. else if (fwrite(buf, 1, c, ofp) != c)
  159. code = ERR_FILE_WRITE;
  160. count -= c;
  161. }
  162. // дублировать все
  163. else
  164. while (count && code == ERR_OK)
  165. {
  166. count = fread(buf, 1, buf_size, ifp);
  167. if (count != buf_size && !feof(ifp))
  168. code = ERR_FILE_READ;
  169. else if (fwrite(buf, 1, count, ofp) != count)
  170. code = ERR_FILE_WRITE;
  171. }
  172. // завершить
  173. cmdBlobClose(buf);
  174. if (fclose(ofp) != 0)
  175. code = (code != ERR_OK) ? code : ERR_BAD_FILE;
  176. if (fclose(ifp) != 0)
  177. code = (code != ERR_OK) ? code : ERR_BAD_FILE;
  178. return code;
  179. }
  180. /*
  181. *******************************************************************************
  182. Проверки
  183. *******************************************************************************
  184. */
  185. err_t cmdFileValNotExist(int count, char* files[])
  186. {
  187. FILE* fp;
  188. int ch;
  189. for (; count--; files++)
  190. {
  191. ASSERT(strIsValid(*files));
  192. if (fp = fopen(*files, "rb"))
  193. {
  194. if (fclose(fp) != 0)
  195. return ERR_BAD_FILE;
  196. if (printf("Some files already exist. Overwrite [y/n]?") < 0)
  197. return ERR_FILE_EXISTS;
  198. do
  199. ch = cmdTermGetch();
  200. while (ch != 'Y' && ch != 'y' && ch != 'N' && ch != 'n' && ch != '\n');
  201. printf("\n");
  202. if (ch == 'N' || ch == 'n' || ch == '\n')
  203. return ERR_FILE_EXISTS;
  204. break;
  205. }
  206. }
  207. return ERR_OK;
  208. }
  209. err_t cmdFileValExist(int count, char* files[])
  210. {
  211. FILE* fp;
  212. for (; count--; files++)
  213. {
  214. ASSERT(strIsValid(*files));
  215. if (!(fp = fopen(*files, "rb")))
  216. return ERR_FILE_NOT_FOUND;
  217. if (fclose(fp) != 0)
  218. return ERR_BAD_FILE;
  219. }
  220. return ERR_OK;
  221. }
  222. bool_t cmdFileAreSame(const char* file1, const char* file2)
  223. {
  224. bool_t ret;
  225. #ifdef OS_UNIX
  226. char* path1 = realpath(file1, 0);
  227. char* path2 = realpath(file2, 0);
  228. ret = path1 && path2 && strEq(path1, path2);
  229. free(path1), free(path2);
  230. #elif defined OS_WIN
  231. char* path1 = _fullpath(0, file1, 0);
  232. char* path2 = _fullpath(0, file2, 0);
  233. ret = path1 && path2 && strEq(path1, path2);
  234. free(path1), free(path2);
  235. #else
  236. ret = strEq(file1, file2);
  237. #endif
  238. return ret;
  239. }