dfs.c 9.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443
  1. /*
  2. * File : dfs.c
  3. * This file is part of Device File System in RT-Thread RTOS
  4. * COPYRIGHT (C) 2004-2012, 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. * 2005-02-22 Bernard The first version.
  23. */
  24. #include <dfs.h>
  25. #include <dfs_fs.h>
  26. #include <dfs_file.h>
  27. #include "dfs_private.h"
  28. /* Global variables */
  29. const struct dfs_filesystem_ops *filesystem_operation_table[DFS_FILESYSTEM_TYPES_MAX];
  30. struct dfs_filesystem filesystem_table[DFS_FILESYSTEMS_MAX];
  31. /* device filesystem lock */
  32. static struct rt_mutex fslock;
  33. #ifdef DFS_USING_WORKDIR
  34. char working_directory[DFS_PATH_MAX] = {"/"};
  35. #endif
  36. struct dfs_fd fd_table[DFS_FD_MAX];
  37. /**
  38. * @addtogroup DFS
  39. */
  40. /*@{*/
  41. /**
  42. * this function will initialize device file system.
  43. */
  44. int dfs_init(void)
  45. {
  46. /* clear filesystem operations table */
  47. memset((void *)filesystem_operation_table, 0, sizeof(filesystem_operation_table));
  48. /* clear filesystem table */
  49. memset(filesystem_table, 0, sizeof(filesystem_table));
  50. /* clean fd table */
  51. memset(fd_table, 0, sizeof(fd_table));
  52. /* create device filesystem lock */
  53. rt_mutex_init(&fslock, "fslock", RT_IPC_FLAG_FIFO);
  54. #ifdef DFS_USING_WORKDIR
  55. /* set current working directory */
  56. memset(working_directory, 0, sizeof(working_directory));
  57. working_directory[0] = '/';
  58. #endif
  59. #ifdef RT_USING_DFS_DEVFS
  60. {
  61. extern int devfs_init(void);
  62. /* if enable devfs, initialize and mount it as soon as possible */
  63. devfs_init();
  64. dfs_mount(NULL, "/dev", "devfs", 0, 0);
  65. }
  66. #endif
  67. return 0;
  68. }
  69. INIT_PREV_EXPORT(dfs_init);
  70. /**
  71. * this function will lock device file system.
  72. *
  73. * @note please don't invoke it on ISR.
  74. */
  75. void dfs_lock(void)
  76. {
  77. rt_err_t result;
  78. result = rt_mutex_take(&fslock, RT_WAITING_FOREVER);
  79. if (result != RT_EOK)
  80. {
  81. RT_ASSERT(0);
  82. }
  83. }
  84. /**
  85. * this function will lock device file system.
  86. *
  87. * @note please don't invoke it on ISR.
  88. */
  89. void dfs_unlock(void)
  90. {
  91. rt_mutex_release(&fslock);
  92. }
  93. /**
  94. * @ingroup Fd
  95. * This function will allocate a file descriptor.
  96. *
  97. * @return -1 on failed or the allocated file descriptor.
  98. */
  99. int fd_new(void)
  100. {
  101. struct dfs_fd *d;
  102. int idx;
  103. /* lock filesystem */
  104. dfs_lock();
  105. /* find an empty fd entry */
  106. for (idx = 0; idx < DFS_FD_MAX && fd_table[idx].ref_count > 0; idx++);
  107. /* can't find an empty fd entry */
  108. if (idx == DFS_FD_MAX)
  109. {
  110. idx = -(1 + DFS_FD_OFFSET);
  111. goto __result;
  112. }
  113. d = &(fd_table[idx]);
  114. d->ref_count = 1;
  115. d->magic = DFS_FD_MAGIC;
  116. __result:
  117. dfs_unlock();
  118. return idx + DFS_FD_OFFSET;
  119. }
  120. /**
  121. * @ingroup Fd
  122. *
  123. * This function will return a file descriptor structure according to file
  124. * descriptor.
  125. *
  126. * @return NULL on on this file descriptor or the file descriptor structure
  127. * pointer.
  128. */
  129. struct dfs_fd *fd_get(int fd)
  130. {
  131. struct dfs_fd *d;
  132. fd = fd - DFS_FD_OFFSET;
  133. if (fd < 0 || fd >= DFS_FD_MAX)
  134. return NULL;
  135. dfs_lock();
  136. d = &fd_table[fd];
  137. /* check dfs_fd valid or not */
  138. if (d->magic != DFS_FD_MAGIC)
  139. {
  140. dfs_unlock();
  141. return NULL;
  142. }
  143. /* increase the reference count */
  144. d->ref_count ++;
  145. dfs_unlock();
  146. return d;
  147. }
  148. /**
  149. * @ingroup Fd
  150. *
  151. * This function will put the file descriptor.
  152. */
  153. void fd_put(struct dfs_fd *fd)
  154. {
  155. RT_ASSERT(fd != NULL);
  156. dfs_lock();
  157. fd->ref_count --;
  158. /* clear this fd entry */
  159. if (fd->ref_count == 0)
  160. {
  161. memset(fd, 0, sizeof(struct dfs_fd));
  162. }
  163. dfs_unlock();
  164. }
  165. /**
  166. * @ingroup Fd
  167. *
  168. * This function will return whether this file has been opend.
  169. *
  170. * @param pathname the file path name.
  171. *
  172. * @return 0 on file has been open successfully, -1 on open failed.
  173. */
  174. int fd_is_open(const char *pathname)
  175. {
  176. char *fullpath;
  177. unsigned int index;
  178. struct dfs_filesystem *fs;
  179. struct dfs_fd *fd;
  180. fullpath = dfs_normalize_path(NULL, pathname);
  181. if (fullpath != NULL)
  182. {
  183. char *mountpath;
  184. fs = dfs_filesystem_lookup(fullpath);
  185. if (fs == NULL)
  186. {
  187. /* can't find mounted file system */
  188. free(fullpath);
  189. return -1;
  190. }
  191. /* get file path name under mounted file system */
  192. if (fs->path[0] == '/' && fs->path[1] == '\0')
  193. mountpath = fullpath;
  194. else
  195. mountpath = fullpath + strlen(fs->path);
  196. dfs_lock();
  197. for (index = 0; index < DFS_FD_MAX; index++)
  198. {
  199. fd = &(fd_table[index]);
  200. if (fd->fops == NULL)
  201. continue;
  202. if (fd->fops == fs->ops->fops && strcmp(fd->path, mountpath) == 0)
  203. {
  204. /* found file in file descriptor table */
  205. rt_free(fullpath);
  206. dfs_unlock();
  207. return 0;
  208. }
  209. }
  210. dfs_unlock();
  211. rt_free(fullpath);
  212. }
  213. return -1;
  214. }
  215. /**
  216. * this function will return a sub-path name under directory.
  217. *
  218. * @param directory the parent directory.
  219. * @param filename the filename.
  220. *
  221. * @return the subdir pointer in filename
  222. */
  223. const char *dfs_subdir(const char *directory, const char *filename)
  224. {
  225. const char *dir;
  226. if (strlen(directory) == strlen(filename)) /* it's a same path */
  227. return NULL;
  228. dir = filename + strlen(directory);
  229. if ((*dir != '/') && (dir != filename))
  230. {
  231. dir --;
  232. }
  233. return dir;
  234. }
  235. RTM_EXPORT(dfs_subdir);
  236. /**
  237. * this function will normalize a path according to specified parent directory
  238. * and file name.
  239. *
  240. * @param directory the parent path
  241. * @param filename the file name
  242. *
  243. * @return the built full file path (absolute path)
  244. */
  245. char *dfs_normalize_path(const char *directory, const char *filename)
  246. {
  247. char *fullpath;
  248. char *dst0, *dst, *src;
  249. /* check parameters */
  250. RT_ASSERT(filename != NULL);
  251. #ifdef DFS_USING_WORKDIR
  252. if (directory == NULL) /* shall use working directory */
  253. directory = &working_directory[0];
  254. #else
  255. if ((directory == NULL) && (filename[0] != '/'))
  256. {
  257. rt_kprintf(NO_WORKING_DIR);
  258. return NULL;
  259. }
  260. #endif
  261. if (filename[0] != '/') /* it's a absolute path, use it directly */
  262. {
  263. fullpath = rt_malloc(strlen(directory) + strlen(filename) + 2);
  264. if (fullpath == NULL)
  265. return NULL;
  266. /* join path and file name */
  267. rt_snprintf(fullpath, strlen(directory) + strlen(filename) + 2,
  268. "%s/%s", directory, filename);
  269. }
  270. else
  271. {
  272. fullpath = rt_strdup(filename); /* copy string */
  273. if (fullpath == NULL)
  274. return NULL;
  275. }
  276. src = fullpath;
  277. dst = fullpath;
  278. dst0 = dst;
  279. while (1)
  280. {
  281. char c = *src;
  282. if (c == '.')
  283. {
  284. if (!src[1]) src ++; /* '.' and ends */
  285. else if (src[1] == '/')
  286. {
  287. /* './' case */
  288. src += 2;
  289. while ((*src == '/') && (*src != '\0'))
  290. src ++;
  291. continue;
  292. }
  293. else if (src[1] == '.')
  294. {
  295. if (!src[2])
  296. {
  297. /* '..' and ends case */
  298. src += 2;
  299. goto up_one;
  300. }
  301. else if (src[2] == '/')
  302. {
  303. /* '../' case */
  304. src += 3;
  305. while ((*src == '/') && (*src != '\0'))
  306. src ++;
  307. goto up_one;
  308. }
  309. }
  310. }
  311. /* copy up the next '/' and erase all '/' */
  312. while ((c = *src++) != '\0' && c != '/')
  313. *dst ++ = c;
  314. if (c == '/')
  315. {
  316. *dst ++ = '/';
  317. while (c == '/')
  318. c = *src++;
  319. src --;
  320. }
  321. else if (!c)
  322. break;
  323. continue;
  324. up_one:
  325. dst --;
  326. if (dst < dst0)
  327. {
  328. rt_free(fullpath);
  329. return NULL;
  330. }
  331. while (dst0 < dst && dst[-1] != '/')
  332. dst --;
  333. }
  334. *dst = '\0';
  335. /* remove '/' in the end of path if exist */
  336. dst --;
  337. if ((dst != fullpath) && (*dst == '/'))
  338. *dst = '\0';
  339. return fullpath;
  340. }
  341. RTM_EXPORT(dfs_normalize_path);
  342. #include <finsh.h>
  343. int list_fd(void)
  344. {
  345. int index;
  346. rt_enter_critical();
  347. for (index = 0; index < DFS_FD_MAX; index ++)
  348. {
  349. struct dfs_fd *fd = &(fd_table[index]);
  350. if (fd->fops)
  351. {
  352. rt_kprintf("--fd: %d--", index);
  353. if (fd->type == FT_DIRECTORY) rt_kprintf("[dir]\n");
  354. if (fd->type == FT_REGULAR) rt_kprintf("[file]\n");
  355. if (fd->type == FT_SOCKET) rt_kprintf("[socket]\n");
  356. if (fd->type == FT_USER) rt_kprintf("[user]\n");
  357. rt_kprintf("refcount=%d\n", fd->ref_count);
  358. rt_kprintf("magic=0x%04x\n", fd->magic);
  359. if (fd->path)
  360. {
  361. rt_kprintf("path: %s\n", fd->path);
  362. }
  363. }
  364. }
  365. rt_exit_critical();
  366. return 0;
  367. }
  368. MSH_CMD_EXPORT(list_fd, list file descriptor);
  369. /*@}*/