dfs.c 10.0 KB

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