dfs.c 7.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363
  1. /*
  2. * File : dfs.c
  3. * This file is part of Device File System in RT-Thread RTOS
  4. * COPYRIGHT (C) 2004-2010, 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. * 2010-07-16
  14. */
  15. #include <dfs.h>
  16. #include <dfs_fs.h>
  17. #include <dfs_file.h>
  18. #define NO_WORKING_DIR "system does not support working dir\n"
  19. /* Global variables */
  20. const struct dfs_filesystem_operation* filesystem_operation_table[DFS_FILESYSTEM_TYPES_MAX];
  21. struct dfs_filesystem filesystem_table[DFS_FILESYSTEMS_MAX];
  22. /* device filesystem lock */
  23. static struct rt_mutex fslock;
  24. #ifdef DFS_USING_WORKDIR
  25. char working_directory[DFS_PATH_MAX] = {"/"};
  26. #endif
  27. #ifdef DFS_USING_STDIO
  28. struct dfs_fd fd_table[3 + DFS_FD_MAX];
  29. #else
  30. struct dfs_fd fd_table[DFS_FD_MAX];
  31. #endif
  32. /**
  33. * @addtogroup DFS
  34. */
  35. /*@{*/
  36. /**
  37. * this function will initialize device file system.
  38. */
  39. void dfs_init()
  40. {
  41. /* clear filesystem operations table */
  42. rt_memset(filesystem_operation_table, 0, sizeof(filesystem_operation_table));
  43. /* clear filesystem table */
  44. rt_memset(filesystem_table, 0, sizeof(filesystem_table));
  45. /* clean fd table */
  46. rt_memset(fd_table, 0, sizeof(fd_table));
  47. /* create device filesystem lock */
  48. rt_mutex_init(&fslock, "fslock", RT_IPC_FLAG_FIFO);
  49. #ifdef DFS_USING_WORKDIR
  50. /* set current working directory */
  51. rt_memset(working_directory, 0, sizeof(working_directory));
  52. working_directory[0] = '/';
  53. #endif
  54. }
  55. /**
  56. * this function will lock device file system.
  57. *
  58. * @note please don't invoke it on ISR.
  59. */
  60. void dfs_lock()
  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()
  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. __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) return RT_NULL;
  126. #else
  127. if ( fd < 0 || fd > DFS_FD_MAX ) return RT_NULL;
  128. #endif
  129. dfs_lock();
  130. d = &fd_table[fd];
  131. /* increase the reference count */
  132. d->ref_count ++;
  133. dfs_unlock();
  134. return d;
  135. }
  136. /**
  137. * @ingroup Fd
  138. *
  139. * This function will put the file descriptor.
  140. */
  141. void fd_put(struct dfs_fd* fd)
  142. {
  143. dfs_lock();
  144. fd->ref_count --;
  145. /* clear this fd entry */
  146. if ( fd->ref_count == 0 )
  147. {
  148. rt_memset(fd, 0, sizeof(struct dfs_fd));
  149. }
  150. dfs_unlock();
  151. };
  152. /**
  153. * @ingroup Fd
  154. *
  155. * This function will return whether this file has been opend.
  156. *
  157. * @param pathname the file path name.
  158. *
  159. * @return 0 on file has been open successfully, -1 on open failed.
  160. */
  161. int fd_is_open(const char* pathname)
  162. {
  163. char *fullpath;
  164. unsigned int index;
  165. struct dfs_filesystem* fs;
  166. struct dfs_fd* fd;
  167. fullpath = dfs_normalize_path(RT_NULL, pathname);
  168. if (fullpath != RT_NULL)
  169. {
  170. char *mountpath;
  171. fs = dfs_filesystem_lookup(fullpath);
  172. if (fs == RT_NULL)
  173. {
  174. /* can't find mounted file system */
  175. rt_free(fullpath);
  176. return -1;
  177. }
  178. /* get file path name under mounted file system */
  179. if (fs->path[0] == '/' && fs->path[1] == '\0')
  180. mountpath = fullpath;
  181. else mountpath = fullpath + strlen(fs->path);
  182. dfs_lock();
  183. for (index = 0; index < DFS_FD_MAX; index++)
  184. {
  185. fd = &(fd_table[index]);
  186. if (fd->fs == RT_NULL) continue;
  187. if (fd->fs == fs &&
  188. strcmp(fd->path, mountpath) == 0)
  189. {
  190. /* found file in file descriptor table */
  191. rt_free(fullpath);
  192. dfs_unlock();
  193. return 0;
  194. }
  195. }
  196. dfs_unlock();
  197. rt_free(fullpath);
  198. }
  199. return -1;
  200. }
  201. /**
  202. * this function will return a sub-path name under directory.
  203. *
  204. * @param directory the parent directory.
  205. * @param filename the filename.
  206. *
  207. * @return the subdir pointer in filename
  208. */
  209. const char* dfs_subdir(const char* directory, const char* filename)
  210. {
  211. const char* dir;
  212. if (strlen(directory) == strlen(filename)) /* it's a same path */
  213. return RT_NULL;
  214. dir = filename + strlen(directory);
  215. if ((*dir != '/') && (dir != filename))
  216. {
  217. dir --;
  218. }
  219. return dir;
  220. }
  221. /**
  222. * this function will normalize a path according to specified parent directory and file name.
  223. *
  224. * @param directory the parent path
  225. * @param filename the file name
  226. *
  227. * @return the built full file path (absoluted path)
  228. */
  229. char* dfs_normalize_path(const char* directory, const char* filename)
  230. {
  231. char *fullpath;
  232. char *dst0, *dst, *src;
  233. /* check parameters */
  234. RT_ASSERT(filename != RT_NULL);
  235. #ifdef DFS_USING_WORKDIR
  236. if (directory == NULL) /* shall use working directory */
  237. directory = &working_directory[0];
  238. #else
  239. if ((directory == NULL) && (filename[0] != '/'))
  240. {
  241. rt_kprintf(NO_WORKING_DIR);
  242. return RT_NULL;
  243. }
  244. #endif
  245. if (filename[0] != '/') /* it's a absolute path, use it directly */
  246. {
  247. fullpath = rt_malloc(strlen(directory) + strlen(filename) + 2);
  248. /* join path and file name */
  249. rt_snprintf(fullpath, strlen(directory) + strlen(filename) + 2,
  250. "%s/%s", directory, filename);
  251. }
  252. else
  253. {
  254. fullpath = rt_strdup(filename); /* copy string */
  255. }
  256. src = fullpath;
  257. dst = fullpath;
  258. while (1)
  259. {
  260. char c = *src;
  261. if (c == '.')
  262. {
  263. if (!src[1]) src ++; /* '.' and ends */
  264. else if (src[1] == '/')
  265. {
  266. /* './' case */
  267. src += 2;
  268. while ((*src == '/') && (*src != '\0')) src ++;
  269. continue;
  270. }
  271. else if (src[1] == '.')
  272. {
  273. if (!src[2])
  274. {
  275. /* '..' and ends case */
  276. src += 2;
  277. goto up_one;
  278. }
  279. else if (src[2] == '/')
  280. {
  281. /* '../' case */
  282. src += 3;
  283. while ((*src == '/') && (*src != '\0')) src ++;
  284. goto up_one;
  285. }
  286. }
  287. }
  288. /* copy up the next '/' and erase all '/' */
  289. while ((c = *src++) != '\0' && c != '/') *dst ++ = c;
  290. if (c == '/')
  291. {
  292. *dst ++ = '/';
  293. while (c == '/') c = *src++;
  294. src --;
  295. }
  296. else if (!c) break;
  297. continue;
  298. up_one:
  299. dst --;
  300. if (dst < dst0) { rt_free(fullpath); return NULL;}
  301. while (dst0 < dst && dst[-1] != '/') dst --;
  302. }
  303. *dst = '\0';
  304. /* remove '/' in the end of path if exist */
  305. dst --;
  306. if ((dst != fullpath) && (*dst == '/')) *dst = '\0';
  307. return fullpath;
  308. }
  309. /*@}*/