dfs.c 7.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378
  1. /*
  2. * File : dfs.c
  3. * This file is part of Device File System in RT-Thread RTOS
  4. * COPYRIGHT (C) 2004-2011, 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(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. __result:
  108. dfs_unlock();
  109. return idx;
  110. }
  111. /**
  112. * @ingroup Fd
  113. *
  114. * This function will return a file descriptor structure according to file
  115. * descriptor.
  116. *
  117. * @return NULL on on this file descriptor or the file descriptor structure
  118. * pointer.
  119. */
  120. struct dfs_fd *fd_get(int fd)
  121. {
  122. struct dfs_fd *d;
  123. #ifdef DFS_USING_STDIO
  124. if (fd < 3 || fd > DFS_FD_MAX + 3)
  125. return RT_NULL;
  126. #else
  127. if (fd < 0 || fd > DFS_FD_MAX)
  128. return RT_NULL;
  129. #endif
  130. dfs_lock();
  131. d = &fd_table[fd];
  132. /* increase the reference count */
  133. d->ref_count ++;
  134. dfs_unlock();
  135. return d;
  136. }
  137. /**
  138. * @ingroup Fd
  139. *
  140. * This function will put the file descriptor.
  141. */
  142. void fd_put(struct dfs_fd *fd)
  143. {
  144. dfs_lock();
  145. fd->ref_count --;
  146. /* clear this fd entry */
  147. if (fd->ref_count == 0)
  148. {
  149. rt_memset(fd, 0, sizeof(struct dfs_fd));
  150. }
  151. dfs_unlock();
  152. };
  153. /**
  154. * @ingroup Fd
  155. *
  156. * This function will return whether this file has been opend.
  157. *
  158. * @param pathname the file path name.
  159. *
  160. * @return 0 on file has been open successfully, -1 on open failed.
  161. */
  162. int fd_is_open(const char *pathname)
  163. {
  164. char *fullpath;
  165. unsigned int index;
  166. struct dfs_filesystem *fs;
  167. struct dfs_fd *fd;
  168. fullpath = dfs_normalize_path(RT_NULL, pathname);
  169. if (fullpath != RT_NULL)
  170. {
  171. char *mountpath;
  172. fs = dfs_filesystem_lookup(fullpath);
  173. if (fs == RT_NULL)
  174. {
  175. /* can't find mounted file system */
  176. rt_free(fullpath);
  177. return -1;
  178. }
  179. /* get file path name under mounted file system */
  180. if (fs->path[0] == '/' && fs->path[1] == '\0')
  181. mountpath = fullpath;
  182. else
  183. mountpath = fullpath + strlen(fs->path);
  184. dfs_lock();
  185. for (index = 0; index < DFS_FD_MAX; index++)
  186. {
  187. fd = &(fd_table[index]);
  188. if (fd->fs == RT_NULL)
  189. continue;
  190. if (fd->fs == fs && strcmp(fd->path, mountpath) == 0)
  191. {
  192. /* found file in file descriptor table */
  193. rt_free(fullpath);
  194. dfs_unlock();
  195. return 0;
  196. }
  197. }
  198. dfs_unlock();
  199. rt_free(fullpath);
  200. }
  201. return -1;
  202. }
  203. /**
  204. * this function will return a sub-path name under directory.
  205. *
  206. * @param directory the parent directory.
  207. * @param filename the filename.
  208. *
  209. * @return the subdir pointer in filename
  210. */
  211. const char *dfs_subdir(const char *directory, const char *filename)
  212. {
  213. const char *dir;
  214. if (strlen(directory) == strlen(filename)) /* it's a same path */
  215. return RT_NULL;
  216. dir = filename + strlen(directory);
  217. if ((*dir != '/') && (dir != filename))
  218. {
  219. dir --;
  220. }
  221. return dir;
  222. }
  223. /**
  224. * this function will normalize a path according to specified parent directory and file name.
  225. *
  226. * @param directory the parent path
  227. * @param filename the file name
  228. *
  229. * @return the built full file path (absolute path)
  230. */
  231. char *dfs_normalize_path(const char *directory, const char *filename)
  232. {
  233. char *fullpath;
  234. char *dst0, *dst, *src;
  235. /* check parameters */
  236. RT_ASSERT(filename != RT_NULL);
  237. #ifdef DFS_USING_WORKDIR
  238. if (directory == NULL) /* shall use working directory */
  239. directory = &working_directory[0];
  240. #else
  241. if ((directory == NULL) && (filename[0] != '/'))
  242. {
  243. rt_kprintf(NO_WORKING_DIR);
  244. return RT_NULL;
  245. }
  246. #endif
  247. if (filename[0] != '/') /* it's a absolute path, use it directly */
  248. {
  249. fullpath = rt_malloc(strlen(directory) + strlen(filename) + 2);
  250. /* join path and file name */
  251. rt_snprintf(fullpath, strlen(directory) + strlen(filename) + 2,
  252. "%s/%s", directory, filename);
  253. }
  254. else
  255. {
  256. fullpath = rt_strdup(filename); /* copy string */
  257. }
  258. src = fullpath;
  259. dst = fullpath;
  260. dst0 = dst;
  261. while (1)
  262. {
  263. char c = *src;
  264. if (c == '.')
  265. {
  266. if (!src[1]) src ++; /* '.' and ends */
  267. else if (src[1] == '/')
  268. {
  269. /* './' case */
  270. src += 2;
  271. while ((*src == '/') && (*src != '\0'))
  272. src ++;
  273. continue;
  274. }
  275. else if (src[1] == '.')
  276. {
  277. if (!src[2])
  278. {
  279. /* '..' and ends case */
  280. src += 2;
  281. goto up_one;
  282. }
  283. else if (src[2] == '/')
  284. {
  285. /* '../' case */
  286. src += 3;
  287. while ((*src == '/') && (*src != '\0'))
  288. src ++;
  289. goto up_one;
  290. }
  291. }
  292. }
  293. /* copy up the next '/' and erase all '/' */
  294. while ((c = *src++) != '\0' && c != '/')
  295. *dst ++ = c;
  296. if (c == '/')
  297. {
  298. *dst ++ = '/';
  299. while (c == '/')
  300. c = *src++;
  301. src --;
  302. }
  303. else if (!c)
  304. break;
  305. continue;
  306. up_one:
  307. dst --;
  308. if (dst < dst0)
  309. {
  310. rt_free(fullpath);
  311. return NULL;
  312. }
  313. while (dst0 < dst && dst[-1] != '/')
  314. dst --;
  315. }
  316. *dst = '\0';
  317. /* remove '/' in the end of path if exist */
  318. dst --;
  319. if ((dst != fullpath) && (*dst == '/'))
  320. *dst = '\0';
  321. return fullpath;
  322. }
  323. /*@}*/