Пример #1
0
        /*** FILESYSTEM OPERATION FUNCTIONS ***/

        private void DoCreateDir()
        {
            bool failIfExists = false;

            if (!string.IsNullOrEmpty(AdditionalArgs))
            {
                string[] args = AdditionalArgs.Split(new[] { ' ' }, StringSplitOptions.RemoveEmptyEntries);
                foreach (var arg in args)
                {
                    if (string.Equals(arg, "--failIfExists", StringComparison.OrdinalIgnoreCase))
                    {
                        failIfExists = true;
                    }
                }
            }

            if (FileUtilities.DirectoryExistsNoFollow(PathAsString) || FileUtilities.FileExistsNoFollow(PathAsString))
            {
                if (failIfExists)
                {
                    throw new InvalidOperationException($"Directory creation failed because '{PathAsString}' exists");
                }
            }

            if (OperatingSystemHelper.IsUnixOS)
            {
                Directory.CreateDirectory(PathAsString);
            }
            else
            {
                // .NET's Directory.CreateDirectory first checks for directory existence, so when a directory
                // exists it does nothing; to test a specific Detours access policy, we want to issue a "create directory"
                // operation regardless of whether the directory exists, hence p-invoking Windows native CreateDirectory.
                if (!ExternWinCreateDirectory(PathAsString, IntPtr.Zero))
                {
                    int errorCode = Marshal.GetLastWin32Error();

                    // If we got ERROR_ALREADY_EXISTS (183), keep it quiet --- yes we did not 'write' but the directory
                    // is on the disk after this method returns --- and do not fail the operation.
                    if (errorCode == ERROR_ALREADY_EXISTS)
                    {
                        return;
                    }

                    throw new InvalidOperationException($"Directory creation (native) for path '{PathAsString}' failed with error {errorCode}.");
                }
            }
        }