Browse Source

NTFileSystemAdapter: Bugfix: FILE_OPEN_IF was not handled properly if the file exists

Tal Aloni 7 years ago
parent
commit
340f55c26e
1 changed files with 53 additions and 38 deletions
  1. 53 38
      SMBLibrary/NTFileStore/Adapter/NTFileSystemAdapter.cs

+ 53 - 38
SMBLibrary/NTFileStore/Adapter/NTFileSystemAdapter.cs

@@ -157,62 +157,77 @@ namespace SMBLibrary
                 else
                 {
                     fileStatus = FileStatus.FILE_EXISTS;
-                    if (!requestedWriteAccess)
-                    {
-                        return NTStatus.STATUS_ACCESS_DENIED;
-                    }
-
-                    if (createDisposition == CreateDisposition.FILE_OVERWRITE ||
-                        createDisposition == CreateDisposition.FILE_OVERWRITE_IF)
+                    if (createDisposition == CreateDisposition.FILE_OPEN_IF)
                     {
-                        // Truncate the file
-                        try
+                        if (entry.IsDirectory && forceFile)
                         {
-                            Stream temp = m_fileSystem.OpenFile(path, FileMode.Truncate, FileAccess.ReadWrite, FileShare.ReadWrite);
-                            temp.Close();
+                            return NTStatus.STATUS_FILE_IS_A_DIRECTORY;
                         }
-                        catch (Exception ex)
+
+                        if (!entry.IsDirectory && forceDirectory)
                         {
-                            NTStatus status = ToNTStatus(ex);
-                            Log(Severity.Verbose, "CreateFile: Error truncating '{0}'. {1}.", path, status);
-                            return status;
+                            return NTStatus.STATUS_OBJECT_PATH_INVALID;
                         }
-                        fileStatus = FileStatus.FILE_OVERWRITTEN;
                     }
-                    else if (createDisposition == CreateDisposition.FILE_SUPERSEDE)
+                    else
                     {
-                        // Delete the old file
-                        try
-                        {
-                            m_fileSystem.Delete(path);
-                        }
-                        catch(Exception ex)
+                        if (!requestedWriteAccess)
                         {
-                            NTStatus status = ToNTStatus(ex);
-                            Log(Severity.Verbose, "CreateFile: Error deleting '{0}'. {1}.", path, status);
-                            return status;
+                            return NTStatus.STATUS_ACCESS_DENIED;
                         }
 
-                        try
+                        if (createDisposition == CreateDisposition.FILE_OVERWRITE ||
+                            createDisposition == CreateDisposition.FILE_OVERWRITE_IF)
                         {
-                            if (forceDirectory)
+                            // Truncate the file
+                            try
                             {
-                                Log(Severity.Information, "CreateFile: Creating directory '{0}'", path);
-                                entry = m_fileSystem.CreateDirectory(path);
+                                Stream temp = m_fileSystem.OpenFile(path, FileMode.Truncate, FileAccess.ReadWrite, FileShare.ReadWrite);
+                                temp.Close();
                             }
-                            else
+                            catch (Exception ex)
                             {
-                                Log(Severity.Information, "CreateFile: Creating file '{0}'", path);
-                                entry = m_fileSystem.CreateFile(path);
+                                NTStatus status = ToNTStatus(ex);
+                                Log(Severity.Verbose, "CreateFile: Error truncating '{0}'. {1}.", path, status);
+                                return status;
                             }
+                            fileStatus = FileStatus.FILE_OVERWRITTEN;
                         }
-                        catch (Exception ex)
+                        else if (createDisposition == CreateDisposition.FILE_SUPERSEDE)
                         {
-                            NTStatus status = ToNTStatus(ex);
-                            Log(Severity.Verbose, "CreateFile: Error creating '{0}'. {1}.", path, status);
-                            return status;
+                            // Delete the old file
+                            try
+                            {
+                                m_fileSystem.Delete(path);
+                            }
+                            catch (Exception ex)
+                            {
+                                NTStatus status = ToNTStatus(ex);
+                                Log(Severity.Verbose, "CreateFile: Error deleting '{0}'. {1}.", path, status);
+                                return status;
+                            }
+
+                            try
+                            {
+                                if (forceDirectory)
+                                {
+                                    Log(Severity.Information, "CreateFile: Creating directory '{0}'", path);
+                                    entry = m_fileSystem.CreateDirectory(path);
+                                }
+                                else
+                                {
+                                    Log(Severity.Information, "CreateFile: Creating file '{0}'", path);
+                                    entry = m_fileSystem.CreateFile(path);
+                                }
+                            }
+                            catch (Exception ex)
+                            {
+                                NTStatus status = ToNTStatus(ex);
+                                Log(Severity.Verbose, "CreateFile: Error creating '{0}'. {1}.", path, status);
+                                return status;
+                            }
+                            fileStatus = FileStatus.FILE_SUPERSEDED;
                         }
-                        fileStatus = FileStatus.FILE_SUPERSEDED;
                     }
                 }
             }