dfs_romfs.c 7.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326
  1. /*
  2. * Copyright (c) 2006-2022, RT-Thread Development Team
  3. *
  4. * SPDX-License-Identifier: Apache-2.0
  5. *
  6. * Change Logs:
  7. * Date Author Notes
  8. */
  9. #include <rtthread.h>
  10. #include <dfs.h>
  11. #include <dfs_fs.h>
  12. #include <dfs_file.h>
  13. #include "dfs_romfs.h"
  14. int dfs_romfs_mount(struct dfs_filesystem *fs, unsigned long rwflag, const void *data)
  15. {
  16. struct romfs_dirent *root_dirent;
  17. if (data == NULL)
  18. return -EIO;
  19. root_dirent = (struct romfs_dirent *)data;
  20. fs->data = root_dirent;
  21. return RT_EOK;
  22. }
  23. int dfs_romfs_unmount(struct dfs_filesystem *fs)
  24. {
  25. return RT_EOK;
  26. }
  27. int dfs_romfs_ioctl(struct dfs_fd *file, int cmd, void *args)
  28. {
  29. return -EIO;
  30. }
  31. rt_inline int check_dirent(struct romfs_dirent *dirent)
  32. {
  33. if ((dirent->type != ROMFS_DIRENT_FILE && dirent->type != ROMFS_DIRENT_DIR)
  34. || dirent->size == ~0U)
  35. return -1;
  36. return 0;
  37. }
  38. struct romfs_dirent *dfs_romfs_lookup(struct romfs_dirent *root_dirent, const char *path, rt_size_t *size)
  39. {
  40. rt_size_t index, found;
  41. const char *subpath, *subpath_end;
  42. struct romfs_dirent *dirent;
  43. rt_size_t dirent_size;
  44. /* Check the root_dirent. */
  45. if (check_dirent(root_dirent) != 0)
  46. return NULL;
  47. if (path[0] == '/' && path[1] == '\0')
  48. {
  49. *size = root_dirent->size;
  50. return root_dirent;
  51. }
  52. /* goto root directory entries */
  53. dirent = (struct romfs_dirent *)root_dirent->data;
  54. dirent_size = root_dirent->size;
  55. /* get the end position of this subpath */
  56. subpath_end = path;
  57. /* skip /// */
  58. while (*subpath_end && *subpath_end == '/')
  59. subpath_end ++;
  60. subpath = subpath_end;
  61. while ((*subpath_end != '/') && *subpath_end)
  62. subpath_end ++;
  63. while (dirent != NULL)
  64. {
  65. found = 0;
  66. /* search in folder */
  67. for (index = 0; index < dirent_size; index ++)
  68. {
  69. if (check_dirent(&dirent[index]) != 0)
  70. return NULL;
  71. if (rt_strlen(dirent[index].name) == (rt_size_t)(subpath_end - subpath) &&
  72. rt_strncmp(dirent[index].name, subpath, (subpath_end - subpath)) == 0)
  73. {
  74. dirent_size = dirent[index].size;
  75. /* skip /// */
  76. while (*subpath_end && *subpath_end == '/')
  77. subpath_end ++;
  78. subpath = subpath_end;
  79. while ((*subpath_end != '/') && *subpath_end)
  80. subpath_end ++;
  81. if (!(*subpath))
  82. {
  83. *size = dirent_size;
  84. return &dirent[index];
  85. }
  86. if (dirent[index].type == ROMFS_DIRENT_DIR)
  87. {
  88. /* enter directory */
  89. dirent = (struct romfs_dirent *)dirent[index].data;
  90. found = 1;
  91. break;
  92. }
  93. else
  94. {
  95. /* return file dirent */
  96. if (subpath != NULL)
  97. break; /* not the end of path */
  98. return &dirent[index];
  99. }
  100. }
  101. }
  102. if (!found)
  103. break; /* not found */
  104. }
  105. /* not found */
  106. return NULL;
  107. }
  108. int dfs_romfs_read(struct dfs_fd *file, void *buf, size_t count)
  109. {
  110. rt_size_t length;
  111. struct romfs_dirent *dirent;
  112. dirent = (struct romfs_dirent *)file->data;
  113. RT_ASSERT(dirent != NULL);
  114. if (check_dirent(dirent) != 0)
  115. {
  116. return -EIO;
  117. }
  118. if (count < file->size - file->pos)
  119. length = count;
  120. else
  121. length = file->size - file->pos;
  122. if (length > 0)
  123. rt_memcpy(buf, &(dirent->data[file->pos]), length);
  124. /* update file current position */
  125. file->pos += length;
  126. return length;
  127. }
  128. int dfs_romfs_lseek(struct dfs_fd *file, off_t offset)
  129. {
  130. if (offset >= 0 && (rt_size_t)offset <= file->size)
  131. {
  132. file->pos = offset;
  133. return file->pos;
  134. }
  135. return -EIO;
  136. }
  137. int dfs_romfs_close(struct dfs_fd *file)
  138. {
  139. file->data = NULL;
  140. return RT_EOK;
  141. }
  142. int dfs_romfs_open(struct dfs_fd *file)
  143. {
  144. rt_size_t size;
  145. struct romfs_dirent *dirent;
  146. struct romfs_dirent *root_dirent;
  147. struct dfs_filesystem *fs;
  148. fs = (struct dfs_filesystem *)file->data;
  149. root_dirent = (struct romfs_dirent *)fs->data;
  150. if (check_dirent(root_dirent) != 0)
  151. return -EIO;
  152. if (file->flags & (O_CREAT | O_WRONLY | O_APPEND | O_TRUNC | O_RDWR))
  153. return -EINVAL;
  154. dirent = dfs_romfs_lookup(root_dirent, file->path, &size);
  155. if (dirent == NULL)
  156. return -ENOENT;
  157. /* entry is a directory file type */
  158. if (dirent->type == ROMFS_DIRENT_DIR)
  159. {
  160. if (!(file->flags & O_DIRECTORY))
  161. return -ENOENT;
  162. }
  163. else
  164. {
  165. /* entry is a file, but open it as a directory */
  166. if (file->flags & O_DIRECTORY)
  167. return -ENOENT;
  168. }
  169. file->data = dirent;
  170. file->size = size;
  171. file->pos = 0;
  172. return RT_EOK;
  173. }
  174. int dfs_romfs_stat(struct dfs_filesystem *fs, const char *path, struct stat *st)
  175. {
  176. rt_size_t size;
  177. struct romfs_dirent *dirent;
  178. struct romfs_dirent *root_dirent;
  179. root_dirent = (struct romfs_dirent *)fs->data;
  180. dirent = dfs_romfs_lookup(root_dirent, path, &size);
  181. if (dirent == NULL)
  182. return -ENOENT;
  183. st->st_dev = 0;
  184. st->st_mode = S_IFREG | S_IRUSR | S_IRGRP | S_IROTH |
  185. S_IWUSR | S_IWGRP | S_IWOTH;
  186. if (dirent->type == ROMFS_DIRENT_DIR)
  187. {
  188. st->st_mode &= ~S_IFREG;
  189. st->st_mode |= S_IFDIR | S_IXUSR | S_IXGRP | S_IXOTH;
  190. }
  191. st->st_size = dirent->size;
  192. st->st_mtime = 0;
  193. return RT_EOK;
  194. }
  195. int dfs_romfs_getdents(struct dfs_fd *file, struct dirent *dirp, uint32_t count)
  196. {
  197. rt_size_t index;
  198. rt_size_t len;
  199. const char *name;
  200. struct dirent *d;
  201. struct romfs_dirent *dirent, *sub_dirent;
  202. dirent = (struct romfs_dirent *)file->data;
  203. if (check_dirent(dirent) != 0)
  204. return -EIO;
  205. RT_ASSERT(dirent->type == ROMFS_DIRENT_DIR);
  206. /* enter directory */
  207. dirent = (struct romfs_dirent *)dirent->data;
  208. /* make integer count */
  209. count = (count / sizeof(struct dirent));
  210. if (count == 0)
  211. return -EINVAL;
  212. index = 0;
  213. for (index = 0; index < count && (rt_size_t)file->pos < file->size; index ++)
  214. {
  215. d = dirp + index;
  216. sub_dirent = &dirent[file->pos];
  217. name = sub_dirent->name;
  218. /* fill dirent */
  219. if (sub_dirent->type == ROMFS_DIRENT_DIR)
  220. d->d_type = DT_DIR;
  221. else
  222. d->d_type = DT_REG;
  223. len = rt_strlen(name);
  224. RT_ASSERT(len <= RT_UINT8_MAX);
  225. d->d_namlen = (rt_uint8_t)len;
  226. d->d_reclen = (rt_uint16_t)sizeof(struct dirent);
  227. rt_strncpy(d->d_name, name, DFS_PATH_MAX);
  228. /* move to next position */
  229. ++ file->pos;
  230. }
  231. return index * sizeof(struct dirent);
  232. }
  233. static const struct dfs_file_ops _rom_fops =
  234. {
  235. dfs_romfs_open,
  236. dfs_romfs_close,
  237. dfs_romfs_ioctl,
  238. dfs_romfs_read,
  239. NULL,
  240. NULL,
  241. dfs_romfs_lseek,
  242. dfs_romfs_getdents,
  243. NULL,
  244. };
  245. static const struct dfs_filesystem_ops _romfs =
  246. {
  247. "rom",
  248. DFS_FS_FLAG_DEFAULT,
  249. &_rom_fops,
  250. dfs_romfs_mount,
  251. dfs_romfs_unmount,
  252. NULL,
  253. NULL,
  254. NULL,
  255. dfs_romfs_stat,
  256. NULL,
  257. };
  258. int dfs_romfs_init(void)
  259. {
  260. /* register rom file system */
  261. dfs_register(&_romfs);
  262. return 0;
  263. }
  264. INIT_COMPONENT_EXPORT(dfs_romfs_init);