src\libraries\System.Private.CoreLib\src\Microsoft\Win32\SafeHandles\SafeFileHandle.Unix.cs (68)
91private static SafeFileHandle Open(string path, Interop.Sys.OpenFlags flags, int mode, bool failForSymlink, out bool wasSymlink,
92Func<Interop.ErrorInfo, Interop.Sys.OpenFlags, string, Exception?>? createOpenException)
96SafeFileHandle handle = Interop.Sys.Open(path, flags, mode);
101Interop.ErrorInfo error = Interop.Sys.GetLastErrorInfo();
137Interop.Sys.Unlink(_path); // ignore errors; it's valid that the path may no longer exist
147Interop.Sys.FLock(handle, Interop.Sys.LockOperations.LOCK_UN); // ignore any errors
152return Interop.Sys.Close(handle) == 0;
174Func<Interop.ErrorInfo, Interop.Sys.OpenFlags, string, Exception?>? createOpenException = null)
180Func<Interop.ErrorInfo, Interop.Sys.OpenFlags, string, Exception?>? createOpenException = null)
187Func<Interop.ErrorInfo, Interop.Sys.OpenFlags, string, Exception?>? createOpenException = null)
190Interop.Sys.OpenFlags openFlags = PreOpenConfigurationFromOptions(mode, access, share, options, failForSymlink);
233private static Interop.Sys.OpenFlags PreOpenConfigurationFromOptions(FileMode mode, FileAccess access, FileShare share, FileOptions options, bool failForSymlink)
236Interop.Sys.OpenFlags flags = default;
239flags |= Interop.Sys.OpenFlags.O_NOFOLLOW;
251flags |= Interop.Sys.OpenFlags.O_TRUNC;
257flags |= Interop.Sys.OpenFlags.O_CREAT;
261flags |= Interop.Sys.OpenFlags.O_CREAT;
264flags |= Interop.Sys.OpenFlags.O_TRUNC;
269flags |= (Interop.Sys.OpenFlags.O_CREAT | Interop.Sys.OpenFlags.O_EXCL);
277flags |= Interop.Sys.OpenFlags.O_RDONLY;
281flags |= Interop.Sys.OpenFlags.O_RDWR;
285flags |= Interop.Sys.OpenFlags.O_WRONLY;
292flags |= Interop.Sys.OpenFlags.O_CLOEXEC;
304flags |= Interop.Sys.OpenFlags.O_SYNC;
313Interop.Sys.FileStatus status = default;
325if ((status.Mode & Interop.Sys.FileTypes.S_IFMT) == Interop.Sys.FileTypes.S_IFDIR)
330if ((status.Mode & Interop.Sys.FileTypes.S_IFMT) == Interop.Sys.FileTypes.S_IFREG)
336Debug.Assert(Interop.Sys.LSeek(this, 0, Interop.Sys.SeekWhence.SEEK_CUR) >= 0);
348Interop.Sys.LockOperations lockOperation = (share == FileShare.None) ? Interop.Sys.LockOperations.LOCK_EX : Interop.Sys.LockOperations.LOCK_SH;
349if (CanLockTheFile(lockOperation, access) && !(_isLocked = Interop.Sys.FLock(this, lockOperation | Interop.Sys.LockOperations.LOCK_NB) >= 0))
355Interop.ErrorInfo errorInfo = Interop.Sys.GetLastErrorInfo();
375Interop.Sys.FileStatus pathStatus;
376if (Interop.Sys.Stat(path, out pathStatus) < 0)
381Interop.ErrorInfo error = Interop.Sys.GetLastErrorInfo();
403Interop.Sys.FileAdvice fadv =
404(options & FileOptions.RandomAccess) != 0 ? Interop.Sys.FileAdvice.POSIX_FADV_RANDOM :
405(options & FileOptions.SequentialScan) != 0 ? Interop.Sys.FileAdvice.POSIX_FADV_SEQUENTIAL :
409FileStreamHelpers.CheckFileCall(Interop.Sys.PosixFAdvise(this, 0, 0, fadv), path,
417if (Interop.Sys.FTruncate(this, 0) < 0)
419Interop.ErrorInfo errorInfo = Interop.Sys.GetLastErrorInfo();
430if (preallocationSize > 0 && Interop.Sys.FAllocate(this, 0, preallocationSize) < 0)
432Interop.ErrorInfo errorInfo = Interop.Sys.GetLastErrorInfo();
442Interop.Sys.Unlink(path!);
454private bool CanLockTheFile(Interop.Sys.LockOperations lockOperation, FileAccess access)
456Debug.Assert(lockOperation == Interop.Sys.LockOperations.LOCK_EX || lockOperation == Interop.Sys.LockOperations.LOCK_SH);
462else if (lockOperation == Interop.Sys.LockOperations.LOCK_EX)
471if (!Interop.Sys.TryGetFileSystemType(this, out Interop.Sys.UnixFileSystemTypes unixFileSystemType))
478case Interop.Sys.UnixFileSystemTypes.nfs: // #44546
479case Interop.Sys.UnixFileSystemTypes.smb:
480case Interop.Sys.UnixFileSystemTypes.smb2: // #53182
481case Interop.Sys.UnixFileSystemTypes.cifs:
488private void FStatCheckIO(string path, ref Interop.Sys.FileStatus status, ref bool statusHasValue)
492if (Interop.Sys.FStat(this, out status) != 0)
494Interop.ErrorInfo error = Interop.Sys.GetLastErrorInfo();
510_canSeek = canSeek = Interop.Sys.LSeek(this, 0, Interop.Sys.SeekWhence.SEEK_CUR) >= 0 ? NullableBool.True : NullableBool.False;
518int result = Interop.Sys.FStat(this, out Interop.Sys.FileStatus status);
src\libraries\System.Private.CoreLib\src\System\IO\FileStatus.Unix.cs (34)
29private Interop.Sys.FileStatus _fileCache;
44return EntryExists && (_fileCache.UserFlags & (uint)Interop.Sys.UserFlags.UF_HIDDEN) == (uint)Interop.Sys.UserFlags.UF_HIDDEN;
106if (_fileCache.Uid == Interop.Sys.GetEUid())
118if (Interop.Sys.IsMemberOfGroup(_fileCache.Gid))
138return EntryExists && (_fileCache.Mode & Interop.Sys.FileTypes.S_IFMT) == Interop.Sys.FileTypes.S_IFLNK;
162if (!Interop.Sys.SupportsHiddenFlag)
246if (Interop.Sys.CanSetHiddenFlag)
251uint flags = hidden ? _fileCache.UserFlags | (uint)Interop.Sys.UserFlags.UF_HIDDEN :
252_fileCache.UserFlags & ~(uint)Interop.Sys.UserFlags.UF_HIDDEN;
253int rv = handle is not null ? Interop.Sys.FChflags(handle, flags) :
254Interop.Sys.LChflags(path!, flags);
277int rv = handle is not null ? Interop.Sys.FChMod(handle, newMode) :
278Interop.Sys.ChMod(path!, newMode);
304if ((_fileCache.Flags & Interop.Sys.FileStatusFlags.HasBirthTime) != 0)
392Interop.Sys.TimeSpec* buf = stackalloc Interop.Sys.TimeSpec[2];
419? Interop.Sys.FUTimens(handle, buf)
420: Interop.Sys.UTimensat(path!, buf);
431bool updateCreationTime = checkCreationTime && (_fileCache.Flags & Interop.Sys.FileStatusFlags.HasBirthTime) != 0 &&
486int rv = handle is not null ? Interop.Sys.FChMod(handle, (int)mode)
487: Interop.Sys.ChMod(path!, (int)mode);
506Interop.Sys.FStat(handle, out _fileCache) :
507Interop.Sys.LStat(Path.TrimEndingDirectorySeparator(path), out _fileCache);
511Interop.ErrorInfo errorInfo = Interop.Sys.GetLastErrorInfo();
531int fileType = _fileCache.Mode & Interop.Sys.FileTypes.S_IFMT;
532bool isDirectory = fileType == Interop.Sys.FileTypes.S_IFDIR;
534if (fileType == Interop.Sys.FileTypes.S_IFLNK)
536if (Interop.Sys.Stat(path, out Interop.Sys.FileStatus target) == 0)
538isDirectory = (target.Mode & Interop.Sys.FileTypes.S_IFMT) == Interop.Sys.FileTypes.S_IFDIR;
541_fileCache.Mode = Interop.Sys.FileTypes.S_IFLNK | (target.Mode & (int)FileSystem.ValidUnixFileModes);
src\libraries\System.Private.CoreLib\src\System\IO\FileSystem.Unix.cs (46)
51Interop.CheckIo(Interop.Sys.CopyFile(src, dst, fileLength));
54private static Exception? CreateOpenExceptionForCopyFile(Interop.ErrorInfo error, Interop.Sys.OpenFlags flags, string path)
77if (Interop.Sys.Link(sourceFullPath, destFullPath) >= 0)
87Interop.ErrorInfo errorInfo = Interop.Sys.GetLastErrorInfo();
126Interop.CheckIo(Interop.Sys.LStat(sourceFullPath, out Interop.Sys.FileStatus sourceStat), sourceFullPath);
129if ((sourceStat.Mode & Interop.Sys.FileTypes.S_IFMT) == Interop.Sys.FileTypes.S_IFDIR)
134Interop.Sys.FileStatus destStat;
135if (Interop.Sys.LStat(destFullPath, out destStat) == 0)
138if ((destStat.Mode & Interop.Sys.FileTypes.S_IFMT) == Interop.Sys.FileTypes.S_IFDIR)
154if (Interop.Sys.Unlink(destBackupFullPath) != 0)
156Interop.ErrorInfo errno = Interop.Sys.GetLastErrorInfo();
170if (Interop.Sys.Stat(destFullPath, out _) != 0)
172Interop.ErrorInfo errno = Interop.Sys.GetLastErrorInfo();
181Interop.CheckIo(Interop.Sys.Rename(sourceFullPath, destFullPath));
195if (Interop.Sys.Rename(sourceFullPath, destFullPath) < 0)
197Interop.ErrorInfo errorInfo = Interop.Sys.GetLastErrorInfo();
224Interop.Sys.FileStatus sourceStat, destStat;
225if (Interop.Sys.LStat(sourceFullPath, out sourceStat) == 0 && // source file exists
226(Interop.Sys.LStat(destFullPath, out destStat) != 0 || // dest file does not exist
229Interop.Sys.Rename(sourceFullPath, destFullPath) == 0) // try the rename
241if (Interop.Sys.Unlink(fullPath) < 0)
243Interop.ErrorInfo errorInfo = Interop.Sys.GetLastErrorInfo();
296int result = Interop.Sys.MkDir(path, (int)unixCreateMode);
302Interop.ErrorInfo errorInfo = Interop.Sys.GetLastErrorInfo();
340int result = Interop.Sys.MkDir(mkdirPath, (int)DefaultUnixCreateDirectoryMode);
346Interop.ErrorInfo errorInfo = Interop.Sys.GetLastErrorInfo();
373int result = Interop.Sys.MkDir(mkdirPath, (int)mode);
376Interop.ErrorInfo errorInfo = Interop.Sys.GetLastErrorInfo();
414if (!isCaseSensitiveRename && Interop.Sys.LStat(destNoDirectorySeparator, out Interop.Sys.FileStatus destFileStatus) >= 0)
419if (Interop.Sys.LStat(srcNoDirectorySeparator, out Interop.Sys.FileStatus sourceFileStatus) < 0)
438else if ((sourceFileStatus.Mode & Interop.Sys.FileTypes.S_IFMT) != Interop.Sys.FileTypes.S_IFDIR
449if (Interop.Sys.Rename(sourceFullPath, destNoDirectorySeparator) < 0)
451Interop.ErrorInfo errorInfo = Interop.Sys.GetLastErrorInfo();
527if (Interop.Sys.RmDir(fullPath) < 0)
529Interop.ErrorInfo errorInfo = Interop.Sys.GetLastErrorInfo();
662internal static string? GetLinkTarget(ReadOnlySpan<char> linkPath, bool isDirectory) => Interop.Sys.ReadLink(linkPath);
666Interop.CheckIo(Interop.Sys.SymLink(pathToTarget, path), path);
675string? linkTarget = Interop.Sys.ReadLink(linkPath);
679Interop.Error error = Interop.Sys.GetLastError();
708current = Interop.Sys.ReadLink(sb.AsSpan());
src\libraries\System.Private.CoreLib\src\System\IO\RandomAccess.Unix.cs (24)
23FileStreamHelpers.CheckFileCall(Interop.Sys.FTruncate(handle, length), handle.Path);
36result = Interop.Sys.PRead(handle, bufPtr, buffer.Length, fileOffset);
41Interop.ErrorInfo errorInfo = Interop.Sys.GetLastErrorInfo();
47result = Interop.Sys.Read(handle, bufPtr, buffer.Length);
53result = Interop.Sys.Read(handle, bufPtr, buffer.Length);
64Span<Interop.Sys.IOVector> vectors = buffers.Count <= IovStackThreshold ? stackalloc Interop.Sys.IOVector[IovStackThreshold] : new Interop.Sys.IOVector[buffers.Count];
74vectors[i] = new Interop.Sys.IOVector { Base = (byte*)memoryHandle.Pointer, Count = (UIntPtr)buffer.Length };
78fixed (Interop.Sys.IOVector* pinnedVectors = &MemoryMarshal.GetReference(vectors))
80result = Interop.Sys.PReadV(handle, pinnedVectors, buffers.Count, fileOffset);
113bytesWritten = Interop.Sys.PWrite(handle, bufPtr, bytesToWrite, fileOffset);
118Interop.ErrorInfo errorInfo = Interop.Sys.GetLastErrorInfo();
124bytesWritten = Interop.Sys.Write(handle, bufPtr, bytesToWrite);
130bytesWritten = Interop.Sys.Write(handle, bufPtr, bytesToWrite);
170Span<Interop.Sys.IOVector> vectors = buffersCount <= IovStackThreshold ?
171stackalloc Interop.Sys.IOVector[IovStackThreshold].Slice(0, buffersCount) :
172new Interop.Sys.IOVector[buffersCount];
183vectors[i] = new Interop.Sys.IOVector { Base = (byte*)memoryHandle.Pointer, Count = (UIntPtr)buffer.Length };
191Span<Interop.Sys.IOVector> left = vectors.Slice(buffersOffset);
192fixed (Interop.Sys.IOVector* pinnedVectors = &MemoryMarshal.GetReference(left))
194bytesWritten = Interop.Sys.PWriteV(handle, pinnedVectors, left.Length, fileOffset);
223Interop.Sys.IOVector current = vectors[buffersOffset];
224vectors[buffersOffset] = new Interop.Sys.IOVector
src\libraries\System.Private.CoreLib\src\System\IO\Strategies\FileStreamHelpers.Unix.cs (10)
23Interop.ErrorInfo errorInfo = Interop.Sys.GetLastErrorInfo();
34CheckFileCall(Interop.Sys.LSeek(handle, offset, (Interop.Sys.SeekWhence)(int)origin), handle.Path); // SeekOrigin values are the same as Interop.libc.SeekWhence values
42if (Interop.Sys.FSync(handle) < 0)
44Interop.ErrorInfo errorInfo = Interop.Sys.GetLastErrorInfo();
66CheckFileCall(Interop.Sys.LockFileRegion(handle, position, length, canWrite ? Interop.Sys.LockType.F_WRLCK : Interop.Sys.LockType.F_RDLCK), handle.Path);
76CheckFileCall(Interop.Sys.LockFileRegion(handle, position, length, Interop.Sys.LockType.F_UNLCK), handle.Path);