private static void WriteToNewFileWithOwnerRWPermissionsUnix()

in CredentialProvider.Microsoft/Util/EncryptedFileWithPermissions.cs [101:122]


        private static void WriteToNewFileWithOwnerRWPermissionsUnix(string path, byte[] bytes)
        {
            int _0600 = Convert.ToInt32("600", 8);

            int fileDescriptor = PosixCreate(path, _0600);

            // if creat() fails, then try to use File.Create because it will throw a meaningful exception.
            if (fileDescriptor == -1)
            {
                int posixCreateError = Marshal.GetLastWin32Error();
                using (File.Create(path))
                {
                    // File.Create() should have thrown an exception with an appropriate error message
                }
                File.Delete(path);
                throw new InvalidOperationException($"libc creat() failed with last error code {posixCreateError}, but File.Create did not");
            }

            var safeFileHandle = new SafeFileHandle((IntPtr)fileDescriptor, ownsHandle: true);
            using var fileStream = new FileStream(safeFileHandle, FileAccess.ReadWrite);
            fileStream.Write(bytes, 0, bytes.Length);
        }