dfs.c 8.9 KB

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