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