src\libraries\System.Private.CoreLib\src\Microsoft\Win32\SafeHandles\SafeFileHandle.Unix.cs (69)
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();
141Interop.Sys.Unlink(_path); // ignore errors; it's valid that the path may no longer exist
151Interop.Sys.FLock(handle, Interop.Sys.LockOperations.LOCK_UN); // ignore any errors
158int result = Interop.Sys.Close(handle);
161t_lastCloseErrorInfo = Interop.Sys.GetLastErrorInfo();
185Func<Interop.ErrorInfo, Interop.Sys.OpenFlags, string, Exception?>? createOpenException = null)
191Func<Interop.ErrorInfo, Interop.Sys.OpenFlags, string, Exception?>? createOpenException = null)
198Func<Interop.ErrorInfo, Interop.Sys.OpenFlags, string, Exception?>? createOpenException = null)
201Interop.Sys.OpenFlags openFlags = PreOpenConfigurationFromOptions(mode, access, share, options, failForSymlink);
244private static Interop.Sys.OpenFlags PreOpenConfigurationFromOptions(FileMode mode, FileAccess access, FileShare share, FileOptions options, bool failForSymlink)
247Interop.Sys.OpenFlags flags = default;
250flags |= Interop.Sys.OpenFlags.O_NOFOLLOW;
262flags |= Interop.Sys.OpenFlags.O_TRUNC;
268flags |= Interop.Sys.OpenFlags.O_CREAT;
272flags |= Interop.Sys.OpenFlags.O_CREAT;
275flags |= Interop.Sys.OpenFlags.O_TRUNC;
280flags |= (Interop.Sys.OpenFlags.O_CREAT | Interop.Sys.OpenFlags.O_EXCL);
288flags |= Interop.Sys.OpenFlags.O_RDONLY;
292flags |= Interop.Sys.OpenFlags.O_RDWR;
296flags |= Interop.Sys.OpenFlags.O_WRONLY;
303flags |= Interop.Sys.OpenFlags.O_CLOEXEC;
315flags |= Interop.Sys.OpenFlags.O_SYNC;
324Interop.Sys.FileStatus status = default;
336if ((status.Mode & Interop.Sys.FileTypes.S_IFMT) == Interop.Sys.FileTypes.S_IFDIR)
341if ((status.Mode & Interop.Sys.FileTypes.S_IFMT) == Interop.Sys.FileTypes.S_IFREG)
347Debug.Assert(Interop.Sys.LSeek(this, 0, Interop.Sys.SeekWhence.SEEK_CUR) >= 0);
359Interop.Sys.LockOperations lockOperation = (share == FileShare.None) ? Interop.Sys.LockOperations.LOCK_EX : Interop.Sys.LockOperations.LOCK_SH;
360if (CanLockTheFile(lockOperation, access) && !(_isLocked = Interop.Sys.FLock(this, lockOperation | Interop.Sys.LockOperations.LOCK_NB) >= 0))
366Interop.ErrorInfo errorInfo = Interop.Sys.GetLastErrorInfo();
386Interop.Sys.FileStatus pathStatus;
387if (Interop.Sys.Stat(path, out pathStatus) < 0)
392Interop.ErrorInfo error = Interop.Sys.GetLastErrorInfo();
414Interop.Sys.FileAdvice fadv =
415(options & FileOptions.RandomAccess) != 0 ? Interop.Sys.FileAdvice.POSIX_FADV_RANDOM :
416(options & FileOptions.SequentialScan) != 0 ? Interop.Sys.FileAdvice.POSIX_FADV_SEQUENTIAL :
420FileStreamHelpers.CheckFileCall(Interop.Sys.PosixFAdvise(this, 0, 0, fadv), path,
428if (Interop.Sys.FTruncate(this, 0) < 0)
430Interop.ErrorInfo errorInfo = Interop.Sys.GetLastErrorInfo();
441if (preallocationSize > 0 && Interop.Sys.FAllocate(this, 0, preallocationSize) < 0)
443Interop.ErrorInfo errorInfo = Interop.Sys.GetLastErrorInfo();
453Interop.Sys.Unlink(path!);
465private bool CanLockTheFile(Interop.Sys.LockOperations lockOperation, FileAccess access)
467Debug.Assert(lockOperation == Interop.Sys.LockOperations.LOCK_EX || lockOperation == Interop.Sys.LockOperations.LOCK_SH);
473else if (lockOperation == Interop.Sys.LockOperations.LOCK_EX)
482if (!Interop.Sys.TryGetFileSystemType(this, out Interop.Sys.UnixFileSystemTypes unixFileSystemType))
489case Interop.Sys.UnixFileSystemTypes.nfs: // #44546
490case Interop.Sys.UnixFileSystemTypes.smb:
491case Interop.Sys.UnixFileSystemTypes.smb2: // #53182
492case Interop.Sys.UnixFileSystemTypes.cifs:
499private void FStatCheckIO(string path, ref Interop.Sys.FileStatus status, ref bool statusHasValue)
503if (Interop.Sys.FStat(this, out status) != 0)
505Interop.ErrorInfo error = Interop.Sys.GetLastErrorInfo();
521_canSeek = canSeek = Interop.Sys.LSeek(this, 0, Interop.Sys.SeekWhence.SEEK_CUR) >= 0 ? NullableBool.True : NullableBool.False;
529int 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);