// Get directory size from path, with or without sub directories
        public static long GetDirectorySize(DirectoryInfo directoryPath, bool IncludeSubDirectories)
        {
            try
            {
                /*
                 * dynamic fso = Activator.CreateInstance(Type.GetTypeFromProgID("Scripting.FileSystemObject"));
                 * var fldr = fso.GetFolder(directoryPath.FullName);
                 *
                 * return Convert.ToInt64((ulong)fldr.size);
                 */

                directoryPath.Refresh();

                if (!directoryPath.Exists || !new DriveInfo(Path.GetPathRoot(directoryPath.Root.FullName)).IsReady)
                {
                    return(0);
                }

                // Define a "long" for directory size
                long DirectorySize = 0;

                foreach (var CurrentFile in directoryPath.EnumerateFiles("*", (IncludeSubDirectories) ? SearchOption.AllDirectories : SearchOption.TopDirectoryOnly).Where(x => x is FileInfo))
                {
                    DirectorySize += CurrentFile.Length;
                }
                // and return directory size
                return(DirectorySize);
            }
            catch (DirectoryNotFoundException ex)
            {
                logger.Error(ex);
                return(0);
            }
            // on error, return 0
            catch (Exception ex)
            {
                logger.Fatal(ex);
                return(0);
            }
        }
Esempio n. 2
0
        private void BackupSubset(IDictionary <string, string> vmNamesMapSubset, Options options, ILogger logger)
        {
            var vssImpl = VssFactoryProvider.Default.GetVssFactory();

            using (var vss = vssImpl.CreateVssBackupComponents())
            {
                RaiseEvent(EventAction.InitializingVss, null, null);

                vss.InitializeForBackup(null);
                vss.SetBackupState(true, true, VssBackupType.Full, false);
                vss.SetContext(VssSnapshotContext.Backup);

                // Add Hyper-V writer
                var hyperVwriterGuid = new Guid("66841cd4-6ded-4f4b-8f17-fd23f8ddc3de");
                vss.EnableWriterClasses(new Guid[] { hyperVwriterGuid });

                vss.GatherWriterMetadata();

                IList <IVssWMComponent> components = new List <IVssWMComponent>();
                // key: volumePath, value: volumeName. These values are equivalent on a standard volume, but differ in the CSV case
                // StringComparer.InvariantCultureIgnoreCase requiered to fix duplicate Keys with different case error
                IDictionary <string, string> volumeMap =
                    new Dictionary <string, string>(StringComparer.InvariantCultureIgnoreCase);

                var wm = vss.WriterMetadata.FirstOrDefault(o => o.WriterId.Equals(hyperVwriterGuid));
                foreach (var component in wm.Components)
                {
                    if (vmNamesMapSubset.ContainsKey(component.ComponentName))
                    {
                        components.Add(component);
                        vss.AddComponent(wm.InstanceId, wm.WriterId, component.Type, component.LogicalPath,
                                         component.ComponentName);
                        foreach (var file in component.Files)
                        {
                            string volumeName;
                            string volumePath;

                            if (CSV.IsSupported() && CSV.IsPathOnSharedVolume(file.Path))
                            {
                                CSV.ClusterPrepareSharedVolumeForBackup(file.Path, out volumePath, out volumeName);
                            }
                            else
                            {
                                volumePath = Path.GetPathRoot(file.Path).ToUpper();
                                volumeName = volumePath;
                            }

                            if (!volumeMap.ContainsKey(volumePath))
                            {
                                volumeMap.Add(volumePath, volumeName);
                            }
                        }
                    }
                }

                if (components.Count > 0)
                {
                    var vssSet = vss.StartSnapshotSet();

                    // Key: volumeName, value: snapshotGuid
                    IDictionary <string, Guid> snapshots = new Dictionary <string, Guid>();

                    foreach (var volumeName in volumeMap.Values)
                    {
                        snapshots.Add(volumeName, vss.AddToSnapshotSet(volumeName, Guid.Empty));
                    }

                    vss.PrepareForBackup();

                    RaiseEvent(EventAction.StartingSnaphotSet, components, volumeMap);
                    vss.DoSnapshotSet();
                    RaiseEvent(EventAction.SnapshotSetDone, components, volumeMap);

                    // key: volumeName, value: snapshotVolumePath
                    IDictionary <string, string> snapshotVolumeMap = new Dictionary <string, string>();

                    foreach (var kv in snapshots)
                    {
                        snapshotVolumeMap.Add(kv.Key, vss.GetSnapshotProperties(kv.Value).SnapshotDeviceObject);
                    }

                    BackupFiles(components, volumeMap, snapshotVolumeMap, vmNamesMapSubset, options, logger);

                    foreach (var component in components)
                    {
                        vss.SetBackupSucceeded(wm.InstanceId, wm.WriterId, component.Type, component.LogicalPath,
                                               component.ComponentName, true);
                    }

                    vss.BackupComplete();

                    RaiseEvent(EventAction.DeletingSnapshotSet, components, volumeMap);
                    vss.DeleteSnapshotSet(vssSet, true);
                }
            }
        }
Esempio n. 3
0
 public override string GetPathRoot(string path)
 {
     return(AfsPath.GetPathRoot(path));
 }