dfs.c 7.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364
  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_config.h>
  18. #include <dfs_file.h>
  19. #define NO_WORKING_DIR "system does not support working dir\n"
  20. /* Global variables */
  21. const struct dfs_filesystem_operation* filesystem_operation_table[DFS_FILESYSTEM_TYPES_MAX];
  22. struct dfs_filesystem filesystem_table[DFS_FILESYSTEMS_MAX];
  23. /* device filesystem lock */
  24. static struct rt_mutex fslock;
  25. #ifdef DFS_USING_WORKDIR
  26. char working_directory[DFS_PATH_MAX] = {"/"};
  27. #endif
  28. #ifdef DFS_USING_STDIO
  29. struct dfs_fd fd_table[3 + DFS_FD_MAX];
  30. #else
  31. struct dfs_fd fd_table[DFS_FD_MAX];
  32. #endif
  33. /**
  34. * @addtogroup DFS
  35. */
  36. /*@{*/
  37. /**
  38. * this function will initialize device file system.
  39. */
  40. void dfs_init()
  41. {
  42. /* clear filesystem operations table */
  43. rt_memset(filesystem_operation_table, 0, sizeof(filesystem_operation_table));
  44. /* clear filesystem table */
  45. rt_memset(filesystem_table, 0, sizeof(filesystem_table));
  46. /* clean fd table */
  47. rt_memset(fd_table, 0, sizeof(fd_table));
  48. /* create device filesystem lock */
  49. rt_mutex_init(&fslock, "fslock", RT_IPC_FLAG_FIFO);
  50. #ifdef DFS_USING_WORKDIR
  51. /* set current working directory */
  52. rt_memset(working_directory, 0, sizeof(working_directory));
  53. working_directory[0] = '/';
  54. #endif
  55. }
  56. /**
  57. * this function will lock device file system.
  58. *
  59. * @note please don't invoke it on ISR.
  60. */
  61. void dfs_lock()
  62. {
  63. rt_err_t result;
  64. result = rt_mutex_take(&fslock, RT_WAITING_FOREVER);
  65. if (result != RT_EOK)
  66. {
  67. RT_ASSERT(0);
  68. }
  69. }
  70. /**
  71. * this function will lock device file system.
  72. *
  73. * @note please don't invoke it on ISR.
  74. */
  75. void dfs_unlock()
  76. {
  77. rt_mutex_release(&fslock);
  78. }
  79. /**
  80. * @ingroup Fd
  81. * This function will allocate a file descriptor.
  82. *
  83. * @return -1 on failed or the allocated file descriptor.
  84. */
  85. int fd_new(void)
  86. {
  87. struct dfs_fd* d;
  88. int idx;
  89. /* lock filesystem */
  90. dfs_lock();
  91. /* find an empty fd entry */
  92. #ifdef DFS_USING_STDIO
  93. for (idx = 3; idx < DFS_FD_MAX + 3 && fd_table[idx].ref_count > 0; idx++);
  94. #else
  95. for (idx = 0; idx < DFS_FD_MAX && fd_table[idx].ref_count > 0; idx++);
  96. #endif
  97. /* can't find an empty fd entry */
  98. #ifdef DFS_USING_STDIO
  99. if (idx == DFS_FD_MAX + 3)
  100. #else
  101. if (idx == DFS_FD_MAX)
  102. #endif
  103. {
  104. idx = -1;
  105. goto __result;
  106. }
  107. d = &(fd_table[idx]);
  108. d->ref_count = 1;
  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) return RT_NULL;
  127. #else
  128. if ( fd < 0 || fd > DFS_FD_MAX ) 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 mountpath = fullpath + strlen(fs->path);
  183. dfs_lock();
  184. for (index = 0; index < DFS_FD_MAX; index++)
  185. {
  186. fd = &(fd_table[index]);
  187. if (fd->fs == RT_NULL) continue;
  188. if (fd->fs == fs &&
  189. strcmp(fd->path, mountpath) == 0)
  190. {
  191. /* found file in file descriptor table */
  192. rt_free(fullpath);
  193. dfs_unlock();
  194. return 0;
  195. }
  196. }
  197. dfs_unlock();
  198. rt_free(fullpath);
  199. }
  200. return -1;
  201. }
  202. /**
  203. * this function will return a sub-path name under directory.
  204. *
  205. * @param directory the parent directory.
  206. * @param filename the filename.
  207. *
  208. * @return the subdir pointer in filename
  209. */
  210. const char* dfs_subdir(const char* directory, const char* filename)
  211. {
  212. const char* dir;
  213. if (strlen(directory) == strlen(filename)) /* it's a same path */
  214. return RT_NULL;
  215. dir = filename + strlen(directory);
  216. if ((*dir != '/') && (dir != filename))
  217. {
  218. dir --;
  219. }
  220. return dir;
  221. }
  222. /**
  223. * this function will normalize a path according to specified parent directory and file name.
  224. *
  225. * @param directory the parent path
  226. * @param filename the file name
  227. *
  228. * @return the built full file path (absoluted path)
  229. */
  230. char* dfs_normalize_path(const char* directory, const char* filename)
  231. {
  232. char *fullpath;
  233. char *dst0, *dst, *src;
  234. /* check parameters */
  235. RT_ASSERT(filename != RT_NULL);
  236. #ifdef DFS_USING_WORKDIR
  237. if (directory == NULL) /* shall use working directory */
  238. directory = &working_directory[0];
  239. #else
  240. if ((directory == NULL) && (filename[0] != '/'))
  241. {
  242. rt_kprintf(NO_WORKING_DIR);
  243. return RT_NULL;
  244. }
  245. #endif
  246. if (filename[0] != '/') /* it's a absolute path, use it directly */
  247. {
  248. fullpath = rt_malloc(strlen(directory) + strlen(filename) + 2);
  249. /* join path and file name */
  250. rt_snprintf(fullpath, strlen(directory) + strlen(filename) + 2,
  251. "%s/%s", directory, filename);
  252. }
  253. else
  254. {
  255. fullpath = rt_strdup(filename); /* copy string */
  256. }
  257. src = fullpath;
  258. dst = fullpath;
  259. while (1)
  260. {
  261. char c = *src;
  262. if (c == '.')
  263. {
  264. if (!src[1]) src ++; /* '.' and ends */
  265. else if (src[1] == '/')
  266. {
  267. /* './' case */
  268. src += 2;
  269. while ((*src == '/') && (*src != '\0')) src ++;
  270. continue;
  271. }
  272. else if (src[1] == '.')
  273. {
  274. if (!src[2])
  275. {
  276. /* '..' and ends case */
  277. src += 2;
  278. goto up_one;
  279. }
  280. else if (src[2] == '/')
  281. {
  282. /* '../' case */
  283. src += 3;
  284. while ((*src == '/') && (*src != '\0')) src ++;
  285. goto up_one;
  286. }
  287. }
  288. }
  289. /* copy up the next '/' and erase all '/' */
  290. while ((c = *src++) != '\0' && c != '/') *dst ++ = c;
  291. if (c == '/')
  292. {
  293. *dst ++ = '/';
  294. while (c == '/') c = *src++;
  295. src --;
  296. }
  297. else if (!c) break;
  298. continue;
  299. up_one:
  300. dst --;
  301. if (dst < dst0) { rt_free(fullpath); return NULL;}
  302. while (dst0 < dst && dst[-1] != '/') dst --;
  303. }
  304. *dst = '\0';
  305. /* remove '/' in the end of path if exist */
  306. dst --;
  307. if ((dst != fullpath) && (*dst == '/')) *dst = '\0';
  308. return fullpath;
  309. }
  310. /*@}*/