in csharp/NativeUtils/FileStream.cs [289:333]
internal FileStreamImpl(string path, FileMode mode, FileAccess access, FileShare share, int bufferSize, FileOptions options)
{
_deleteOnClose = options.HasFlag(FileOptions.DeleteOnClose);
_fileName = path; //_deleteOnClose ? path : null;
_fd = -1;
if (options.HasFlag((FileOptions.Asynchronous | FileOptions.Encrypted)))
throw new NotImplementedException($"options: {options}");
int flags = 0;
if ((int)mode < 1 || (int)mode > 6)
throw new ArgumentException($"mode: {mode}?");
if ((int)access < 1 || (int)access > 3)
throw new ArgumentException($"access: {access}?");
if (mode == FileMode.Append && access != FileAccess.Write)
throw new ArgumentException($"mode: Append, access: {access}?");
// Arrays treated as 1-based
flags = ModeFlags[(int)mode - 1] | AccessFlags[(int)access - 1];
// We never modify our file on open, truncation is done separately, in order to not damage the file if it turns out to be locked
int fd = Check("open", open(path, flags & ~O_TRUNC, Convert.ToInt32("777", 8)));
try
{
LockFd(fd, share, path);
// If the lock is taken and the file is opened, try to truncate, if necessary
if (0 != (flags & O_TRUNC))
{
Check("lseek", lseek(fd, 0, 0));
Check("ftruncate", ftruncate(fd, 0));
}
_fd = fd;
}
catch
{
// On error, close file/lock and rethrow
close(fd);
GC.SuppressFinalize(this);
throw;
}
}