2021-07-19 22:16:11 +02:00
|
|
|
|
using LibHac.Fs;
|
2020-06-10 07:55:59 +02:00
|
|
|
|
using LibHac.Fs.Fsa;
|
2020-01-28 08:55:39 +01:00
|
|
|
|
using Xunit;
|
|
|
|
|
|
|
|
|
|
namespace LibHac.Tests.Fs.IFileSystemTestBase
|
|
|
|
|
{
|
|
|
|
|
public abstract partial class IFileSystemTests
|
|
|
|
|
{
|
|
|
|
|
[Fact]
|
|
|
|
|
public void RenameFile_SameParentDirectory_EntryIsRenamed()
|
|
|
|
|
{
|
|
|
|
|
IFileSystem fs = CreateFileSystem();
|
|
|
|
|
|
2021-07-19 22:16:11 +02:00
|
|
|
|
fs.CreateFile("/file1", 0, CreateFileOptions.None);
|
2020-01-28 08:55:39 +01:00
|
|
|
|
|
2021-07-19 22:16:11 +02:00
|
|
|
|
Assert.Success(fs.RenameFile("/file1", "/file2"));
|
2020-01-28 08:55:39 +01:00
|
|
|
|
|
2021-07-19 22:16:11 +02:00
|
|
|
|
Assert.Success(fs.GetEntryType(out DirectoryEntryType type, "/file2"));
|
|
|
|
|
Result rc = fs.GetEntryType(out _, "/file1");
|
2020-01-28 08:55:39 +01:00
|
|
|
|
|
|
|
|
|
Assert.Equal(DirectoryEntryType.File, type);
|
2020-07-06 17:37:34 +02:00
|
|
|
|
Assert.Result(ResultFs.PathNotFound, rc);
|
2020-01-28 08:55:39 +01:00
|
|
|
|
}
|
|
|
|
|
[Fact]
|
|
|
|
|
public void RenameFile_DifferentParentDirectory_EntryIsRenamed()
|
|
|
|
|
{
|
|
|
|
|
IFileSystem fs = CreateFileSystem();
|
|
|
|
|
|
2021-07-19 22:16:11 +02:00
|
|
|
|
fs.CreateFile("/file1", 0, CreateFileOptions.None);
|
|
|
|
|
fs.CreateDirectory("/dir");
|
2020-01-28 08:55:39 +01:00
|
|
|
|
|
2021-07-19 22:16:11 +02:00
|
|
|
|
Assert.Success(fs.RenameFile("/file1", "/dir/file2"));
|
2020-01-28 08:55:39 +01:00
|
|
|
|
|
2021-07-19 22:16:11 +02:00
|
|
|
|
Assert.Success(fs.GetEntryType(out DirectoryEntryType type, "/dir/file2"));
|
|
|
|
|
Result rc = fs.GetEntryType(out _, "/file1");
|
2020-01-28 08:55:39 +01:00
|
|
|
|
|
|
|
|
|
Assert.Equal(DirectoryEntryType.File, type);
|
2020-07-06 17:37:34 +02:00
|
|
|
|
Assert.Result(ResultFs.PathNotFound, rc);
|
2020-01-28 08:55:39 +01:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
[Fact]
|
|
|
|
|
public void RenameFile_DestExistsAsFile_ReturnsPathAlreadyExists()
|
|
|
|
|
{
|
|
|
|
|
IFileSystem fs = CreateFileSystem();
|
|
|
|
|
|
2021-07-19 22:16:11 +02:00
|
|
|
|
fs.CreateFile("/file1", 0, CreateFileOptions.None);
|
|
|
|
|
fs.CreateFile("/file2", 0, CreateFileOptions.None);
|
2020-01-28 08:55:39 +01:00
|
|
|
|
|
2021-07-19 22:16:11 +02:00
|
|
|
|
Result rc = fs.RenameFile("/file1", "/file2");
|
2020-01-28 08:55:39 +01:00
|
|
|
|
|
2020-07-06 17:37:34 +02:00
|
|
|
|
Assert.Result(ResultFs.PathAlreadyExists, rc);
|
2020-01-28 08:55:39 +01:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
[Fact]
|
|
|
|
|
public void RenameFile_DestExistsAsDirectory_ReturnsPathAlreadyExists()
|
|
|
|
|
{
|
|
|
|
|
IFileSystem fs = CreateFileSystem();
|
|
|
|
|
|
2021-07-19 22:16:11 +02:00
|
|
|
|
fs.CreateFile("/file", 0, CreateFileOptions.None);
|
|
|
|
|
fs.CreateDirectory("/dir");
|
2020-01-28 08:55:39 +01:00
|
|
|
|
|
2021-07-19 22:16:11 +02:00
|
|
|
|
Result rc = fs.RenameFile("/file", "/dir");
|
2020-01-28 08:55:39 +01:00
|
|
|
|
|
2020-07-06 17:37:34 +02:00
|
|
|
|
Assert.Result(ResultFs.PathAlreadyExists, rc);
|
2020-01-28 08:55:39 +01:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
[Fact]
|
|
|
|
|
public void RenameFile_DestExistsAsFile_FileSizesDoNotChange()
|
|
|
|
|
{
|
|
|
|
|
IFileSystem fs = CreateFileSystem();
|
|
|
|
|
|
2021-07-19 22:16:11 +02:00
|
|
|
|
fs.CreateFile("/file1", 54321, CreateFileOptions.None);
|
|
|
|
|
fs.CreateFile("/file2", 12345, CreateFileOptions.None);
|
2020-01-28 08:55:39 +01:00
|
|
|
|
|
2021-07-19 22:16:11 +02:00
|
|
|
|
fs.RenameFile("/file1", "/file2");
|
2020-01-28 08:55:39 +01:00
|
|
|
|
|
2021-07-19 22:16:11 +02:00
|
|
|
|
Assert.Success(fs.OpenFile(out IFile file1, "/file1", OpenMode.Read));
|
|
|
|
|
Assert.Success(fs.OpenFile(out IFile file2, "/file2", OpenMode.Read));
|
2020-01-28 08:55:39 +01:00
|
|
|
|
|
|
|
|
|
using (file1)
|
|
|
|
|
using (file2)
|
|
|
|
|
{
|
2020-07-06 17:37:34 +02:00
|
|
|
|
Assert.Success(file1.GetSize(out long file1Size));
|
|
|
|
|
Assert.Success(file2.GetSize(out long file2Size));
|
2020-01-28 08:55:39 +01:00
|
|
|
|
|
|
|
|
|
Assert.Equal(54321, file1Size);
|
|
|
|
|
Assert.Equal(12345, file2Size);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
[Fact]
|
|
|
|
|
public void RenameFile_DataIsUnmodified()
|
|
|
|
|
{
|
2021-01-20 03:49:58 +01:00
|
|
|
|
byte[] data = { 7, 4, 1, 0, 8, 5, 2, 9, 6, 3 };
|
2020-01-28 08:55:39 +01:00
|
|
|
|
|
|
|
|
|
IFileSystem fs = CreateFileSystem();
|
|
|
|
|
|
2021-07-19 22:16:11 +02:00
|
|
|
|
fs.CreateFile("/file", data.Length, CreateFileOptions.None);
|
2020-01-28 08:55:39 +01:00
|
|
|
|
|
2021-07-19 22:16:11 +02:00
|
|
|
|
fs.OpenFile(out IFile file, "/file", OpenMode.Write);
|
2020-06-08 00:23:51 +02:00
|
|
|
|
file.Write(0, data, WriteOption.None);
|
2020-01-28 08:55:39 +01:00
|
|
|
|
file.Dispose();
|
|
|
|
|
|
2021-07-19 22:16:11 +02:00
|
|
|
|
fs.RenameFile("/file", "/renamed");
|
2020-01-28 08:55:39 +01:00
|
|
|
|
|
2021-01-20 03:49:58 +01:00
|
|
|
|
byte[] readData = new byte[data.Length];
|
2020-01-28 08:55:39 +01:00
|
|
|
|
|
2021-07-19 22:16:11 +02:00
|
|
|
|
fs.OpenFile(out file, "/renamed", OpenMode.Read);
|
2020-06-08 00:23:51 +02:00
|
|
|
|
Result rc = file.Read(out long bytesRead, 0, readData, ReadOption.None);
|
2020-01-28 08:55:39 +01:00
|
|
|
|
file.Dispose();
|
|
|
|
|
|
2020-07-06 17:37:34 +02:00
|
|
|
|
Assert.Success(rc);
|
2020-01-28 08:55:39 +01:00
|
|
|
|
Assert.Equal(data.Length, bytesRead);
|
|
|
|
|
Assert.Equal(data, readData);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|