miniffs_tools.c 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116
  1. /******************************************************************************
  2. * MiniFFS : Mini Flat File System
  3. * miniffs_tools.c: Contain all the function needed for offline tools
  4. *
  5. * Copyright (c) 2008-2022 986-Studio. All rights reserved.
  6. *
  7. ******************************************************************************/
  8. #include <stdio.h>
  9. #include <stdlib.h>
  10. #include <stdbool.h>
  11. #include <string.h>
  12. #define __miniffs_internal
  13. #include <miniffs.h>
  14. miniffs_t *miniffs_createfs()
  15. {
  16. miniffs_t *fs = (miniffs_t *)calloc(1, sizeof(miniffs_t));
  17. fs->file_list_size = 2;
  18. fs->files = (fs_fent_t *)calloc(2, sizeof(fs_fent_t));
  19. return fs;
  20. }
  21. int miniffs_addfile(miniffs_t *fs, char *name, char *ext, char *host_path)
  22. {
  23. fs_fent_t *cur;
  24. uint8_t nameLen = strnlen(name, MINIFFS_FILENAME_LENGTH);
  25. uint8_t extLen = strnlen(ext, MINIFFS_EXTENSION_LENGTH);
  26. if (fs->file_list_count == fs->file_list_size)
  27. {
  28. fs->file_list_size *= 2;
  29. fs->files = realloc(fs->files, sizeof(fs_fent_t) * fs->file_list_size);
  30. }
  31. cur = &fs->files[fs->file_list_count];
  32. memset(cur, 0, sizeof(fs_fent_t));
  33. memcpy(cur->name, name, nameLen);
  34. memcpy(cur->ext, ext, extLen);
  35. cur->size = host_map_file(host_path, &cur->file_pointer);
  36. cur->deleted = false;
  37. fs->file_list_count++;
  38. fs->file_count++;
  39. }
  40. int miniffs_delfile(miniffs_t *fs, char *name, char *ext, char *host_path)
  41. {
  42. // TODO:
  43. // 1 - find file
  44. // 2 - mark as deleted
  45. }
  46. int miniffs_writeimage(miniffs_t *fs, char *host_path)
  47. {
  48. int i;
  49. FILE *fp;
  50. miniffs_header_t *header;
  51. size_t headerSize = sizeof(miniffs_header_t) + fs->file_count * sizeof(fileentry_t);
  52. size_t fileIndex = 0;
  53. size_t filePosition = headerSize;
  54. header = (miniffs_header_t *)calloc(1, headerSize);
  55. header->magic = MINIFFS_MAGIC;
  56. header->fs_version_major = MINIFFS_VERSION_MAJOR;
  57. header->fs_version_minor = MINIFFS_VERSION_MINOR;
  58. header->fs_filename_len = MINIFFS_FILENAME_LENGTH;
  59. header->fs_extention_len = MINIFFS_EXTENSION_LENGTH;
  60. header->entry_count = fs->file_count;
  61. for(i = 0; i < fs->file_list_count; i++)
  62. {
  63. if (fs->files[i].deleted == false)
  64. {
  65. memcpy(header->fent[fileIndex].name, fs->files[i].name, MINIFFS_FILENAME_LENGTH);
  66. memcpy(header->fent[fileIndex].ext, fs->files[i].ext, MINIFFS_EXTENSION_LENGTH);
  67. header->fent[fileIndex].size = fs->files[i].size;
  68. header->fent[fileIndex].offset = filePosition;
  69. fileIndex ++;
  70. filePosition += header->fent[fileIndex].size;
  71. }
  72. }
  73. fp = fopen(host_path, "wb");
  74. fwrite(header, 1, headerSize, fp);
  75. for(i = 0; i < fs->file_count; i++)
  76. {
  77. if (fs->files[i].deleted == false)
  78. {
  79. fwrite(fs->files[i].file_pointer, 1, fs->files[i].size, fp);
  80. }
  81. }
  82. fclose(fp);
  83. }
  84. int miniffs_closefs(miniffs_t *fs)
  85. {
  86. int i;
  87. for(i = 0; i < fs->file_count; i++)
  88. {
  89. if (fs->files[i].mapped)
  90. {
  91. host_unmap_file(&fs->files[i].file_pointer, fs->files[i].size);
  92. fs->files[i].mapped = false;
  93. }
  94. }
  95. free(fs->files);
  96. free(fs);
  97. }