Esempio n. 1
0
        internal void DeleteDirectory(CloudFileDirectory dir, bool recurse)
        {
            if (dir.Share.GetRootDirectoryReference().Uri == dir.Uri)
            {
                dir.Share.Delete();
                return;
            }

            var items = dir.ListFilesAndDirectories();

            if (recurse)
            {
                HandleItems(items,
                            (f) => f.Delete(),
                            (d) => DeleteDirectory(d, recurse),
                            (s) => s.Delete());

                dir.Delete();
            }
            else
            {
                if (items.Count() == 0)
                {
                    dir.Delete();
                }
                else
                {
                    throw new Exception("The directory is not empty. Please specify -recurse to delete it.");
                }
            }
        }
Esempio n. 2
0
        public void DownloadFileFromShareSnapshot_dir()
        {
            string shareName = CloudFileUtil.GenerateUniqueFileShareName();
            string dirName   = CloudFileUtil.GenerateUniqueDirectoryName();
            string fileName  = CloudFileUtil.GenerateUniqueFileName();

            try
            {
                CloudFileShare     share          = fileUtil.EnsureFileShareExists(shareName);
                CloudFileShare     shareSnapshot1 = share.Snapshot();
                CloudFileDirectory dir            = fileUtil.EnsureDirectoryExists(share, dirName);
                CloudFile          file           = fileUtil.CreateFile(dir, fileName);
                CloudFileShare     shareSnapshot2 = share.Snapshot();
                file.Delete();
                dir.Delete();

                //Get File content
                string StorageConnectionString = Test.Data.Get("StorageConnectionString");
                Test.Assert((CommandAgent as PowerShellAgent).InvokePSScript(string.Format(",(New-AzureStorageContext -ConnectionString \"{5}\" | Get-AzureStorageShare -Name {0} -SnapshotTime \"{1}\").GetRootDirectoryReference().GetDirectoryReference(\"{4}\") | Get-AzureStorageFileContent -Path {2} -Destination {3} -Force",
                                                                                           shareName,
                                                                                           shareSnapshot2.SnapshotTime.Value,
                                                                                           fileName,
                                                                                           fileName,
                                                                                           dirName,
                                                                                           StorageConnectionString)),
                            string.Format("Download File {0} from share snapshot {1}, {2} should success.", dirName + "\\" + fileName, shareName, shareSnapshot2.SnapshotTime.Value));

                //validate MD5
                CloudFile file2 = shareSnapshot2.GetRootDirectoryReference().GetDirectoryReference(dirName).GetFileReference(fileName);
                file2.FetchAttributes();
                Test.Assert(file2.Properties.ContentMD5 == FileUtil.GetFileContentMD5(fileName), "Expected MD5: {0}, real MD5: {1}", file2.Properties.ContentMD5, FileUtil.GetFileContentMD5(fileName));
            }
            finally
            {
                try
                {
                    fileUtil.DeleteFileShareIfExists(shareName);
                }
                catch (Exception e)
                {
                    Test.Warn("Unexpected exception when cleanup file share {0}: {1}", shareName, e);
                }
            }
        }
Esempio n. 3
0
 public void CleanupDirectory(CloudFileDirectory directory)
 {
     foreach (var item in directory.ListFilesAndDirectories())
     {
         CloudFile file = item as CloudFile;
         if (file != null)
         {
             file.Delete();
         }
         else
         {
             CloudFileDirectory dir = item as CloudFileDirectory;
             if (dir != null)
             {
                 this.CleanupDirectory(dir);
                 dir.Delete();
             }
         }
     }
 }
        private void Delete(CloudFileDirectory directory)
        {
            var items       = directory.ListFilesAndDirectories();
            var files       = items.OfType <CloudFile>().ToList();
            var directories = items.OfType <CloudFileDirectory>().ToList();

            foreach (var cloudFile in files)
            {
                _logger.WriteLine("Deleting file {0}", cloudFile.Name);
                cloudFile.Delete();
            }
            foreach (var dir in directories)
            {
                Delete(dir);
            }
            _logger.WriteLine("Deleting directory {0}", directory.Name);
            if (directory.Parent != null)
            {
                directory.Delete();
            }
        }
Esempio n. 5
0
        /// <summary>
        /// Deletes the expired files in the specified directory. CloudFileDirectories found in the current directory will
        /// call DeleteExpiredFiles recursively to get any files nested inside of one or more CloudFileDirectories.
        /// </summary>
        /// <param name="daysToRetain">The days to retain.</param>
        /// <param name="currentDirectory">The current directory.</param>
        public static void DeleteExpiredFiles(int daysToRetain, CloudFileDirectory currentDirectory = null)
        {
            bool isRootDirectory = false;

            if (currentDirectory == null)
            {
                // GetCloudDirectory without a parameter returns the root directory.
                currentDirectory = GetCloudDirectory();
                isRootDirectory  = true;
            }

            var azureItems = currentDirectory.ListFilesAndDirectories();

            foreach (var azureItem in azureItems)
            {
                if (azureItem.GetType() == typeof(CloudFileDirectory))
                {
                    var azureDirectory = (CloudFileDirectory)azureItem;
                    DeleteExpiredFiles(daysToRetain, azureDirectory);
                }
                else if (azureItem.GetType() == typeof(CloudFile))
                {
                    var azureFile = (CloudFile)azureItem;
                    azureFile.FetchAttributes();
                    Console.WriteLine(azureFile.Properties);

                    var lastModified = azureFile.Properties.LastModified;

                    if (lastModified != null)
                    {
                        var compareResult = DateTimeOffset.Compare((DateTimeOffset)lastModified, DateTimeOffset.Now.AddDays(-(double)daysToRetain));

                        if (compareResult < 0)
                        {
                            // Remove from Avbox database
                            SqlHelper.TryRemoveAzureFileInfo(azureFile);

                            // Remove from Azure Storage.
                            azureFile.Delete();
                        }
                    }
                    else
                    {
                        throw new Exception("Cannot determine age of file. LastModified is null.");
                    }
                }
                else
                {
                    throw new Exception("Unknown file type found. " + azureItem.Uri);
                }
            }

            // If the current directory has no files left and is not the root directory, delete the directory as well.
            if (!isRootDirectory)
            {
                // Directory is not root directory.
                var isDirectoryEmpty = !currentDirectory.ListFilesAndDirectories().Any();

                if (isDirectoryEmpty)
                {
                    currentDirectory.Delete();
                }
            }
        }