dfs.c 12 KB

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