dfs_romfs.c 8.0 KB

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