dir.c 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142
  1. #include <stdlib.h>
  2. #include <string.h>
  3. #include "dir.h"
  4. #include "general.h"
  5. #include "debug.h"
  6. #include "internal.h"
  7. #include "file.h"
  8. #include "inode.h"
  9. #include "file_info.h"
  10. int MxfsFindFileInDir(MINIX_FS *FileSys, unsigned Inode, minix_inode *DirInfo, const char *Filename)
  11. {
  12. unsigned TotalEntries, i, j = 0, Offset;
  13. int Result;
  14. minix_dir_entry Entries[MINIX_BLOCK_SIZE / sizeof(minix_dir_entry)];
  15. TotalEntries = DirInfo->d2_size / sizeof(minix_dir_entry);
  16. for(i = 0; i < TotalEntries; ++i)
  17. {
  18. if(i % COUNTOF(Entries) == 0)
  19. {
  20. Offset = i * sizeof(minix_dir_entry);
  21. Result = MxfsGetBlockFromFileOffset(FileSys, Inode, DirInfo, Offset, FALSE);
  22. if(Result < 0)
  23. return Result;
  24. Result = MxfsCacheRead(FileSys, Entries, Result, 0, MINIX_BLOCK_SIZE);
  25. if(Result < 0)
  26. return Result;
  27. j = 0;
  28. }
  29. if(Entries[j].inode != 0 && strncmp(Filename, Entries[j].name, MAX_NAME_LEN) == 0)
  30. return Entries[j].inode;
  31. ++j;
  32. }
  33. return -1;
  34. }
  35. int MxfsParsePath(MINIX_FS *FileSys, const char *Path, BOOL Parent)
  36. {
  37. char Filename[32];
  38. unsigned FilenameLen = 0;
  39. int Result = 1;
  40. minix_inode DirInfo;
  41. while(1)
  42. {
  43. if(*Path != '/' && *Path != '\\' && *Path != '\0')
  44. {
  45. if(FilenameLen + 1 >= sizeof(Filename))
  46. return -ERROR_NOT_FOUND;
  47. Filename[FilenameLen++] = *Path;
  48. }
  49. else if(FilenameLen != 0)
  50. {
  51. if(Parent && *Path == '\0')
  52. break;
  53. Filename[FilenameLen] = '\0';
  54. MxfsReadInode(FileSys, Result, &DirInfo);
  55. if(!(DirInfo.d2_mode & I_DIRECTORY))
  56. return -ERROR_NOT_FOUND;
  57. Result = MxfsFindFileInDir(FileSys, Result, &DirInfo, Filename);
  58. FilenameLen = 0;
  59. }
  60. if(Result < 0 || *Path == '\0')
  61. break;
  62. ++Path;
  63. }
  64. return Result;
  65. }
  66. int DOKAN_CALLBACK MxfsFindFiles(
  67. LPCWSTR PathName,
  68. PFillFindData Callback,
  69. PDOKAN_FILE_INFO FileInfo)
  70. {
  71. TRACE("%ls %p", PathName, FileInfo);
  72. ASSERT(FileInfo->IsDirectory);
  73. int Result;
  74. minix_dir_entry Entries[MINIX_BLOCK_SIZE / sizeof(minix_dir_entry)];
  75. unsigned TotalEntries, FileIdx, Offset, i, j;
  76. minix_inode DirInfo, Info;
  77. MINIX_FS *FileSys = (MINIX_FS*)(LONG_PTR)FileInfo->DokanOptions->GlobalContext;
  78. FILE_CTX *FileCtx = (FILE_CTX*)(LONG_PTR)FileInfo->Context;
  79. ASSERT(FileCtx);
  80. Result = MxfsReadInode(FileSys, FileCtx->Index, &DirInfo);
  81. if(Result < 0)
  82. return Result;
  83. TotalEntries = DirInfo.d2_size / sizeof(minix_dir_entry);
  84. if(TotalEntries == 0)
  85. return 0; // empty dir
  86. WIN32_FIND_DATAW wfd;
  87. ZeroMemory(&wfd, sizeof(wfd));
  88. for(i = 0, j = 0; i < TotalEntries; ++i, ++j)
  89. {
  90. if(i % COUNTOF(Entries) == 0)
  91. {
  92. Offset = i * sizeof(minix_dir_entry);
  93. Result = MxfsGetBlockFromFileOffset(FileSys, FileCtx->Index, &DirInfo, Offset, FALSE);
  94. if(Result < 0)
  95. return Result;
  96. Result = MxfsCacheRead(FileSys, Entries, Result, 0, MINIX_BLOCK_SIZE);
  97. if(Result < 0)
  98. return Result;
  99. j = 0;
  100. }
  101. FileIdx = Entries[j].inode;
  102. if(!FileIdx)
  103. continue;
  104. if(MxfsReadInode(FileSys, FileIdx, &Info) < 0)
  105. {
  106. WARN("Invalid node in directory\n");
  107. continue;
  108. }
  109. MxfsFillFindData(&wfd, &Entries[j], &Info);
  110. Callback(&wfd, FileInfo);
  111. }
  112. return 0;
  113. }