dfs.c 8.4 KB

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