dfs.c 13 KB

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