Skip to content

Commit c157f48

Browse files
authored
Revert "Address some System.Formats.Tar TODOs (infra and syscalls) (#69107)"
This reverts commit 5408323.
1 parent 3f0c2cc commit c157f48

File tree

18 files changed

+120
-184
lines changed

18 files changed

+120
-184
lines changed

src/libraries/Common/src/Interop/Unix/System.Native/Interop.DeviceFiles.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,6 @@ internal static int CreateCharacterDevice(string pathName, uint mode, uint major
2323
private static partial int MkNod(string pathName, uint mode, uint major, uint minor);
2424

2525
[LibraryImport(Libraries.SystemNative, EntryPoint = "SystemNative_GetDeviceIdentifiers", SetLastError = true)]
26-
internal static unsafe partial void GetDeviceIdentifiers(ulong dev, uint* majorNumber, uint* minorNumber);
26+
internal static unsafe partial int GetDeviceIdentifiers(ulong dev, uint* majorNumber, uint* minorNumber);
2727
}
2828
}

src/libraries/Common/src/Interop/Unix/System.Native/Interop.GroupNameUserName.cs

Lines changed: 0 additions & 35 deletions
This file was deleted.

src/libraries/Common/src/Interop/Unix/System.Native/Interop.Stat.cs

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,6 @@ internal struct FileStatus
3131
internal long BirthTime;
3232
internal long BirthTimeNsec;
3333
internal long Dev;
34-
internal long RDev;
3534
internal long Ino;
3635
internal uint UserFlags;
3736
}

src/libraries/System.Formats.Tar/src/System.Formats.Tar.csproj

Lines changed: 1 addition & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
<Project Sdk="Microsoft.NET.Sdk">
22
<PropertyGroup>
3-
<TargetFrameworks>$(NetCoreAppCurrent)-windows;$(NetCoreAppCurrent)-Unix;$(NetCoreAppCurrent)-Browser;$(NetCoreAppCurrent)</TargetFrameworks>
3+
<TargetFrameworks>$(NetCoreAppCurrent)-windows;$(NetCoreAppCurrent)-Unix;$(NetCoreAppCurrent)</TargetFrameworks>
44
<AllowUnsafeBlocks>true</AllowUnsafeBlocks>
55
</PropertyGroup>
66

@@ -57,14 +57,10 @@
5757
<Compile Include="$(CommonPath)Interop\Unix\Interop.Errors.cs" Link="Common\Interop\Unix\System.Native\Interop.Errors.cs" />
5858
<Compile Include="$(CommonPath)Interop\Unix\System.Native\Interop.DeviceFiles.cs" Link="Common\Interop\Unix\System.Native\Interop.DeviceFiles.cs" />
5959
<Compile Include="$(CommonPath)Interop\Unix\System.Native\Interop.FChMod.cs" Link="Common\Interop\Unix\System.Native\Interop.FChMod.cs" />
60-
<Compile Include="$(CommonPath)Interop\Unix\System.Native\Interop.GroupNameUserName.cs" Link="Common\Interop\Unix\System.Native\Interop.GroupNameUserName.cs" />
6160
<Compile Include="$(CommonPath)Interop\Unix\System.Native\Interop.Link.cs" Link="Common\Interop\Unix\System.Native\Interop.Link.cs" />
6261
<Compile Include="$(CommonPath)Interop\Unix\System.Native\Interop.MkFifo.cs" Link="Common\Interop\Unix\System.Native\Interop.MkFifo.cs" />
6362
<Compile Include="$(CommonPath)Interop\Unix\System.Native\Interop.Permissions.cs" Link="Common\Interop\Unix\Interop.Permissions.cs" />
6463
<Compile Include="$(CommonPath)Interop\Unix\System.Native\Interop.Stat.cs" Link="Common\Interop\Unix\Interop.Stat.cs" />
65-
</ItemGroup>
66-
<!-- Unix and Browser specific files -->
67-
<ItemGroup Condition="'$(TargetPlatformIdentifier)' == 'Unix' or '$(TargetPlatformIdentifier)' == 'Browser'">
6864
<Compile Include="$(CommonPath)System\IO\Archiving.Utils.Unix.cs" Link="Common\System\IO\Archiving.Utils.Unix.cs" />
6965
</ItemGroup>
7066
<ItemGroup>

src/libraries/System.Formats.Tar/src/System/Formats/Tar/TarEntry.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -431,7 +431,7 @@ private void ExtractAsRegularFile(string destinationFileName)
431431
{
432432
Debug.Assert(!Path.Exists(destinationFileName));
433433

434-
FileStreamOptions fileStreamOptions = new()
434+
FileStreamOptions fileStreamOptions = new FileStreamOptions()
435435
{
436436
Access = FileAccess.Write,
437437
Mode = FileMode.CreateNew,

src/libraries/System.Formats.Tar/src/System/Formats/Tar/TarFile.cs

Lines changed: 15 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -72,8 +72,12 @@ public static void CreateFromDirectory(string sourceDirectoryName, string destin
7272
throw new DirectoryNotFoundException(string.Format(SR.IO_PathNotFound_Path, sourceDirectoryName));
7373
}
7474

75-
// Throws if the destination file exists
76-
using FileStream fs = new(destinationFileName, FileMode.CreateNew, FileAccess.Write);
75+
if (Path.Exists(destinationFileName))
76+
{
77+
throw new IOException(string.Format(SR.IO_FileExists_Name, destinationFileName));
78+
}
79+
80+
using FileStream fs = File.Create(destinationFileName, bufferSize: 0x1000, FileOptions.None);
7781

7882
CreateFromDirectoryInternal(sourceDirectoryName, fs, includeBaseDirectory, leaveOpen: false);
7983
}
@@ -166,7 +170,15 @@ public static void ExtractToDirectory(string sourceFileName, string destinationD
166170
throw new DirectoryNotFoundException(string.Format(SR.IO_PathNotFound_Path, destinationDirectoryName));
167171
}
168172

169-
using FileStream archive = File.OpenRead(sourceFileName);
173+
FileStreamOptions fileStreamOptions = new()
174+
{
175+
Access = FileAccess.Read,
176+
BufferSize = 0x1000,
177+
Mode = FileMode.Open,
178+
Share = FileShare.Read
179+
};
180+
181+
using FileStream archive = File.Open(sourceFileName, fileStreamOptions);
170182

171183
ExtractToDirectoryInternal(archive, destinationDirectoryName, overwriteFiles, leaveOpen: false);
172184
}

src/libraries/System.Formats.Tar/src/System/Formats/Tar/TarWriter.Unix.cs

Lines changed: 16 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,6 @@
11
// Licensed to the .NET Foundation under one or more agreements.
22
// The .NET Foundation licenses this file to you under the MIT license.
33

4-
using System.Collections.Generic;
54
using System.Diagnostics;
65
using System.IO;
76

@@ -10,9 +9,6 @@ namespace System.Formats.Tar
109
// Unix specific methods for the TarWriter class.
1110
public sealed partial class TarWriter : IDisposable
1211
{
13-
private readonly Dictionary<uint, string> _userIdentifiers = new Dictionary<uint, string>();
14-
private readonly Dictionary<uint, string> _groupIdentifiers = new Dictionary<uint, string>();
15-
1612
// Unix specific implementation of the method that reads an entry from disk and writes it into the archive stream.
1713
partial void ReadFileFromDiskAndWriteToArchiveStreamAsEntry(string fullPath, string entryName)
1814
{
@@ -45,13 +41,13 @@ partial void ReadFileFromDiskAndWriteToArchiveStreamAsEntry(string fullPath, str
4541
_ => throw new FormatException(string.Format(SR.TarInvalidFormat, Format)),
4642
};
4743

48-
if (entryType is TarEntryType.BlockDevice or TarEntryType.CharacterDevice)
44+
if ((entryType is TarEntryType.BlockDevice or TarEntryType.CharacterDevice) && status.Dev > 0)
4945
{
5046
uint major;
5147
uint minor;
5248
unsafe
5349
{
54-
Interop.Sys.GetDeviceIdentifiers((ulong)status.RDev, &major, &minor);
50+
Interop.CheckIo(Interop.Sys.GetDeviceIdentifiers((ulong)status.Dev, &major, &minor));
5551
}
5652

5753
entry._header._devMajor = (int)major;
@@ -64,23 +60,12 @@ partial void ReadFileFromDiskAndWriteToArchiveStreamAsEntry(string fullPath, str
6460

6561
entry._header._mode = (status.Mode & 4095); // First 12 bits
6662

67-
// Uid and UName
68-
entry._header._uid = (int)status.Uid;
69-
if (!_userIdentifiers.TryGetValue(status.Uid, out string? uName))
70-
{
71-
uName = Interop.Sys.GetUserName(status.Uid);
72-
_userIdentifiers.Add(status.Uid, uName);
73-
}
74-
entry._header._uName = uName;
63+
entry.Uid = (int)status.Uid;
64+
entry.Gid = (int)status.Gid;
7565

76-
// Gid and GName
77-
entry._header._gid = (int)status.Gid;
78-
if (!_groupIdentifiers.TryGetValue(status.Gid, out string? gName))
79-
{
80-
gName = Interop.Sys.GetGroupName(status.Gid);
81-
_groupIdentifiers.Add(status.Gid, gName);
82-
}
83-
entry._header._gName = gName;
66+
// TODO: Add these p/invokes https://github.com/dotnet/runtime/issues/68230
67+
entry._header._uName = "";// Interop.Sys.GetUName();
68+
entry._header._gName = "";// Interop.Sys.GetGName();
8469

8570
if (entry.EntryType == TarEntryType.SymbolicLink)
8671
{
@@ -89,8 +74,16 @@ partial void ReadFileFromDiskAndWriteToArchiveStreamAsEntry(string fullPath, str
8974

9075
if (entry.EntryType is TarEntryType.RegularFile or TarEntryType.V7RegularFile)
9176
{
77+
FileStreamOptions options = new()
78+
{
79+
Mode = FileMode.Open,
80+
Access = FileAccess.Read,
81+
Share = FileShare.Read,
82+
Options = FileOptions.None
83+
};
84+
9285
Debug.Assert(entry._header._dataStream == null);
93-
entry._header._dataStream = File.OpenRead(fullPath);
86+
entry._header._dataStream = File.Open(fullPath, options);
9487
}
9588

9689
WriteEntry(entry);

src/libraries/System.Formats.Tar/tests/System.Formats.Tar.Tests.csproj

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -56,7 +56,6 @@
5656
<Compile Include="$(CommonPath)Interop\Unix\Interop.IOErrors.cs" Link="Common\Interop\Unix\Interop.IOErrors.cs" />
5757
<Compile Include="$(CommonPath)Interop\Unix\Interop.Libraries.cs" Link="Common\Interop\Unix\Interop.Libraries.cs" />
5858
<Compile Include="$(CommonPath)Interop\Unix\System.Native\Interop.DeviceFiles.cs" Link="Common\Interop\Unix\System.Native\Interop.DeviceFiles.cs" />
59-
<Compile Include="$(CommonPath)Interop\Unix\System.Native\Interop.GroupNameUserName.cs" Link="Common\Interop\Unix\System.Native\Interop.GroupNameUserName.cs" />
6059
<Compile Include="$(CommonPath)Interop\Unix\System.Native\Interop.Link.cs" Link="Common\Interop\Unix\System.Native\Interop.Link.cs" />
6160
<Compile Include="$(CommonPath)Interop\Unix\System.Native\Interop.MkFifo.cs" Link="Common\Interop\Unix\System.Native\Interop.MkFifo.cs" />
6261
<Compile Include="$(CommonPath)Interop\Unix\System.Native\Interop.Stat.cs" Link="Common\Interop\Unix\System.Native\Interop.Stat.cs" />
@@ -67,4 +66,7 @@
6766
<ItemGroup>
6867
<PackageReference Include="System.Formats.Tar.TestData" Version="$(SystemFormatsTarTestDataVersion)" />
6968
</ItemGroup>
69+
<ItemGroup>
70+
<ProjectReference Include="..\src\System.Formats.Tar.csproj" />
71+
</ItemGroup>
7072
</Project>

src/libraries/System.Formats.Tar/tests/TarFile/TarFile.ExtractToDirectory.File.Tests.cs

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -100,7 +100,10 @@ public void Extract_Archive_File_OverwriteFalse()
100100

101101
string filePath = Path.Join(destination.Path, "file.txt");
102102

103-
File.Create(filePath).Dispose();
103+
using (StreamWriter writer = File.CreateText(filePath))
104+
{
105+
writer.WriteLine("My existence should cause an exception");
106+
}
104107

105108
Assert.Throws<IOException>(() => TarFile.ExtractToDirectory(sourceArchiveFileName, destination.Path, overwriteFiles: false));
106109
}

src/libraries/System.Formats.Tar/tests/TarReader/TarReader.File.Tests.cs

Lines changed: 14 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -713,8 +713,13 @@ private void Verify_Archive_BlockDevice(PosixTarEntry blockDevice, IReadOnlyDict
713713
Assert.True(blockDevice.ModificationTime > DateTimeOffset.UnixEpoch);
714714
Assert.Equal(expectedFileName, blockDevice.Name);
715715
Assert.Equal(AssetUid, blockDevice.Uid);
716-
Assert.Equal(AssetBlockDeviceMajor, blockDevice.DeviceMajor);
717-
Assert.Equal(AssetBlockDeviceMinor, blockDevice.DeviceMinor);
716+
717+
// TODO: Figure out why the numbers don't match https://github.com/dotnet/runtime/issues/68230
718+
// Assert.Equal(AssetBlockDeviceMajor, blockDevice.DeviceMajor);
719+
// Assert.Equal(AssetBlockDeviceMinor, blockDevice.DeviceMinor);
720+
// Remove these two temporary checks when the above is fixed
721+
Assert.True(blockDevice.DeviceMajor > 0);
722+
Assert.True(blockDevice.DeviceMinor > 0);
718723
Assert.Equal(AssetGName, blockDevice.GroupName);
719724
Assert.Equal(AssetUName, blockDevice.UserName);
720725

@@ -744,8 +749,13 @@ private void Verify_Archive_CharacterDevice(PosixTarEntry characterDevice, IRead
744749
Assert.True(characterDevice.ModificationTime > DateTimeOffset.UnixEpoch);
745750
Assert.Equal(expectedFileName, characterDevice.Name);
746751
Assert.Equal(AssetUid, characterDevice.Uid);
747-
Assert.Equal(AssetCharacterDeviceMajor, characterDevice.DeviceMajor);
748-
Assert.Equal(AssetCharacterDeviceMinor, characterDevice.DeviceMinor);
752+
753+
// TODO: Figure out why the numbers don't match https://github.com/dotnet/runtime/issues/68230
754+
//Assert.Equal(AssetBlockDeviceMajor, characterDevice.DeviceMajor);
755+
//Assert.Equal(AssetBlockDeviceMinor, characterDevice.DeviceMinor);
756+
// Remove these two temporary checks when the above is fixed
757+
Assert.True(characterDevice.DeviceMajor > 0);
758+
Assert.True(characterDevice.DeviceMinor > 0);
749759
Assert.Equal(AssetGName, characterDevice.GroupName);
750760
Assert.Equal(AssetUName, characterDevice.UserName);
751761

0 commit comments

Comments
 (0)