dfs_romfs.c 6.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303
  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. struct romfs_dirent *dfs_romfs_lookup(struct romfs_dirent *root_dirent, const char *path, rt_size_t *size)
  45. {
  46. rt_size_t index, found;
  47. const char *subpath, *subpath_end;
  48. struct romfs_dirent *dirent;
  49. rt_size_t dirent_size;
  50. if (path[0] == '/' && path[1] == '\0')
  51. {
  52. *size = root_dirent->size;
  53. return root_dirent;
  54. }
  55. /* goto root directy entries */
  56. dirent = (struct romfs_dirent *)root_dirent->data;
  57. dirent_size = root_dirent->size;
  58. /* get the end position of this subpath */
  59. subpath_end = path;
  60. /* skip /// */
  61. while (*subpath_end && *subpath_end == '/')
  62. subpath_end ++;
  63. subpath = subpath_end;
  64. while ((*subpath_end != '/') && *subpath_end)
  65. subpath_end ++;
  66. while (dirent != RT_NULL)
  67. {
  68. found = 0;
  69. /* search in folder */
  70. for (index = 0; index < dirent_size; index ++)
  71. {
  72. if (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 != RT_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 RT_NULL;
  107. }
  108. int dfs_romfs_read(struct dfs_fd *file, void *buf, rt_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 != RT_NULL);
  114. if (count < file->size - file->pos)
  115. length = count;
  116. else
  117. length = file->size - file->pos;
  118. if (length > 0)
  119. memcpy(buf, &(dirent->data[file->pos]), length);
  120. /* update file current position */
  121. file->pos += length;
  122. return length;
  123. }
  124. int dfs_romfs_lseek(struct dfs_fd *file, rt_off_t offset)
  125. {
  126. if (offset <= file->size)
  127. {
  128. file->pos = offset;
  129. return file->pos;
  130. }
  131. return -DFS_STATUS_EIO;
  132. }
  133. int dfs_romfs_close(struct dfs_fd *file)
  134. {
  135. file->data = RT_NULL;
  136. return DFS_STATUS_OK;
  137. }
  138. int dfs_romfs_open(struct dfs_fd *file)
  139. {
  140. rt_size_t size;
  141. struct romfs_dirent *dirent;
  142. struct romfs_dirent *root_dirent;
  143. root_dirent = (struct romfs_dirent *)file->fs->data;
  144. if (file->flags & (DFS_O_CREAT | DFS_O_WRONLY | DFS_O_APPEND | DFS_O_TRUNC | DFS_O_RDWR))
  145. return -DFS_STATUS_EINVAL;
  146. dirent = dfs_romfs_lookup(root_dirent, file->path, &size);
  147. if (dirent == RT_NULL)
  148. return -DFS_STATUS_ENOENT;
  149. /* entry is a directory file type */
  150. if (dirent->type == ROMFS_DIRENT_DIR)
  151. {
  152. if (!(file->flags & DFS_O_DIRECTORY))
  153. return -DFS_STATUS_ENOENT;
  154. }
  155. else
  156. {
  157. /* entry is a file, but open it as a directory */
  158. if (file->flags & DFS_O_DIRECTORY)
  159. return -DFS_STATUS_ENOENT;
  160. }
  161. file->data = dirent;
  162. file->size = size;
  163. file->pos = 0;
  164. return DFS_STATUS_OK;
  165. }
  166. int dfs_romfs_stat(struct dfs_filesystem *fs, const char *path, struct stat *st)
  167. {
  168. rt_size_t size;
  169. struct romfs_dirent *dirent;
  170. struct romfs_dirent *root_dirent;
  171. root_dirent = (struct romfs_dirent *)fs->data;
  172. dirent = dfs_romfs_lookup(root_dirent, path, &size);
  173. if (dirent == RT_NULL)
  174. return -DFS_STATUS_ENOENT;
  175. st->st_dev = 0;
  176. st->st_mode = DFS_S_IFREG | DFS_S_IRUSR | DFS_S_IRGRP | DFS_S_IROTH |
  177. DFS_S_IWUSR | DFS_S_IWGRP | DFS_S_IWOTH;
  178. if (dirent->type == ROMFS_DIRENT_DIR)
  179. {
  180. st->st_mode &= ~DFS_S_IFREG;
  181. st->st_mode |= DFS_S_IFDIR | DFS_S_IXUSR | DFS_S_IXGRP | DFS_S_IXOTH;
  182. }
  183. st->st_size = dirent->size;
  184. st->st_mtime = 0;
  185. st->st_blksize = 512;
  186. return DFS_STATUS_OK;
  187. }
  188. int dfs_romfs_getdents(struct dfs_fd *file, struct dirent *dirp, rt_uint32_t count)
  189. {
  190. rt_size_t index;
  191. const char *name;
  192. struct dirent *d;
  193. struct romfs_dirent *dirent, *sub_dirent;
  194. dirent = (struct romfs_dirent *)file->data;
  195. RT_ASSERT(dirent->type == ROMFS_DIRENT_DIR);
  196. /* enter directory */
  197. dirent = (struct romfs_dirent *)dirent->data;
  198. /* make integer count */
  199. count = (count / sizeof(struct dirent));
  200. if (count == 0)
  201. return -DFS_STATUS_EINVAL;
  202. index = 0;
  203. for (index = 0; index < count && file->pos < file->size; index ++)
  204. {
  205. d = dirp + index;
  206. sub_dirent = &dirent[file->pos];
  207. name = sub_dirent->name;
  208. /* fill dirent */
  209. if (sub_dirent->type == ROMFS_DIRENT_DIR)
  210. d->d_type = DFS_DT_DIR;
  211. else
  212. d->d_type = DFS_DT_REG;
  213. d->d_namlen = rt_strlen(name);
  214. d->d_reclen = (rt_uint16_t)sizeof(struct dirent);
  215. rt_strncpy(d->d_name, name, rt_strlen(name) + 1);
  216. /* move to next position */
  217. ++ file->pos;
  218. }
  219. return index * sizeof(struct dirent);
  220. }
  221. static const struct dfs_filesystem_operation _romfs =
  222. {
  223. "rom",
  224. DFS_FS_FLAG_DEFAULT,
  225. dfs_romfs_mount,
  226. dfs_romfs_unmount,
  227. RT_NULL,
  228. RT_NULL,
  229. dfs_romfs_open,
  230. dfs_romfs_close,
  231. dfs_romfs_ioctl,
  232. dfs_romfs_read,
  233. RT_NULL,
  234. RT_NULL,
  235. dfs_romfs_lseek,
  236. dfs_romfs_getdents,
  237. RT_NULL,
  238. dfs_romfs_stat,
  239. RT_NULL,
  240. };
  241. int dfs_romfs_init(void)
  242. {
  243. /* register rom file system */
  244. dfs_register(&_romfs);
  245. return 0;
  246. }
  247. INIT_FS_EXPORT(dfs_romfs_init);