Convert ActiveRecord update code to direct ADO calls to reduce locks
[pithos-ms-client] / trunk / Pithos.Interfaces / FileInfoExtensions.cs
index 56e0ca2..a4c432e 100644 (file)
@@ -23,7 +23,7 @@ namespace Pithos.Interfaces
                 path=path.ToLower() + "\\";
             int pathLength = path.Length;            
             
-            var filePath = fileInfo.FullName;
+            var filePath = fileInfo.GetProperCapitalization();
             
             if (!filePath.StartsWith(path,StringComparison.InvariantCultureIgnoreCase))
                 throw new ArgumentException(String.Format("The path {0} doesn't contain the file {1}",path,filePath));
@@ -53,6 +53,133 @@ namespace Pithos.Interfaces
         }
 
 
+        public static string GetProperDirectoryCapitalization(string fileName)
+        {
+            if (String.IsNullOrWhiteSpace(fileName))
+                throw new ArgumentNullException("fileName");
+            if (!Path.IsPathRooted(fileName))
+                throw new ArgumentException("fileName must be an absolute path", "fileName");
+            Contract.EndContractBlock();
+
+            var dirInfo = new DirectoryInfo(fileName);
+            return dirInfo.GetProperCapitalization();
+        }
+
+        public static string GetProperCapitalization(this DirectoryInfo dirInfo)
+        {
+            if (dirInfo == null)
+                throw new ArgumentNullException("dirInfo");
+            Contract.EndContractBlock();
+
+            var parentDirInfo = dirInfo.Parent;
+            if (null == parentDirInfo)
+                return dirInfo.Name;
+
+            try
+            {
+
+
+                if (dirInfo.Exists)
+                    return Path.Combine(GetProperDirectoryCapitalization(parentDirInfo.FullName),
+                                        parentDirInfo.GetDirectories(dirInfo.Name)[0].Name);
+                else
+                {
+                    return dirInfo.FullName;
+                }
+            }
+            catch (DirectoryNotFoundException)
+            {
+                //An exception can occur if a directory is deleted right after the Exists call
+                return dirInfo.FullName;
+            }
+        }
+
+
+        public static string GetProperFilePathCapitalization(string fileName)
+        {
+            if (String.IsNullOrWhiteSpace(fileName))
+                throw new ArgumentNullException("fileName");
+            if (!Path.IsPathRooted(fileName))
+                throw new ArgumentException("fileName must be an absolute path", "fileName");
+            Contract.EndContractBlock();
+
+
+            var fileInfo = new FileInfo(fileName);
+            return fileInfo.GetProperCapitalization();
+        }
+
+        public static string GetProperCapitalization(this FileInfo fileInfo)
+        {
+            if (fileInfo == null)
+                throw new ArgumentNullException("fileInfo");
+            Contract.EndContractBlock();
+
+
+            var dirInfo = fileInfo.Directory;
 
+            //Directory will not be null for an absolute path
+            Contract.Assume(dirInfo != null);
+
+            try
+            {
+
+                if (fileInfo.Exists)
+                    return Path.Combine(GetProperDirectoryCapitalization(dirInfo.FullName),
+                                        dirInfo.GetFiles(fileInfo.Name)[0].Name);
+                else
+                {
+                    return fileInfo.FullName;
+                }
+            }
+            catch (FileNotFoundException)
+            {
+                //An exception can occur if a file is deleted right after the Exists call
+                return fileInfo.FullName;
+
+            }
+        }
+
+        public static string GetProperCapitalization(this FileSystemInfo info)
+        {
+            if (info is FileInfo)
+                return (info as FileInfo).GetProperCapitalization();
+            if (info is DirectoryInfo)
+                return (info as DirectoryInfo).GetProperCapitalization();
+            throw new NotSupportedException("Unexpected parameter type");
+        }
+
+        public static DirectoryInfo WithProperCapitalization(this DirectoryInfo dirInfo)
+        {
+            if (dirInfo==null)
+                throw new ArgumentNullException("dirInfo");
+            Contract.EndContractBlock();
+
+            var path = dirInfo.GetProperCapitalization();
+            return new DirectoryInfo(path);
+        }
+
+        public static FileInfo WithProperCapitalization(this FileInfo fileInfo)
+        {
+            if (fileInfo==null)
+                throw new ArgumentNullException("fileInfo");
+            Contract.EndContractBlock();
+
+            var path = fileInfo.GetProperCapitalization();
+            return new FileInfo(path);
+        }
+
+        public static FileSystemInfo WithProperCapitalization(this FileSystemInfo info)
+        {
+            if (info==null)
+                throw new ArgumentNullException("info");
+            Contract.EndContractBlock();
+
+            if (info is FileInfo)
+                return (info as FileInfo).WithProperCapitalization();
+            if (info is DirectoryInfo)
+                return (info as DirectoryInfo).WithProperCapitalization();
+
+            throw new NotSupportedException("Unexpected parameter type");
+        }
     }
 }