dfs.c 12 KB

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