IFileSystem.cs 6.7 KB

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