1
0

dfs_romfs.c 7.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326
  1. /*
  2. * File : dfs_romfs.c
  3. * This file is part of Device File System in RT-Thread RTOS
  4. * COPYRIGHT (C) 2004-2011, RT-Thread Development Team
  5. *
  6. * This program is free software; you can redistribute it and/or modify
  7. * it under the terms of the GNU General Public License as published by
  8. * the Free Software Foundation; either version 2 of the License, or
  9. * (at your option) any later version.
  10. *
  11. * This program is distributed in the hope that it will be useful,
  12. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  13. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  14. * GNU General Public License for more details.
  15. *
  16. * You should have received a copy of the GNU General Public License along
  17. * with this program; if not, write to the Free Software Foundation, Inc.,
  18. * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
  19. *
  20. * Change Logs:
  21. * Date Author Notes
  22. */
  23. #include <rtthread.h>
  24. #include <dfs.h>
  25. #include <dfs_fs.h>
  26. #include "dfs_romfs.h"
  27. int dfs_romfs_mount(struct dfs_filesystem *fs, unsigned long rwflag, const void *data)
  28. {
  29. struct romfs_dirent *root_dirent;
  30. if (data == RT_NULL)
  31. return -DFS_STATUS_EIO;
  32. root_dirent = (struct romfs_dirent *)data;
  33. fs->data = root_dirent;
  34. return DFS_STATUS_OK;
  35. }
  36. int dfs_romfs_unmount(struct dfs_filesystem *fs)
  37. {
  38. return DFS_STATUS_OK;
  39. }
  40. int dfs_romfs_ioctl(struct dfs_fd *file, int cmd, void *args)
  41. {
  42. return -DFS_STATUS_EIO;
  43. }
  44. rt_inline int check_dirent(struct romfs_dirent *dirent)
  45. {
  46. if ((dirent->type != ROMFS_DIRENT_FILE && dirent->type != ROMFS_DIRENT_DIR)
  47. || dirent->size == ~0)
  48. return -1;
  49. return 0;
  50. }
  51. struct romfs_dirent *dfs_romfs_lookup(struct romfs_dirent *root_dirent, const char *path, rt_size_t *size)
  52. {
  53. rt_size_t index, found;
  54. const char *subpath, *subpath_end;
  55. struct romfs_dirent *dirent;
  56. rt_size_t dirent_size;
  57. /* Check the root_dirent. */
  58. if (check_dirent(root_dirent) != 0)
  59. return RT_NULL;
  60. if (path[0] == '/' && path[1] == '\0')
  61. {
  62. *size = root_dirent->size;
  63. return root_dirent;
  64. }
  65. /* goto root directy entries */
  66. dirent = (struct romfs_dirent *)root_dirent->data;
  67. dirent_size = root_dirent->size;
  68. /* get the end position of this subpath */
  69. subpath_end = path;
  70. /* skip /// */
  71. while (*subpath_end && *subpath_end == '/')
  72. subpath_end ++;
  73. subpath = subpath_end;
  74. while ((*subpath_end != '/') && *subpath_end)
  75. subpath_end ++;
  76. while (dirent != RT_NULL)
  77. {
  78. found = 0;
  79. /* search in folder */
  80. for (index = 0; index < dirent_size; index ++)
  81. {
  82. if (check_dirent(&dirent[index]) != 0)
  83. return RT_NULL;
  84. if (rt_strncmp(dirent[index].name, subpath, (subpath_end - subpath)) == 0)
  85. {
  86. dirent_size = dirent[index].size;
  87. /* skip /// */
  88. while (*subpath_end && *subpath_end == '/')
  89. subpath_end ++;
  90. subpath = subpath_end;
  91. while ((*subpath_end != '/') && *subpath_end)
  92. subpath_end ++;
  93. if (!(*subpath))
  94. {
  95. *size = dirent_size;
  96. return &dirent[index];
  97. }
  98. if (dirent[index].type == ROMFS_DIRENT_DIR)
  99. {
  100. /* enter directory */
  101. dirent = (struct romfs_dirent*)dirent[index].data;
  102. found = 1;
  103. break;
  104. }
  105. else
  106. {
  107. /* return file dirent */
  108. if (subpath != RT_NULL)
  109. break; /* not the end of path */
  110. return &dirent[index];
  111. }
  112. }
  113. }
  114. if (!found)
  115. break; /* not found */
  116. }
  117. /* not found */
  118. return RT_NULL;
  119. }
  120. int dfs_romfs_read(struct dfs_fd *file, void *buf, rt_size_t count)
  121. {
  122. rt_size_t length;
  123. struct romfs_dirent *dirent;
  124. dirent = (struct romfs_dirent *)file->data;
  125. RT_ASSERT(dirent != RT_NULL);
  126. if (check_dirent(dirent) != 0)
  127. {
  128. return -DFS_STATUS_EIO;
  129. }
  130. if (count < file->size - file->pos)
  131. length = count;
  132. else
  133. length = file->size - file->pos;
  134. if (length > 0)
  135. memcpy(buf, &(dirent->data[file->pos]), length);
  136. /* update file current position */
  137. file->pos += length;
  138. return length;
  139. }
  140. int dfs_romfs_lseek(struct dfs_fd *file, rt_off_t offset)
  141. {
  142. if (offset <= file->size)
  143. {
  144. file->pos = offset;
  145. return file->pos;
  146. }
  147. return -DFS_STATUS_EIO;
  148. }
  149. int dfs_romfs_close(struct dfs_fd *file)
  150. {
  151. file->data = RT_NULL;
  152. return DFS_STATUS_OK;
  153. }
  154. int dfs_romfs_open(struct dfs_fd *file)
  155. {
  156. rt_size_t size;
  157. struct romfs_dirent *dirent;
  158. struct romfs_dirent *root_dirent;
  159. root_dirent = (struct romfs_dirent *)file->fs->data;
  160. if (check_dirent(root_dirent) != 0)
  161. return -DFS_STATUS_EIO;
  162. if (file->flags & (DFS_O_CREAT | DFS_O_WRONLY | DFS_O_APPEND | DFS_O_TRUNC | DFS_O_RDWR))
  163. return -DFS_STATUS_EINVAL;
  164. dirent = dfs_romfs_lookup(root_dirent, file->path, &size);
  165. if (dirent == RT_NULL)
  166. return -DFS_STATUS_ENOENT;
  167. /* entry is a directory file type */
  168. if (dirent->type == ROMFS_DIRENT_DIR)
  169. {
  170. if (!(file->flags & DFS_O_DIRECTORY))
  171. return -DFS_STATUS_ENOENT;
  172. }
  173. else
  174. {
  175. /* entry is a file, but open it as a directory */
  176. if (file->flags & DFS_O_DIRECTORY)
  177. return -DFS_STATUS_ENOENT;
  178. }
  179. file->data = dirent;
  180. file->size = size;
  181. file->pos = 0;
  182. return DFS_STATUS_OK;
  183. }
  184. int dfs_romfs_stat(struct dfs_filesystem *fs, const char *path, struct stat *st)
  185. {
  186. rt_size_t size;
  187. struct romfs_dirent *dirent;
  188. struct romfs_dirent *root_dirent;
  189. root_dirent = (struct romfs_dirent *)fs->data;
  190. dirent = dfs_romfs_lookup(root_dirent, path, &size);
  191. if (dirent == RT_NULL)
  192. return -DFS_STATUS_ENOENT;
  193. st->st_dev = 0;
  194. st->st_mode = DFS_S_IFREG | DFS_S_IRUSR | DFS_S_IRGRP | DFS_S_IROTH |
  195. DFS_S_IWUSR | DFS_S_IWGRP | DFS_S_IWOTH;
  196. if (dirent->type == ROMFS_DIRENT_DIR)
  197. {
  198. st->st_mode &= ~DFS_S_IFREG;
  199. st->st_mode |= DFS_S_IFDIR | DFS_S_IXUSR | DFS_S_IXGRP | DFS_S_IXOTH;
  200. }
  201. st->st_size = dirent->size;
  202. st->st_mtime = 0;
  203. return DFS_STATUS_OK;
  204. }
  205. int dfs_romfs_getdents(struct dfs_fd *file, struct dirent *dirp, rt_uint32_t count)
  206. {
  207. rt_size_t index;
  208. const char *name;
  209. struct dirent *d;
  210. struct romfs_dirent *dirent, *sub_dirent;
  211. dirent = (struct romfs_dirent *)file->data;
  212. if (check_dirent(dirent) != 0)
  213. return -DFS_STATUS_EIO;
  214. RT_ASSERT(dirent->type == ROMFS_DIRENT_DIR);
  215. /* enter directory */
  216. dirent = (struct romfs_dirent *)dirent->data;
  217. /* make integer count */
  218. count = (count / sizeof(struct dirent));
  219. if (count == 0)
  220. return -DFS_STATUS_EINVAL;
  221. index = 0;
  222. for (index = 0; index < count && file->pos < file->size; index ++)
  223. {
  224. d = dirp + index;
  225. sub_dirent = &dirent[file->pos];
  226. name = sub_dirent->name;
  227. /* fill dirent */
  228. if (sub_dirent->type == ROMFS_DIRENT_DIR)
  229. d->d_type = DFS_DT_DIR;
  230. else
  231. d->d_type = DFS_DT_REG;
  232. d->d_namlen = rt_strlen(name);
  233. d->d_reclen = (rt_uint16_t)sizeof(struct dirent);
  234. rt_strncpy(d->d_name, name, rt_strlen(name) + 1);
  235. /* move to next position */
  236. ++ file->pos;
  237. }
  238. return index * sizeof(struct dirent);
  239. }
  240. static const struct dfs_filesystem_operation _romfs =
  241. {
  242. "rom",
  243. DFS_FS_FLAG_DEFAULT,
  244. dfs_romfs_mount,
  245. dfs_romfs_unmount,
  246. RT_NULL,
  247. RT_NULL,
  248. dfs_romfs_open,
  249. dfs_romfs_close,
  250. dfs_romfs_ioctl,
  251. dfs_romfs_read,
  252. RT_NULL,
  253. RT_NULL,
  254. dfs_romfs_lseek,
  255. dfs_romfs_getdents,
  256. RT_NULL,
  257. dfs_romfs_stat,
  258. RT_NULL,
  259. };
  260. int dfs_romfs_init(void)
  261. {
  262. /* register rom file system */
  263. dfs_register(&_romfs);
  264. return 0;
  265. }
  266. INIT_FS_EXPORT(dfs_romfs_init);