IFileSystem.cs 6.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210
  1. using LibHac;
  2. using LibHac.Common;
  3. using LibHac.Fs;
  4. using LibHac.Fs.Fsa;
  5. using static Ryujinx.HLE.Utilities.StringUtils;
  6. namespace Ryujinx.HLE.HOS.Services.Fs.FileSystemProxy
  7. {
  8. class IFileSystem : IpcService
  9. {
  10. private LibHac.Fs.Fsa.IFileSystem _fileSystem;
  11. public IFileSystem(LibHac.Fs.Fsa.IFileSystem provider)
  12. {
  13. _fileSystem = provider;
  14. }
  15. public LibHac.Fs.Fsa.IFileSystem GetBaseFileSystem()
  16. {
  17. return _fileSystem;
  18. }
  19. [Command(0)]
  20. // CreateFile(u32 createOption, u64 size, buffer<bytes<0x301>, 0x19, 0x301> path)
  21. public ResultCode CreateFile(ServiceCtx context)
  22. {
  23. U8Span name = ReadUtf8Span(context);
  24. CreateFileOptions createOption = (CreateFileOptions)context.RequestData.ReadInt32();
  25. context.RequestData.BaseStream.Position += 4;
  26. long size = context.RequestData.ReadInt64();
  27. return (ResultCode)_fileSystem.CreateFile(name, size, createOption).Value;
  28. }
  29. [Command(1)]
  30. // DeleteFile(buffer<bytes<0x301>, 0x19, 0x301> path)
  31. public ResultCode DeleteFile(ServiceCtx context)
  32. {
  33. U8Span name = ReadUtf8Span(context);
  34. return (ResultCode)_fileSystem.DeleteFile(name).Value;
  35. }
  36. [Command(2)]
  37. // CreateDirectory(buffer<bytes<0x301>, 0x19, 0x301> path)
  38. public ResultCode CreateDirectory(ServiceCtx context)
  39. {
  40. U8Span name = ReadUtf8Span(context);
  41. return (ResultCode)_fileSystem.CreateDirectory(name).Value;
  42. }
  43. [Command(3)]
  44. // DeleteDirectory(buffer<bytes<0x301>, 0x19, 0x301> path)
  45. public ResultCode DeleteDirectory(ServiceCtx context)
  46. {
  47. U8Span name = ReadUtf8Span(context);
  48. return (ResultCode)_fileSystem.DeleteDirectory(name).Value;
  49. }
  50. [Command(4)]
  51. // DeleteDirectoryRecursively(buffer<bytes<0x301>, 0x19, 0x301> path)
  52. public ResultCode DeleteDirectoryRecursively(ServiceCtx context)
  53. {
  54. U8Span name = ReadUtf8Span(context);
  55. return (ResultCode)_fileSystem.DeleteDirectoryRecursively(name).Value;
  56. }
  57. [Command(5)]
  58. // RenameFile(buffer<bytes<0x301>, 0x19, 0x301> oldPath, buffer<bytes<0x301>, 0x19, 0x301> newPath)
  59. public ResultCode RenameFile(ServiceCtx context)
  60. {
  61. U8Span oldName = ReadUtf8Span(context, 0);
  62. U8Span newName = ReadUtf8Span(context, 1);
  63. return (ResultCode)_fileSystem.RenameFile(oldName, newName).Value;
  64. }
  65. [Command(6)]
  66. // RenameDirectory(buffer<bytes<0x301>, 0x19, 0x301> oldPath, buffer<bytes<0x301>, 0x19, 0x301> newPath)
  67. public ResultCode RenameDirectory(ServiceCtx context)
  68. {
  69. U8Span oldName = ReadUtf8Span(context, 0);
  70. U8Span newName = ReadUtf8Span(context, 1);
  71. return (ResultCode)_fileSystem.RenameDirectory(oldName, newName).Value;
  72. }
  73. [Command(7)]
  74. // GetEntryType(buffer<bytes<0x301>, 0x19, 0x301> path) -> nn::fssrv::sf::DirectoryEntryType
  75. public ResultCode GetEntryType(ServiceCtx context)
  76. {
  77. U8Span name = ReadUtf8Span(context);
  78. Result result = _fileSystem.GetEntryType(out DirectoryEntryType entryType, name);
  79. context.ResponseData.Write((int)entryType);
  80. return (ResultCode)result.Value;
  81. }
  82. [Command(8)]
  83. // OpenFile(u32 mode, buffer<bytes<0x301>, 0x19, 0x301> path) -> object<nn::fssrv::sf::IFile> file
  84. public ResultCode OpenFile(ServiceCtx context)
  85. {
  86. OpenMode mode = (OpenMode)context.RequestData.ReadInt32();
  87. U8Span name = ReadUtf8Span(context);
  88. Result result = _fileSystem.OpenFile(out LibHac.Fs.Fsa.IFile file, name, mode);
  89. if (result.IsSuccess())
  90. {
  91. IFile fileInterface = new IFile(file);
  92. MakeObject(context, fileInterface);
  93. }
  94. return (ResultCode)result.Value;
  95. }
  96. [Command(9)]
  97. // OpenDirectory(u32 filter_flags, buffer<bytes<0x301>, 0x19, 0x301> path) -> object<nn::fssrv::sf::IDirectory> directory
  98. public ResultCode OpenDirectory(ServiceCtx context)
  99. {
  100. OpenDirectoryMode mode = (OpenDirectoryMode)context.RequestData.ReadInt32();
  101. U8Span name = ReadUtf8Span(context);
  102. Result result = _fileSystem.OpenDirectory(out LibHac.Fs.Fsa.IDirectory dir, name, mode);
  103. if (result.IsSuccess())
  104. {
  105. IDirectory dirInterface = new IDirectory(dir);
  106. MakeObject(context, dirInterface);
  107. }
  108. return (ResultCode)result.Value;
  109. }
  110. [Command(10)]
  111. // Commit()
  112. public ResultCode Commit(ServiceCtx context)
  113. {
  114. return (ResultCode)_fileSystem.Commit().Value;
  115. }
  116. [Command(11)]
  117. // GetFreeSpaceSize(buffer<bytes<0x301>, 0x19, 0x301> path) -> u64 totalFreeSpace
  118. public ResultCode GetFreeSpaceSize(ServiceCtx context)
  119. {
  120. U8Span name = ReadUtf8Span(context);
  121. Result result = _fileSystem.GetFreeSpaceSize(out long size, name);
  122. context.ResponseData.Write(size);
  123. return (ResultCode)result.Value;
  124. }
  125. [Command(12)]
  126. // GetTotalSpaceSize(buffer<bytes<0x301>, 0x19, 0x301> path) -> u64 totalSize
  127. public ResultCode GetTotalSpaceSize(ServiceCtx context)
  128. {
  129. U8Span name = ReadUtf8Span(context);
  130. Result result = _fileSystem.GetTotalSpaceSize(out long size, name);
  131. context.ResponseData.Write(size);
  132. return (ResultCode)result.Value;
  133. }
  134. [Command(13)]
  135. // CleanDirectoryRecursively(buffer<bytes<0x301>, 0x19, 0x301> path)
  136. public ResultCode CleanDirectoryRecursively(ServiceCtx context)
  137. {
  138. U8Span name = ReadUtf8Span(context);
  139. return (ResultCode)_fileSystem.CleanDirectoryRecursively(name).Value;
  140. }
  141. [Command(14)]
  142. // GetFileTimeStampRaw(buffer<bytes<0x301>, 0x19, 0x301> path) -> bytes<0x20> timestamp
  143. public ResultCode GetFileTimeStampRaw(ServiceCtx context)
  144. {
  145. U8Span name = ReadUtf8Span(context);
  146. Result result = _fileSystem.GetFileTimeStampRaw(out FileTimeStampRaw timestamp, name);
  147. context.ResponseData.Write(timestamp.Created);
  148. context.ResponseData.Write(timestamp.Modified);
  149. context.ResponseData.Write(timestamp.Accessed);
  150. byte[] data = new byte[8];
  151. // is valid?
  152. data[0] = 1;
  153. context.ResponseData.Write(data);
  154. return (ResultCode)result.Value;
  155. }
  156. }
  157. }