Fix for FileState.Create constraint violation in StatusAgent.cs
[pithos-ms-client] / trunk / Pithos.Core / Agents / Uploader.cs
index 8239341..ee6bfb2 100644 (file)
@@ -83,14 +83,28 @@ namespace Pithos.Core.Agents
                         {
 
                             var client = new CloudFilesClient(accountInfo);
+
                             //Even if GetObjectInfo times out, we can proceed with the upload            
                             var cloudInfo = client.GetObjectInfo(account, cloudFile.Container, cloudFile.Name);
 
-                            //If this is a read-only file, do not upload changes
-                            if (!cloudInfo.IsWritable(action.AccountInfo.UserName))
-                                return;
+                            //If this a shared file
+                            if (!cloudFile.Account.Equals(action.AccountInfo.UserName,StringComparison.InvariantCultureIgnoreCase))
+                            {
+                                //If this is a read-only file, do not upload changes
+                                if (!cloudInfo.IsWritable(action.AccountInfo.UserName))
+                                {
+                                    MakeFileReadOnly(fullFileName);
+                                    return;
+                                }
 
-                                //TODO: If the object does not exist, check that we can upload to the folder
+                                //If the file is new, can we upload it?
+                                if ( !cloudInfo.Exists && !client.CanUpload(account, cloudFile))
+                                {
+                                    MakeFileReadOnly(fullFileName);
+                                    return;
+                                }
+
+                            }
 
                             if (fileInfo is DirectoryInfo)
                             {
@@ -143,7 +157,7 @@ namespace Pithos.Core.Agents
                             if (response.StatusCode == HttpStatusCode.Forbidden)
                             {
                                 StatusKeeper.SetFileState(fileInfo.FullName, FileStatus.Forbidden, FileOverlayStatus.Conflict, "Forbidden");
-                                
+                                MakeFileReadOnly(fullFileName);
                             }
                             else
                                 //In any other case, propagate the error
@@ -177,6 +191,15 @@ namespace Pithos.Core.Agents
             }
         }
 
+        private static void MakeFileReadOnly(string fullFileName)
+        {
+            var attributes = File.GetAttributes(fullFileName);
+            //Do not make any modifications if not necessary
+            if (attributes.HasFlag(FileAttributes.ReadOnly))
+                return;
+            File.SetAttributes(fullFileName, attributes | FileAttributes.ReadOnly);
+        }
+
         private static AccountInfo GetSharerAccount(string relativePath, AccountInfo accountInfo)
         {
             var parts = relativePath.Split('\\');