dfs_win32.c 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600
  1. /*
  2. * Copyright (c) 2006-2021, RT-Thread Development Team
  3. *
  4. * SPDX-License-Identifier: Apache-2.0
  5. *
  6. * Change Logs:
  7. * Date Author Notes
  8. * 2012-11-27 prife the first version
  9. * 2013-03-03 aozima add dfs_win32_stat st_mtime support.
  10. * 2017-10-20 urey support rt-thread 3.0
  11. */
  12. #include <rtthread.h>
  13. #include <rtlibc.h>
  14. #include <dfs_fs.h>
  15. #include <dfs_file.h>
  16. #include <rtdevice.h>
  17. #include <io.h>
  18. #include <fcntl.h>
  19. #include <string.h>
  20. #include <stdio.h>
  21. #include <string.h>
  22. #include <WinError.h>
  23. #include <windows.h>
  24. #if defined(__MINGW32__) && defined(_NO_OLDNAMES)
  25. #define O_RDONLY _O_RDONLY
  26. #define O_WRONLY _O_WRONLY
  27. #define O_RDWR _O_RDWR
  28. #define O_ACCMODE _O_ACCMODE
  29. #define O_APPEND _O_APPEND
  30. #define O_CREAT _O_CREAT
  31. #define O_TRUNC _O_TRUNC
  32. #define O_EXCL _O_EXCL
  33. #define O_TEXT _O_TEXT
  34. #define O_BINARY _O_BINARY
  35. #define O_TEMPORARY _O_TEMPORARY
  36. #define O_NOINHERIT _O_NOINHERIT
  37. #define O_SEQUENTIAL _O_SEQUENTIAL
  38. #define O_RANDOM _O_RANDOM
  39. #endif
  40. /*
  41. * RT-Thread DFS Interface for win-directory as an disk device
  42. */
  43. #define FILE_PATH_MAX 256 /* the longest file path */
  44. #define WIN32_DIRDISK_ROOT "./disk"
  45. typedef struct {
  46. HANDLE handle;
  47. char *start;
  48. char *end;
  49. char *curr;
  50. struct _finddata_t finddata;
  51. } WINDIR;
  52. /* There are so many error codes in windows, you'd better google for details.
  53. * google "System Error Codes (Windows)"
  54. * http://msdn.microsoft.com/ZH-CN/library/windows/desktop/ms681381(v=vs.85).aspx
  55. */
  56. struct _errcode_map
  57. {
  58. rt_uint16_t dfserr;
  59. rt_uint16_t win32err;
  60. };
  61. static const struct _errcode_map errcode_table[] =
  62. {
  63. {ENOENT, ERROR_FILE_NOT_FOUND },
  64. {ENOENT, ERROR_PATH_NOT_FOUND },
  65. {EEXIST, ERROR_FILE_EXISTS },
  66. {EEXIST, ERROR_ALREADY_EXISTS },
  67. {ENOTEMPTY, ERROR_DIR_NOT_EMPTY },
  68. {EBUSY, ERROR_PATH_BUSY },
  69. {EINVAL, ERROR_ACCESS_DENIED },
  70. #if 0 /* TODO: MORE NEED BE ADDED */
  71. {DFS_STATUS_EISDIR, ERROR_FILE_EXISTS },
  72. {DFS_STATUS_ENOTDIR, ERROR_FILE_EXISTS },
  73. {DFS_STATUS_EBADF, ERROR_FILE_EXISTS },
  74. {DFS_STATUS_EBUSY, ERROR_FILE_EXISTS },
  75. {DFS_STATUS_ENOMEM, ERROR_FILE_EXISTS },
  76. {DFS_STATUS_ENOSPC, ERROR_FILE_EXISTS },
  77. #endif
  78. };
  79. static int win32_result_to_dfs(DWORD res)
  80. {
  81. int i;
  82. int err = 0;
  83. for (i = 0; i < sizeof(errcode_table) / sizeof(struct _errcode_map); i++)
  84. {
  85. if (errcode_table[i].win32err == (res & 0xFFFF))
  86. {
  87. err = -errcode_table[i].dfserr;
  88. return err;
  89. }
  90. }
  91. /* unknown error */
  92. rt_kprintf("dfs win32 error not supported yet: %d\n", res);
  93. return -1;
  94. }
  95. static int dfs_win32_mount(
  96. struct dfs_filesystem *fs,
  97. unsigned long rwflag,
  98. const void *data)
  99. {
  100. return 0;
  101. }
  102. static int dfs_win32_unmount(struct dfs_filesystem *fs)
  103. {
  104. return 0;
  105. }
  106. static int dfs_win32_mkfs(rt_device_t devid)
  107. {
  108. return -ENOSYS;
  109. }
  110. static int dfs_win32_statfs(struct dfs_filesystem *fs,
  111. struct statfs *buf)
  112. {
  113. return -ENOSYS;
  114. }
  115. static char *winpath_dirdup(char *des, const char *src)
  116. {
  117. char *path;
  118. int i = 0;
  119. path = rt_malloc(FILE_PATH_MAX);
  120. if (path == RT_NULL)
  121. return RT_NULL;
  122. strcpy(path, des);
  123. strcat(path, src);
  124. while (1)
  125. {
  126. if (path[i] == 0)
  127. break;
  128. if (path[i] == '/')
  129. path[i] = '\\';
  130. i++;
  131. }
  132. return path;
  133. }
  134. /* This function can convert the path in rt-thread/dfs to the path in windows */
  135. char * dfs_win32_dirdup(char * path)
  136. {
  137. char * file_path;
  138. file_path = winpath_dirdup(WIN32_DIRDISK_ROOT, path);
  139. return file_path;
  140. }
  141. static int dfs_win32_open(struct dfs_fd *file)
  142. {
  143. int fd;
  144. uint32_t oflag, mode;
  145. char *file_path;
  146. int res;
  147. oflag = file->flags;
  148. if (oflag & O_DIRECTORY) /* operations about dir */
  149. {
  150. WINDIR *wdirp;
  151. HANDLE handle;
  152. int len;
  153. file_path = winpath_dirdup(WIN32_DIRDISK_ROOT, file->path);
  154. if (oflag & O_CREAT) /* create a dir*/
  155. {
  156. res = CreateDirectory(file_path, NULL);
  157. if (res == 0)
  158. {
  159. rt_free(file_path);
  160. return win32_result_to_dfs(GetLastError());
  161. }
  162. }
  163. len = strlen(file_path);
  164. if (file_path[len - 1] != '\\')
  165. {
  166. file_path[len] = '\\';
  167. file_path[len + 1] = 0;
  168. }
  169. strcat(file_path, "*.*");
  170. /* _findfirst will get '.' */
  171. /* save this pointer,will used by dfs_win32_getdents*/
  172. wdirp = rt_malloc(sizeof(WINDIR));
  173. RT_ASSERT(wdirp != NULL);
  174. if ((handle = _findfirst(file_path, &wdirp->finddata)) == -1)
  175. {
  176. rt_free(wdirp);
  177. rt_free(file_path);
  178. goto __err;
  179. }
  180. len = strlen(wdirp->finddata.name) + 1;
  181. wdirp->handle = handle;
  182. //wdirp->nfiles = 1;
  183. wdirp->start = malloc(len); //not rt_malloc!
  184. wdirp->end = wdirp->curr = wdirp->start;
  185. wdirp->end += len;
  186. strncpy(wdirp->curr, wdirp->finddata.name, len);
  187. file->data = (void *)wdirp;
  188. rt_free(file_path);
  189. return 0;
  190. }
  191. /* regular file operations */
  192. mode = O_BINARY;
  193. if (oflag & O_RDONLY) mode |= O_RDONLY;
  194. if (oflag & O_WRONLY) mode |= O_WRONLY;
  195. if (oflag & O_RDWR) mode |= O_RDWR;
  196. /* Opens the file, if it is existing. If not, a new file is created. */
  197. if (oflag & O_CREAT) mode |= O_CREAT;
  198. /* Creates a new file. If the file is existing, it is truncated and overwritten. */
  199. if (oflag & O_TRUNC) mode |= O_TRUNC;
  200. /* Creates a new file. The function fails if the file is already existing. */
  201. if (oflag & O_EXCL) mode |= O_EXCL;
  202. file_path = winpath_dirdup(WIN32_DIRDISK_ROOT, file->path);
  203. fd = _open(file_path, mode, 0x0100 | 0x0080); /* _S_IREAD | _S_IWRITE */
  204. rt_free(file_path);
  205. if (fd < 0)
  206. goto __err;
  207. /* save this pointer, it will be used when calling read(), write(),
  208. * flush(), seek(), and will be free when calling close()*/
  209. file->data = (void *)fd;
  210. file->pos = 0;
  211. file->size = _lseek(fd, 0, SEEK_END);
  212. if (oflag & O_APPEND)
  213. {
  214. file->pos = file->size;
  215. }
  216. else
  217. _lseek(fd, 0, SEEK_SET);
  218. return 0;
  219. __err:
  220. res = GetLastError();
  221. return win32_result_to_dfs(res);
  222. }
  223. static int dfs_win32_close(struct dfs_fd *file)
  224. {
  225. if (file->flags & O_DIRECTORY)
  226. {
  227. WINDIR *wdirp = (WINDIR*)(file->data);
  228. RT_ASSERT(wdirp != RT_NULL);
  229. if (_findclose((intptr_t)wdirp->handle) == 0) {
  230. free(wdirp->start); //NOTE: here we don't use rt_free!
  231. rt_free(wdirp);
  232. return 0;
  233. }
  234. }
  235. else /* regular file operations */
  236. {
  237. if (_close((int)(file->data)) == 0)
  238. return 0;
  239. }
  240. return win32_result_to_dfs(GetLastError());
  241. }
  242. static int dfs_win32_ioctl(struct dfs_fd *file, int cmd, void *args)
  243. {
  244. return -ENOSYS;
  245. }
  246. static int dfs_win32_read(struct dfs_fd *file, void *buf, size_t len)
  247. {
  248. int fd;
  249. int char_read;
  250. fd = (int)(file->data);
  251. char_read = _read(fd, buf, len);
  252. if (char_read < 0)
  253. return win32_result_to_dfs(GetLastError());
  254. /* update position */
  255. file->pos = _lseek(fd, 0, SEEK_CUR);
  256. return char_read;
  257. }
  258. static int dfs_win32_write(struct dfs_fd *file, const void *buf, size_t len)
  259. {
  260. int fd;
  261. int char_write;
  262. fd = (int)(file->data);
  263. char_write = _write(fd, buf, len);
  264. if (char_write < 0)
  265. return win32_result_to_dfs(GetLastError());
  266. /* update position */
  267. file->pos = _lseek(fd, 0, SEEK_CUR);
  268. return char_write;
  269. }
  270. static int dfs_win32_flush(struct dfs_fd *file)
  271. {
  272. return 0;
  273. }
  274. static int dfs_win32_seek(struct dfs_fd *file,
  275. rt_off_t offset)
  276. {
  277. int result;
  278. /* set offset as current offset */
  279. if (file->type == FT_DIRECTORY)
  280. {
  281. WINDIR* wdirp = (WINDIR*)(file->data);
  282. RT_ASSERT(wdirp != RT_NULL);
  283. wdirp->curr = wdirp->start + offset;
  284. return offset;
  285. }
  286. else //file->type == FT_REGULAR)
  287. {
  288. result = _lseek((int)(file->data), offset, SEEK_SET);
  289. if (result >= 0)
  290. return offset;
  291. else
  292. return win32_result_to_dfs(GetLastError());
  293. }
  294. }
  295. /* return the size of struct dirent*/
  296. static int dfs_win32_getdents(struct dfs_fd *file, struct dirent *dirp, rt_uint32_t count)
  297. {
  298. WINDIR *wdirp;
  299. struct dirent *d = dirp;
  300. int result;
  301. /* make integer count */
  302. if (count / sizeof(struct dirent) != 1)
  303. return -EINVAL;
  304. wdirp = (WINDIR*)(file->data);
  305. RT_ASSERT(wdirp != RT_NULL);
  306. if (wdirp->curr == NULL) //no more entries in this directory
  307. return 0;
  308. /* get the current entry */
  309. if (wdirp->finddata.attrib & _A_SUBDIR)
  310. d->d_type = DT_DIR;
  311. else
  312. d->d_type = DT_REG;
  313. d->d_namlen = strlen(wdirp->curr);
  314. strncpy(d->d_name, wdirp->curr, DFS_PATH_MAX);
  315. d->d_reclen = (rt_uint16_t)sizeof(struct dirent);
  316. wdirp->curr += (strlen(wdirp->curr) + 1);
  317. file->pos = wdirp->curr - wdirp->start + sizeof(struct dirent);//NOTE!
  318. /* now set up for the next call to readdir */
  319. if (wdirp->curr >= wdirp->end)
  320. {
  321. if (_findnext(wdirp->handle, &wdirp->finddata) == 0)
  322. {
  323. char* old_start = wdirp->start;
  324. long name_len = strlen(wdirp->finddata.name) + 1;
  325. wdirp->start = realloc(wdirp->start, wdirp->end - wdirp->start + name_len);
  326. wdirp->curr = wdirp->start + (wdirp->curr - old_start);
  327. wdirp->end = wdirp->curr + name_len;
  328. strcpy(wdirp->curr, wdirp->finddata.name);
  329. }
  330. else
  331. {
  332. if ((result = GetLastError()) == ERROR_NO_MORE_FILES)
  333. wdirp->curr = NULL;
  334. else
  335. return win32_result_to_dfs(result);
  336. }
  337. }
  338. return sizeof(struct dirent);
  339. }
  340. static int dfs_win32_unlink(struct dfs_filesystem *fs, const char *path)
  341. {
  342. int result;
  343. char *fp;
  344. fp = winpath_dirdup(WIN32_DIRDISK_ROOT, path);
  345. if (fp == RT_NULL)
  346. {
  347. rt_kprintf("out of memory.\n");
  348. return -ENOMEM;
  349. }
  350. result = GetFileAttributes(fp);
  351. if (result == INVALID_FILE_ATTRIBUTES)
  352. goto __err;
  353. if (result & FILE_ATTRIBUTE_DIRECTORY)//winnt.h
  354. {
  355. if (RemoveDirectory(fp) == RT_FALSE)
  356. goto __err;
  357. }
  358. else //(result & FILE_ATTRIBUTE_NORMAL)
  359. {
  360. if (_unlink(fp) < 0)
  361. goto __err;
  362. }
  363. rt_free(fp);
  364. return 0;
  365. __err:
  366. rt_free(fp);
  367. return win32_result_to_dfs(GetLastError());
  368. }
  369. static int dfs_win32_rename(
  370. struct dfs_filesystem *fs,
  371. const char *oldpath,
  372. const char *newpath)
  373. {
  374. int result;
  375. char *op, *np;
  376. op = winpath_dirdup(WIN32_DIRDISK_ROOT, oldpath);
  377. np = winpath_dirdup(WIN32_DIRDISK_ROOT, newpath);
  378. if (op == RT_NULL || np == RT_NULL)
  379. {
  380. rt_kprintf("out of memory.\n");
  381. return -ENOMEM;
  382. }
  383. /* If the function fails, the return value is zero. */
  384. result = MoveFile(op, np);
  385. rt_free(op);
  386. rt_free(np);
  387. if (result == 0)
  388. return win32_result_to_dfs(GetLastError());
  389. return 0;
  390. }
  391. static int dfs_win32_stat(struct dfs_filesystem *fs, const char *path, struct stat *st)
  392. {
  393. WIN32_FIND_DATA fileInfo;
  394. HANDLE hFind;
  395. char *fp;
  396. fp = winpath_dirdup(WIN32_DIRDISK_ROOT, path);
  397. if (fp == RT_NULL)
  398. {
  399. rt_kprintf("out of memory.\n");
  400. return -ENOMEM;
  401. }
  402. hFind = FindFirstFile(fp, &fileInfo);
  403. rt_free(fp);
  404. if (hFind == INVALID_HANDLE_VALUE)
  405. goto __err;
  406. st->st_mode = S_IFREG | S_IRUSR | S_IRGRP | S_IROTH |
  407. S_IWUSR | S_IWGRP | S_IWOTH;
  408. /* convert file info to dfs stat structure */
  409. if (fileInfo.dwFileAttributes & FILE_ATTRIBUTE_DIRECTORY)
  410. {
  411. st->st_mode &= ~S_IFREG;
  412. st->st_mode |= S_IFDIR | S_IXUSR | S_IXGRP | S_IXOTH;
  413. }
  414. st->st_dev = 0;
  415. st->st_size = fileInfo.nFileSizeLow;
  416. /* get st_mtime. */
  417. {
  418. LARGE_INTEGER time_tmp;
  419. time_tmp.LowPart = fileInfo.ftLastWriteTime.dwLowDateTime;
  420. time_tmp.HighPart = fileInfo.ftLastWriteTime.dwHighDateTime;
  421. /* removes the diff between 1970 and 1601. */
  422. time_tmp.QuadPart -= 11644473600000 * 10000;
  423. /* converts back from 100-nanoseconds to seconds. */
  424. time_tmp.QuadPart /= 10UL * 1000 * 1000;
  425. st->st_mtime = time_tmp.QuadPart;
  426. }
  427. FindClose(hFind);
  428. return 0;
  429. __err:
  430. return win32_result_to_dfs(GetLastError());
  431. }
  432. static const struct dfs_file_ops dfs_win32_file_ops =
  433. {
  434. dfs_win32_open,
  435. dfs_win32_close,
  436. dfs_win32_ioctl,
  437. dfs_win32_read,
  438. dfs_win32_write,
  439. dfs_win32_flush,
  440. dfs_win32_seek,
  441. dfs_win32_getdents,
  442. };
  443. static const struct dfs_filesystem_ops dfs_win32_ops =
  444. {
  445. "wdir", /* file system type: dir */
  446. DFS_FS_FLAG_DEFAULT,
  447. &dfs_win32_file_ops,
  448. dfs_win32_mount,
  449. dfs_win32_unmount,
  450. dfs_win32_mkfs,
  451. dfs_win32_statfs,
  452. dfs_win32_unlink,
  453. dfs_win32_stat,
  454. dfs_win32_rename,
  455. };
  456. int dfs_win32_init(void)
  457. {
  458. /* register uffs file system */
  459. dfs_register(&dfs_win32_ops);
  460. return 0;
  461. }
  462. static rt_err_t nop_init(rt_device_t dev)
  463. {
  464. return RT_EOK;
  465. }
  466. static rt_err_t nop_open(rt_device_t dev, rt_uint16_t oflag)
  467. {
  468. return RT_EOK;
  469. }
  470. static rt_err_t nop_close(rt_device_t dev)
  471. {
  472. return RT_EOK;
  473. }
  474. static rt_size_t nop_read(rt_device_t dev,
  475. rt_off_t pos,
  476. void *buffer,
  477. rt_size_t size)
  478. {
  479. return size;
  480. }
  481. static rt_size_t nop_write(rt_device_t dev,
  482. rt_off_t pos,
  483. const void *buffer,
  484. rt_size_t size)
  485. {
  486. return size;
  487. }
  488. static rt_err_t nop_control(rt_device_t dev, int cmd, void *args)
  489. {
  490. return RT_EOK;
  491. }
  492. static struct rt_device win_sharedir_dev;
  493. rt_err_t rt_win_sharedir_init(const char *name)
  494. {
  495. rt_device_t dev;
  496. dev = &win_sharedir_dev;
  497. RT_ASSERT(dev != RT_NULL);
  498. /* set device class and generic device interface */
  499. dev->type = RT_Device_Class_Block;
  500. dev->init = nop_init;
  501. dev->open = nop_open;
  502. dev->read = nop_read;
  503. dev->write = nop_write;
  504. dev->close = nop_close;
  505. dev->control = nop_control;
  506. dev->rx_indicate = RT_NULL;
  507. dev->tx_complete = RT_NULL;
  508. /* register to RT-Thread device system */
  509. return rt_device_register(dev, name, RT_DEVICE_FLAG_RDWR | RT_DEVICE_FLAG_STANDALONE);
  510. }