1
0

dfs_romfs.c 6.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292
  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. * The license and distribution terms for this file may be
  7. * found in the file LICENSE in this distribution or at
  8. * http://www.rt-thread.org/license/LICENSE.
  9. *
  10. * Change Logs:
  11. * Date Author Notes
  12. */
  13. #include <rtthread.h>
  14. #include <dfs.h>
  15. #include <dfs_fs.h>
  16. #include "dfs_romfs.h"
  17. int dfs_romfs_mount(struct dfs_filesystem *fs, unsigned long rwflag, const void *data)
  18. {
  19. struct romfs_dirent *root_dirent;
  20. if (data == RT_NULL)
  21. return -DFS_STATUS_EIO;
  22. root_dirent = (struct romfs_dirent *)data;
  23. fs->data = root_dirent;
  24. return DFS_STATUS_OK;
  25. }
  26. int dfs_romfs_unmount(struct dfs_filesystem *fs)
  27. {
  28. return DFS_STATUS_OK;
  29. }
  30. int dfs_romfs_ioctl(struct dfs_fd *file, int cmd, void *args)
  31. {
  32. return -DFS_STATUS_EIO;
  33. }
  34. struct romfs_dirent *dfs_romfs_lookup(struct romfs_dirent *root_dirent, const char *path, rt_size_t *size)
  35. {
  36. rt_size_t index, found;
  37. const char *subpath, *subpath_end;
  38. struct romfs_dirent *dirent;
  39. rt_size_t dirent_size;
  40. if (path[0] == '/' && path[1] == '\0')
  41. {
  42. *size = root_dirent->size;
  43. return root_dirent;
  44. }
  45. /* goto root directy entries */
  46. dirent = (struct romfs_dirent *)root_dirent->data;
  47. dirent_size = root_dirent->size;
  48. /* get the end position of this subpath */
  49. subpath_end = path;
  50. /* skip /// */
  51. while (*subpath_end && *subpath_end == '/')
  52. subpath_end ++;
  53. subpath = subpath_end;
  54. while ((*subpath_end != '/') && *subpath_end)
  55. subpath_end ++;
  56. while (dirent != RT_NULL)
  57. {
  58. found = 0;
  59. /* search in folder */
  60. for (index = 0; index < dirent_size; index ++)
  61. {
  62. if (rt_strncmp(dirent[index].name, subpath, (subpath_end - subpath)) == 0)
  63. {
  64. dirent_size = dirent[index].size;
  65. /* skip /// */
  66. while (*subpath_end && *subpath_end == '/')
  67. subpath_end ++;
  68. subpath = subpath_end;
  69. while ((*subpath_end != '/') && *subpath_end)
  70. subpath_end ++;
  71. if (!(*subpath))
  72. {
  73. *size = dirent_size;
  74. return &dirent[index];
  75. }
  76. if (dirent[index].type == ROMFS_DIRENT_DIR)
  77. {
  78. /* enter directory */
  79. dirent = (struct romfs_dirent*)dirent[index].data;
  80. found = 1;
  81. break;
  82. }
  83. else
  84. {
  85. /* return file dirent */
  86. if (subpath != RT_NULL)
  87. break; /* not the end of path */
  88. return &dirent[index];
  89. }
  90. }
  91. }
  92. if (!found)
  93. break; /* not found */
  94. }
  95. /* not found */
  96. return RT_NULL;
  97. }
  98. int dfs_romfs_read(struct dfs_fd *file, void *buf, rt_size_t count)
  99. {
  100. rt_size_t length;
  101. struct romfs_dirent *dirent;
  102. dirent = (struct romfs_dirent *)file->data;
  103. RT_ASSERT(dirent != RT_NULL);
  104. if (count < file->size - file->pos)
  105. length = count;
  106. else
  107. length = file->size - file->pos;
  108. if (length > 0)
  109. memcpy(buf, &(dirent->data[file->pos]), length);
  110. /* update file current position */
  111. file->pos += length;
  112. return length;
  113. }
  114. int dfs_romfs_lseek(struct dfs_fd *file, rt_off_t offset)
  115. {
  116. if (offset <= file->size)
  117. {
  118. file->pos = offset;
  119. return file->pos;
  120. }
  121. return -DFS_STATUS_EIO;
  122. }
  123. int dfs_romfs_close(struct dfs_fd *file)
  124. {
  125. file->data = RT_NULL;
  126. return DFS_STATUS_OK;
  127. }
  128. int dfs_romfs_open(struct dfs_fd *file)
  129. {
  130. rt_size_t size;
  131. struct romfs_dirent *dirent;
  132. struct romfs_dirent *root_dirent;
  133. root_dirent = (struct romfs_dirent *)file->fs->data;
  134. if (file->flags & (DFS_O_CREAT | DFS_O_WRONLY | DFS_O_APPEND | DFS_O_TRUNC | DFS_O_RDWR))
  135. return -DFS_STATUS_EINVAL;
  136. dirent = dfs_romfs_lookup(root_dirent, file->path, &size);
  137. if (dirent == RT_NULL)
  138. return -DFS_STATUS_ENOENT;
  139. /* entry is a directory file type */
  140. if (dirent->type == ROMFS_DIRENT_DIR)
  141. {
  142. if (!(file->flags & DFS_O_DIRECTORY))
  143. return -DFS_STATUS_ENOENT;
  144. }
  145. else
  146. {
  147. /* entry is a file, but open it as a directory */
  148. if (file->flags & DFS_O_DIRECTORY)
  149. return -DFS_STATUS_ENOENT;
  150. }
  151. file->data = dirent;
  152. file->size = size;
  153. file->pos = 0;
  154. return DFS_STATUS_OK;
  155. }
  156. int dfs_romfs_stat(struct dfs_filesystem *fs, const char *path, struct stat *st)
  157. {
  158. rt_size_t size;
  159. struct romfs_dirent *dirent;
  160. struct romfs_dirent *root_dirent;
  161. root_dirent = (struct romfs_dirent *)fs->data;
  162. dirent = dfs_romfs_lookup(root_dirent, path, &size);
  163. if (dirent == RT_NULL)
  164. return -DFS_STATUS_ENOENT;
  165. st->st_dev = 0;
  166. st->st_mode = DFS_S_IFREG | DFS_S_IRUSR | DFS_S_IRGRP | DFS_S_IROTH |
  167. DFS_S_IWUSR | DFS_S_IWGRP | DFS_S_IWOTH;
  168. if (dirent->type == ROMFS_DIRENT_DIR)
  169. {
  170. st->st_mode &= ~DFS_S_IFREG;
  171. st->st_mode |= DFS_S_IFDIR | DFS_S_IXUSR | DFS_S_IXGRP | DFS_S_IXOTH;
  172. }
  173. st->st_size = dirent->size;
  174. st->st_mtime = 0;
  175. st->st_blksize = 512;
  176. return DFS_STATUS_OK;
  177. }
  178. int dfs_romfs_getdents(struct dfs_fd *file, struct dirent *dirp, rt_uint32_t count)
  179. {
  180. rt_size_t index;
  181. const char *name;
  182. struct dirent *d;
  183. struct romfs_dirent *dirent, *sub_dirent;
  184. dirent = (struct romfs_dirent *)file->data;
  185. RT_ASSERT(dirent->type == ROMFS_DIRENT_DIR);
  186. /* enter directory */
  187. dirent = (struct romfs_dirent *)dirent->data;
  188. /* make integer count */
  189. count = (count / sizeof(struct dirent));
  190. if (count == 0)
  191. return -DFS_STATUS_EINVAL;
  192. index = 0;
  193. for (index = 0; index < count && file->pos < file->size; index ++)
  194. {
  195. d = dirp + index;
  196. sub_dirent = &dirent[file->pos];
  197. name = sub_dirent->name;
  198. /* fill dirent */
  199. if (sub_dirent->type == ROMFS_DIRENT_DIR)
  200. d->d_type = DFS_DT_DIR;
  201. else
  202. d->d_type = DFS_DT_REG;
  203. d->d_namlen = rt_strlen(name);
  204. d->d_reclen = (rt_uint16_t)sizeof(struct dirent);
  205. rt_strncpy(d->d_name, name, rt_strlen(name) + 1);
  206. /* move to next position */
  207. ++ file->pos;
  208. }
  209. return index * sizeof(struct dirent);
  210. }
  211. static const struct dfs_filesystem_operation _romfs =
  212. {
  213. "rom",
  214. DFS_FS_FLAG_DEFAULT,
  215. dfs_romfs_mount,
  216. dfs_romfs_unmount,
  217. RT_NULL,
  218. RT_NULL,
  219. dfs_romfs_open,
  220. dfs_romfs_close,
  221. dfs_romfs_ioctl,
  222. dfs_romfs_read,
  223. RT_NULL,
  224. RT_NULL,
  225. dfs_romfs_lseek,
  226. dfs_romfs_getdents,
  227. RT_NULL,
  228. dfs_romfs_stat,
  229. RT_NULL,
  230. };
  231. int dfs_romfs_init(void)
  232. {
  233. /* register rom file system */
  234. dfs_register(&_romfs);
  235. return 0;
  236. }