Пример #1
0
		private const int DefaultBufferSize = 16 * 1024; // 16KB

		public static async Task Copy(string sourceFilePath, string destinationFilePath, OverwriteMode overwriteMode = OverwriteMode.AlwaysOverwrite, CopyOptions options = CopyOptions.AllowHardLinkCreation, CancellationToken? cancellationToken = null, Action<long, long> progressCallback = null)
		{
			if (string.IsNullOrEmpty(sourceFilePath)) throw new ArgumentNullException(nameof(sourceFilePath));
			if (string.IsNullOrEmpty(destinationFilePath)) throw new ArgumentNullException(nameof(destinationFilePath));

			var ct = cancellationToken ?? CancellationToken.None;
			Directory.CreateDirectory(Path.GetDirectoryName(destinationFilePath));

			if (options.HasFlag(CopyOptions.AllowHardLinkCreation))
			{
				if (sourceFilePath.Length > 3 && destinationFilePath.Length > 3
					&& sourceFilePath[1] == Path.VolumeSeparatorChar && sourceFilePath[2] == Path.PathSeparator
					&& sourceFilePath.Take(3).SequenceEqual(destinationFilePath.Take(3)))
				{
					if (NtfsHelper.CreateHardLink(sourceFilePath, destinationFilePath))
					{
						if (progressCallback != null)
						{
							var length = new FileInfo(sourceFilePath).Length;
							progressCallback(length, length);
						}

						return;
					}
				}
			}

			await Win32CopyEx.Copy(sourceFilePath, destinationFilePath, overwriteMode, options, ct, progressCallback).ConfigureAwait(false);

			ct.ThrowIfCancellationRequested();
		}
        public static void Run()
        {
            // ExStart:1
            // The path to the documents directory.
            string dataDir = RunExamples.GetDataDir(System.Reflection.MethodBase.GetCurrentMethod().DeclaringType);

            // Load sample excel file
            Workbook wb = new Workbook(dataDir + "sample.xlsx");

            // Access the first sheet which contains chart
            Worksheet source = wb.Worksheets[0];

            // Add another sheet named DestSheet
            Worksheet destination = wb.Worksheets.Add("DestSheet");

            // Set CopyOptions.ReferToDestinationSheet to true
            CopyOptions options = new CopyOptions();
            options.ReferToDestinationSheet = true;

            // Copy all the rows of source worksheet to destination worksheet which includes chart as well
            // The chart data source will now refer to DestSheet
            destination.Cells.CopyRows(source.Cells, 0, 0, source.Cells.MaxDisplayRange.RowCount, options);

            // Save workbook in xlsx format
            wb.Save(dataDir + "output_out.xlsx", SaveFormat.Xlsx);
            // ExEnd:1           
            
        }
Пример #3
0
		public static Task Copy(string source, string destination, OverwriteMode overwriteMode, CopyOptions options, CancellationToken? cancellationToken = null, Action<long, long> progressCallback = null)
		{
			if (string.IsNullOrEmpty(source)) throw new ArgumentNullException(nameof(source));
			if (string.IsNullOrEmpty(destination)) throw new ArgumentNullException(nameof(destination));

			var copyFileFlags = CopyFileFlags.COPY_FILE_RESTARTABLE;

			if (overwriteMode != OverwriteMode.AlwaysOverwrite)
				copyFileFlags |= CopyFileFlags.COPY_FILE_FAIL_IF_EXISTS;

			if (options.HasFlag(CopyOptions.DisableBuffering))
				copyFileFlags |= CopyFileFlags.COPY_FILE_NO_BUFFERING;

			int isCancelled = 0;
			var ct = cancellationToken ?? CancellationToken.None;

			CopyProgressRoutine progressRoutine =
				(total, transferred, streamSize, streamByteTrans, dwStreamNumber, reason, hSourceFile, hDestinationFile, lpData) =>
				{
					if (progressCallback != null && reason == CopyProgressCallbackReason.CALLBACK_CHUNK_FINISHED)
						progressCallback(transferred, total);

					return ct.IsCancellationRequested ? CopyProgressResult.PROGRESS_CANCEL : CopyProgressResult.PROGRESS_CONTINUE;
				};

			return Task.Run(
				() =>
				{
					if (!CopyFileEx(source, destination, progressRoutine, IntPtr.Zero, ref isCancelled, copyFileFlags))
					{
						int errorCode = Marshal.GetLastWin32Error();

						if (errorCode == (int) Win32ErrorCode.ERROR_FILE_EXISTS || errorCode == (int) Win32ErrorCode.ERROR_ALREADY_EXISTS || errorCode == (int) Win32ErrorCode.ERROR_OBJECT_ALREADY_EXISTS || errorCode == (int) Win32ErrorCode.ERROR_OBJECT_NAME_EXISTS)
						{
							if (overwriteMode == OverwriteMode.OverwriteIfDifferent)
							{
								if (IOHelper.AreSameFile(source, destination))
									return;
								else
								{
									copyFileFlags &= ~CopyFileFlags.COPY_FILE_FAIL_IF_EXISTS;
									if (CopyFileEx(source, destination, progressRoutine, IntPtr.Zero, ref isCancelled, copyFileFlags))
										return;
								}
							}
						}

						throw new Win32Exception(errorCode);
					}
				});
		}
Пример #4
0
        /// <summary>
        ///     Copies a directory to another location
        /// </summary>
        /// <param name="source"> Source directory </param>
        /// <param name="destination"> Destination directory </param>
        /// <param name="recursive"> Should the copy be recursive </param>
        /// <param name="options"> Options used in copying </param>
        /// <returns> The DirectoryInfo for the destination info </returns>
        public static DirectoryInfo CopyTo(this DirectoryInfo source, string destination, bool recursive = true,
			CopyOptions options = CopyOptions.CopyAlways)
        {
            if (source == null)
                throw new ArgumentNullException("source");

            if (!source.Exists)
                throw new DirectoryNotFoundException("Source directory " + source.FullName + " not found.");

            if (destination == null)
                throw new ArgumentNullException("destination");

            var destinationInfo = new DirectoryInfo(destination);
            destinationInfo.Create();
            foreach (var tempFile in source.EnumerateFiles())
            {
                if (options == CopyOptions.CopyAlways)
                {
                    tempFile.CopyTo(Path.Combine(destinationInfo.FullName, tempFile.Name), true);
                }
                else if (options == CopyOptions.CopyIfNewer)
                {
                    if (File.Exists(Path.Combine(destinationInfo.FullName, tempFile.Name)))
                    {
                        var FileInfo = new FileInfo(Path.Combine(destinationInfo.FullName, tempFile.Name));
                        if (FileInfo.LastWriteTime.CompareTo(tempFile.LastWriteTime) < 0)
                            tempFile.CopyTo(Path.Combine(destinationInfo.FullName, tempFile.Name), true);
                    }
                    else
                    {
                        tempFile.CopyTo(Path.Combine(destinationInfo.FullName, tempFile.Name), true);
                    }
                }
                else if (options == CopyOptions.DoNotOverwrite)
                {
                    tempFile.CopyTo(Path.Combine(destinationInfo.FullName, tempFile.Name), false);
                }
            }
            if (recursive)
            {
                foreach (var subDirectory in source.EnumerateDirectories())
                    subDirectory.CopyTo(Path.Combine(destinationInfo.FullName, subDirectory.Name), true, options);
            }
            return new DirectoryInfo(destination);
        }
 /// <summary>
 /// Copies a directory to another location
 /// </summary>
 /// <param name="Source">Source directory</param>
 /// <param name="Destination">Destination directory</param>
 /// <param name="Recursive">Should the copy be recursive</param>
 /// <param name="Options">Options used in copying</param>
 /// <returns>The DirectoryInfo for the destination info</returns>
 public static DirectoryInfo CopyTo(this DirectoryInfo Source, string Destination, bool Recursive = true, CopyOptions Options = CopyOptions.CopyAlways)
 {
     if (Source == null)
         throw new ArgumentNullException("Source");
     if (!Source.Exists)
         throw new DirectoryNotFoundException("Source directory " + Source.FullName + " not found.");
     if (string.IsNullOrEmpty(Destination))
         throw new ArgumentNullException("Destination");
     DirectoryInfo DestinationInfo = new DirectoryInfo(Destination);
     DestinationInfo.Create();
     foreach (FileInfo TempFile in Source.EnumerateFiles())
     {
         if (Options == CopyOptions.CopyAlways)
         {
             TempFile.CopyTo(Path.Combine(DestinationInfo.FullName, TempFile.Name), true);
         }
         else if (Options == CopyOptions.CopyIfNewer)
         {
             if (File.Exists(Path.Combine(DestinationInfo.FullName, TempFile.Name)))
             {
                 FileInfo FileInfo = new FileInfo(Path.Combine(DestinationInfo.FullName, TempFile.Name));
                 if (FileInfo.LastWriteTime.CompareTo(TempFile.LastWriteTime) < 0)
                     TempFile.CopyTo(Path.Combine(DestinationInfo.FullName, TempFile.Name), true);
             }
             else
             {
                 TempFile.CopyTo(Path.Combine(DestinationInfo.FullName, TempFile.Name), true);
             }
         }
         else if (Options == CopyOptions.DoNotOverwrite)
         {
             TempFile.CopyTo(Path.Combine(DestinationInfo.FullName, TempFile.Name), false);
         }
     }
     if (Recursive)
     {
         foreach (DirectoryInfo SubDirectory in Source.EnumerateDirectories())
             SubDirectory.CopyTo(Path.Combine(DestinationInfo.FullName, SubDirectory.Name), Recursive, Options);
     }
     return new DirectoryInfo(Destination);
 }
Пример #6
0
        //private static ILog _log = LogContext.GetLogger("RexToy.Copy");
        public static void ShallowCopy(this object src, object dest, CopyOptions option = CopyOptions.BaseOnBoth, bool throwOnNotExist = true)
        {
            src.ThrowIfNullArgument(nameof(src));
            dest.ThrowIfNullArgument(nameof(dest));

            option.ThrowIfEnumOutOfRange();
            switch (option)
            {
                case CopyOptions.BaseOnBoth:
                    CopyByBoth(src, dest);
                    break;

                case CopyOptions.BaseOnSource:
                    CopyBySource(src, dest, throwOnNotExist);
                    break;

                case CopyOptions.BaseOnDest:
                    CopyByDest(src, dest, throwOnNotExist);
                    break;
            }
        }
        public static OperationIncludeAttributes ToData(CdmOperationIncludeAttributes instance, ResolveOptions resOpt, CopyOptions options)
        {
            if (instance == null)
            {
                return(null);
            }

            return(new OperationIncludeAttributes
            {
                Type = OperationTypeConvertor.OperationTypeToString(CdmOperationType.IncludeAttributes),
                Explanation = instance.Explanation,
                IncludeAttributes = instance.IncludeAttributes,
            });
        }
Пример #8
0
 public override dynamic CopyData(ResolveOptions resOpt, CopyOptions options)
 {
     return(CdmObjectBase.CopyData <CdmTraitDefinition>(this, resOpt, options));
 }
Пример #9
0
        public static async Task <LocalEntity> ToData(CdmEntityDefinition instance, ResolveOptions resOpt, CopyOptions options, CdmCorpusContext ctx)
        {
            var result = new LocalEntity
            {
                Name        = instance.EntityName,
                Description = instance.GetProperty("description"),
                Type        = "LocalEntity"
            };

            Utils.ProcessTraitsAndAnnotationsToData(instance.Ctx, result, instance.ExhibitsTraits);

            if (instance.Attributes != null)
            {
                result.Attributes = new List <Attribute>();
                foreach (CdmAttributeItem element in instance.Attributes)
                {
                    if (element.ObjectType != CdmObjectType.TypeAttributeDef)
                    {
                        Logger.Error(nameof(EntityPersistence), (ResolveContext)ctx, "Saving a manifest, with an entity containing an entity attribute, to model.json format is currently not supported.");

                        return(null);
                    }

                    // TODO: handle when attribute is something else other than CdmTypeAttributeDefinition.
                    var attribute = await TypeAttributePersistence.ToData(element as CdmTypeAttributeDefinition, resOpt, options);

                    if (attribute != null)
                    {
                        result.Attributes.Add(attribute);
                    }
                    else
                    {
                        Logger.Error(nameof(EntityPersistence), (ResolveContext)ctx, "There was an error while trying to convert model.json attribute to cdm attribute.");

                        return(null);
                    }
                }
            }

            return(result);
        }
Пример #10
0
 public FileInfo CopyTo(string destinationPath, CopyOptions copyOptions, bool preserveDates, PathFormat pathFormat)
 {
    string destinationPathLp;
    CopyToMoveToInternal(destinationPath, preserveDates, copyOptions, null, null, null, out destinationPathLp, pathFormat);
    return new FileInfo(Transaction, destinationPathLp, PathFormat.LongFullPath);
 }
Пример #11
0
 public static CopyMoveResult Copy(string sourceFileName, string destinationFileName, CopyOptions copyOptions, bool preserveDates, CopyMoveProgressRoutine progressHandler, object userProgressData)
 {
    return CopyMoveInternal(false, null, sourceFileName, destinationFileName, preserveDates, copyOptions, null, progressHandler, userProgressData, PathFormat.RelativePath);
 }
Пример #12
0
 public static CopyMoveResult Copy(string sourcePath, string destinationPath, CopyOptions copyOptions, CopyMoveProgressRoutine progressHandler, object userProgressData)
 {
    return CopyMoveInternal(null, sourcePath, destinationPath, copyOptions, null, progressHandler, userProgressData, PathFormat.RelativePath);
 }
Пример #13
0
        public static async Task <LocalEntity> ToData(CdmEntityDefinition instance, ResolveOptions resOpt, CopyOptions options, CdmCorpusContext ctx)
        {
            var result = new LocalEntity
            {
                Name        = instance.EntityName,
                Description = instance.Description,
                Type        = "LocalEntity"
            };

            await Utils.ProcessAnnotationsToData(instance.Ctx, result, instance.ExhibitsTraits);

            if (instance.Attributes != null)
            {
                result.Attributes = new List <Attribute>();
                foreach (dynamic element in instance.Attributes)
                {
                    // TODO: handle when attribute is something else other than CdmTypeAttributeDefinition.
                    var attribute = await TypeAttributePersistence.ToData(element as CdmTypeAttributeDefinition, resOpt, options);

                    if (attribute != null)
                    {
                        result.Attributes.Add(attribute);
                    }
                    else
                    {
                        Logger.Error(nameof(EntityPersistence), (ResolveContext)ctx, "There was an error while trying to convert model.json attribute to cdm attribute.");

                        return(null);
                    }
                }
            }

            return(result);
        }
Пример #14
0
        public static OperationIncludeAttributes ToData(CdmOperationIncludeAttributes instance, ResolveOptions resOpt, CopyOptions options)
        {
            if (instance == null)
            {
                return(null);
            }

            OperationIncludeAttributes obj = OperationBasePersistence.ToData <OperationIncludeAttributes>(instance, resOpt, options);

            obj.IncludeAttributes = instance.IncludeAttributes;

            return(obj);
        }
Пример #15
0
        private static Dictionary <string, JToken> CreateProperties(CdmTypeAttributeDefinition instance, ResolveOptions resOpt, CopyOptions options)
        {
            var properties = new Dictionary <string, JToken>();

            var displayName            = instance.GetProperty("displayName");
            var sourceName             = instance.GetProperty("sourceName");
            var description            = instance.GetProperty("description");
            var isReadOnly             = instance.GetProperty("isReadOnly");
            var maximumLength          = instance.GetProperty("maximumLength");
            var maximumValue           = instance.GetProperty("maximumValue");
            var minimumValue           = instance.GetProperty("minimumValue");
            var sourceOrdering         = instance.GetProperty("sourceOrdering");
            var valueConstrainedToList = instance.GetProperty("valueConstrainedToList");
            var isPrimaryKey           = instance.GetProperty("isPrimaryKey");
            var defValue = instance.GetProperty("defaultValue");

            if (displayName != null)
            {
                properties["cdm:displayName"] = JToken.FromObject(displayName);
            }

            if (instance.Explanation != null)
            {
                properties["cdm:explanation"] = JToken.FromObject(instance.Explanation);
            }

            if (sourceName != null)
            {
                properties["cdm:sourceName"] = JToken.FromObject(sourceName);
            }

            if (description != null)
            {
                properties["cdm:description"] = JToken.FromObject(description);
            }

            if (instance.AppliedTraits != null && instance.AppliedTraits.Count > 0)
            {
                properties["cdm:traits"] = JToken.FromObject(CopyDataUtils.ListCopyData(resOpt, instance.AppliedTraits?
                                                                                        .Where(trait => trait is CdmTraitGroupReference || !(trait as CdmTraitReference).IsFromProperty), options),
                                                             new JsonSerializer {
                    NullValueHandling = NullValueHandling.Ignore, ContractResolver = new CamelCasePropertyNamesContractResolver()
                });
            }

            if (isReadOnly != null)
            {
                properties["cdm:isReadOnly"] = JToken.FromObject(isReadOnly);
            }

            if (maximumLength != null)
            {
                properties["cdm:maximumLength"] = JToken.FromObject(maximumLength);
            }

            if (maximumValue != null)
            {
                properties["cdm:maximumValue"] = JToken.FromObject(maximumValue);
            }

            if (minimumValue != null)
            {
                properties["cdm:minimumValue"] = JToken.FromObject(minimumValue);
            }

            if (sourceOrdering != null && sourceOrdering != 0)
            {
                properties["cdm:sourceOrdering"] = JToken.FromObject(sourceOrdering);
            }

            if (valueConstrainedToList != null)
            {
                properties["cdm:valueConstrainedToList"] = JToken.FromObject(valueConstrainedToList);
            }

            if (isPrimaryKey != null)
            {
                properties["cdm:isPrimaryKey"] = JToken.FromObject(isPrimaryKey);
            }

            if (defValue != null)
            {
                properties["cdm:defaultValue"] = JToken.FromObject(defValue);
            }

            return(properties);
        }
Пример #16
0
        public static DataColumn ToData(CdmTypeAttributeDefinition instance, CdmCorpusContext ctx, ResolveOptions resOpt, CopyOptions options)
        {
            var properties = CreateProperties(instance, resOpt, options);

            var originDataTypeName = new TypeInfo
            {
                Properties    = properties,
                IsComplexType = false,
                IsNullable    = instance.GetProperty("isNullable"),
                TypeFamily    = "cdm"
            };

            var t2pm          = new TraitToPropertyMap(instance);;
            var numericTraits = t2pm.FetchTraitReference("is.dataFormat.numeric.shaped");

            if (numericTraits != null)
            {
                foreach (var numericTraitsArg in numericTraits.Arguments)
                {
                    if (numericTraitsArg.Name == "precision")
                    {
                        originDataTypeName.Precision = (int)numericTraitsArg.Value;
                    }
                    if (numericTraitsArg.Name == "scale")
                    {
                        originDataTypeName.Scale = (int)numericTraitsArg.Value;
                    }
                }
            }

            var dataFormat = instance.GetProperty("dataFormat");

            originDataTypeName = Utils.CdmDataFormatToSymsDataType(dataFormat, originDataTypeName);
            if (originDataTypeName.TypeName == null)
            {
                Logger.Error(ctx, Tag, nameof(ToData), instance.AtCorpusPath, CdmLogCode.ErrPersistSymsUnknownDataFormat, instance.DisplayName);
                return(null);
            }

            return(new DataColumn
            {
                OriginDataTypeName = originDataTypeName,
                Name = instance.Name
            });
        }
        public static OperationCombineAttributes ToData(CdmOperationCombineAttributes instance, ResolveOptions resOpt, CopyOptions options)
        {
            if (instance == null)
            {
                return(null);
            }

            OperationCombineAttributes obj = OperationBasePersistence.ToData <OperationCombineAttributes>(instance, resOpt, options);

            obj.Select    = instance.Select;
            obj.MergeInto = Utils.JsonForm(instance.MergeInto, resOpt, options);

            return(obj);
        }
Пример #18
0
        /// <inheritdoc />
        public Task <PushFileResult> PushFileAsync(OperationContext context, ContentHash hash, Stream stream, MachineLocation targetMachine, CopyOptions options)
        {
            (string host, int port) = ExtractHostInfo(targetMachine);

            return(_clientCache.UseAsync(context, host, port, (nestedContext, client) => client.PushFileAsync(nestedContext, hash, stream, options)));
        }
Пример #19
0
        public async Task <TResult> CheckBandwidthAtIntervalAsync <TResult>(
            OperationContext context,
            Func <CancellationToken, Task <TResult> > copyTaskFactory,
            CopyOptions options,
            Func <string, TResult> getErrorResult)
            where TResult : ICopyResult
        {
            if (_historicalBandwidthLimitSource != null)
            {
                var timer = Stopwatch.StartNew();
                var(result, bytesCopied) = await impl();

                timer.Stop();

                // Bandwidth checker expects speed in MiB/s, so convert it.
                var speed = bytesCopied / timer.Elapsed.TotalSeconds / BytesInMb;
                _historicalBandwidthLimitSource.AddBandwidthRecord(speed);

                return(result);
            }
            else
            {
                return((await impl()).result);
            }

            async Task <(TResult result, long bytesCopied)> impl()
            {
                // This method should not fail with exceptions because the resulting task may be left unobserved causing an application to crash
                // (given that the app is configured to fail on unobserved task exceptions).
                var minimumSpeedInMbPerSec = _bandwidthLimitSource.GetMinimumSpeedInMbPerSec() * _config.BandwidthLimitMultiplier;

                minimumSpeedInMbPerSec = Math.Min(minimumSpeedInMbPerSec, _config.MaxBandwidthLimit);

                long startPosition    = options.TotalBytesCopied;
                long previousPosition = startPosition;
                var  copyCompleted    = false;

                using var copyCancellation = CancellationTokenSource.CreateLinkedTokenSource(context.Token);

                Task <TResult> copyTask = copyTaskFactory(copyCancellation.Token);

                // Subscribing for potential task failure here to avoid unobserved task exceptions.
                traceCopyTaskFailures();

                while (!copyCompleted)
                {
                    // Wait some time for bytes to be copied
                    var configBandwidthCheckInterval = options.BandwidthConfiguration?.Interval ?? _config.BandwidthCheckInterval;

                    if (options.BandwidthConfiguration != null)
                    {
                        minimumSpeedInMbPerSec = options.BandwidthConfiguration.RequiredMegabytesPerSecond;
                    }

                    var firstCompletedTask = await Task.WhenAny(copyTask,
                                                                Task.Delay(configBandwidthCheckInterval, context.Token));

                    copyCompleted = firstCompletedTask == copyTask;
                    if (copyCompleted)
                    {
                        var result = await copyTask;
                        result.MinimumSpeedInMbPerSec = minimumSpeedInMbPerSec;
                        var bytesCopied = result.Size ?? options.TotalBytesCopied;

                        TrackBytesReceived(bytesCopied - previousPosition);
                        return(result, bytesCopied);
                    }
                    else if (context.Token.IsCancellationRequested)
                    {
                        context.Token.ThrowIfCancellationRequested();
                    }

                    // Copy is not completed and operation has not been canceled, perform
                    // bandwidth check
                    var position = options.TotalBytesCopied;

                    var bytesTransferredPerIteration = position - previousPosition;

                    TrackBytesReceived(bytesTransferredPerIteration);

                    var receivedMiB  = bytesTransferredPerIteration / BytesInMb;
                    var currentSpeed = receivedMiB / configBandwidthCheckInterval.TotalSeconds;

                    if (currentSpeed == 0 || currentSpeed < minimumSpeedInMbPerSec)
                    {
                        copyCancellation.Cancel();

                        var totalBytesCopied = position - startPosition;
                        var result           = getErrorResult(
                            $"Average speed was {currentSpeed}MiB/s - under {minimumSpeedInMbPerSec}MiB/s requirement. Aborting copy with {totalBytesCopied} bytes copied (received {bytesTransferredPerIteration} bytes in {configBandwidthCheckInterval.TotalSeconds} seconds).");
                        return(result, totalBytesCopied);
                    }

                    previousPosition = position;
                }

                var copyFileResult = await copyTask;

                copyFileResult.MinimumSpeedInMbPerSec = minimumSpeedInMbPerSec;
                return(copyFileResult, previousPosition - startPosition);

                void traceCopyTaskFailures()
                {
                    // When the operation is cancelled, it is possible for the copy operation to fail.
                    // In this case we still want to trace the failure (but just with the debug severity and not with the error),
                    // but we should exclude ObjectDisposedException completely.
                    // That's why we don't use task.FireAndForget but tracing inside the task's continuation.
                    copyTask.ContinueWith(t =>
                    {
                        if (t.IsFaulted)
                        {
                            if (!(t.Exception?.InnerException is ObjectDisposedException))
                            {
                                context.TracingContext.Debug($"Checked copy failed. {t.Exception}", component: nameof(BandwidthChecker), operation: nameof(CheckBandwidthAtIntervalAsync));
                            }
                        }
                    });
                }
            }
        }
        public static async Task <LocalEntity> ToData(CdmLocalEntityDeclarationDefinition instance, CdmManifestDefinition manifest, ResolveOptions resOpt, CopyOptions options)
        {
            var localEntity = await DocumentPersistence.ToData(instance.EntityPath, manifest, resOpt, options, instance.Ctx);

            if (localEntity != null)
            {
                var t2pm          = new TraitToPropertyMap(instance);
                var isHiddenTrait = t2pm.FetchTraitReference("is.hidden");

                if (localEntity.Description == null)
                {
                    localEntity.Description = instance.Explanation;
                }
                localEntity.LastChildFileModifiedTime = instance.LastChildFileModifiedTime;
                localEntity.LastFileModifiedTime      = instance.LastFileModifiedTime;
                localEntity.LastFileStatusCheckTime   = instance.LastFileStatusCheckTime;

                if (isHiddenTrait != null)
                {
                    localEntity.IsHidden = true;
                }

                if (t2pm.FetchPropertyValue("cdmSchemas") is List <string> schemas)
                {
                    localEntity.Schemas = schemas;
                }

                if (instance.DataPartitions != null && instance.DataPartitions.Count > 0)
                {
                    localEntity.Partitions = new List <Partition>();

                    foreach (var element in instance.DataPartitions)
                    {
                        var partition = await DataPartitionPersistence.ToData(element, resOpt, options);

                        if (partition != null)
                        {
                            localEntity.Partitions.Add(partition);
                        }
                    }
                }
            }

            return(localEntity);
        }
Пример #21
0
 /// <summary>
 /// Copies a directory to a new location
 /// </summary>
 /// <param name="Source">Source directory</param>
 /// <param name="Destination">Destination to move the directory to</param>
 /// <param name="Recursive">If true it will go through all sub directories, otherwise it wont</param>
 /// <param name="Options">Copy options, can be set to copy if newer, always copy, or do not overwrite</param>
 public static void CopyDirectory(string Source, string Destination, bool Recursive = true, CopyOptions Options = CopyOptions.CopyAlways)
 {
     if (string.IsNullOrEmpty(Source))
         throw new ArgumentNullException("Source");
     if (string.IsNullOrEmpty(Destination))
         throw new ArgumentNullException("Destination");
     if (!DirectoryExists(Source))
         throw new ArgumentException("Source directory does not exist");
     DirectoryInfo SourceInfo = new DirectoryInfo(Source);
     DirectoryInfo DestinationInfo = new DirectoryInfo(Destination);
     CreateDirectory(Destination);
     List<FileInfo> Files = FileList(Source);
     foreach (FileInfo File in Files)
     {
         if (Options == CopyOptions.CopyAlways)
         {
             File.CopyTo(Path.Combine(DestinationInfo.FullName, File.Name), true);
         }
         else if (Options == CopyOptions.CopyIfNewer)
         {
             if (FileExists(Path.Combine(DestinationInfo.FullName, File.Name)))
             {
                 FileInfo FileInfo = new FileInfo(Path.Combine(DestinationInfo.FullName, File.Name));
                 if (FileInfo.LastWriteTime.CompareTo(File.LastWriteTime) < 0)
                 {
                     File.CopyTo(Path.Combine(DestinationInfo.FullName, File.Name), true);
                 }
             }
             else
             {
                 File.CopyTo(Path.Combine(DestinationInfo.FullName, File.Name), true);
             }
         }
         else if (Options == CopyOptions.DoNotOverwrite)
         {
             File.CopyTo(Path.Combine(DestinationInfo.FullName, File.Name), false);
         }
     }
     if (Recursive)
     {
         List<DirectoryInfo> Directories = DirectoryList(SourceInfo.FullName);
         foreach (DirectoryInfo Directory in Directories)
         {
             CopyDirectory(Directory.FullName, Path.Combine(DestinationInfo.FullName, Directory.Name), Recursive, Options);
         }
     }
 }
Пример #22
0
 public static TraitGroup ToData(CdmTraitGroupDefinition instance, ResolveOptions resOpt, CopyOptions options)
 {
     return(new TraitGroup
     {
         Explanation = instance.Explanation,
         TraitGroupName = instance.TraitGroupName,
         ExhibitsTraits = CopyDataUtils.ListCopyData(resOpt, instance.ExhibitsTraits, options)
     });
 }
Пример #23
0
 public static CopyMoveResult Copy(KernelTransaction transaction, string sourcePath, string destinationPath, CopyOptions copyOptions, CopyMoveProgressRoutine progressHandler, object userProgressData, PathFormat pathFormat)
 {
    return CopyMoveInternal(transaction, sourcePath, destinationPath, copyOptions, null, progressHandler, userProgressData, pathFormat);
 }
Пример #24
0
        public static async Task<LocalEntity> ToData(dynamic documentObjectOrPath, CdmManifestDefinition manifest, ResolveOptions resOpt, CopyOptions options, CdmCorpusContext ctx)
        {
            if (documentObjectOrPath is string)
            {
                if (await ctx.Corpus.FetchObjectAsync<CdmEntityDefinition>(documentObjectOrPath, manifest) is CdmEntityDefinition cdmEntity)
                {
                    var entity = await EntityPersistence.ToData(cdmEntity, resOpt, options, ctx);
                    if (cdmEntity.Owner != null && cdmEntity.Owner is CdmDocumentDefinition document)
                    {
                        if (document.Imports.Count > 0)
                        {
                            entity.Imports = new List<Import>();
                            foreach (var element in document.Imports)
                            {
                                var import = CdmFolder.ImportPersistence.ToData(element, resOpt, options);
                                // the corpus path in the imports are relative to the document where it was defined.
                                // when saving in model.json the documents are flattened to the manifest level
                                // so it is necessary to recalculate the path to be relative to the manifest.
                                var absolutePath = ctx.Corpus.Storage.CreateAbsoluteCorpusPath(import.CorpusPath, document);

                                if (!string.IsNullOrEmpty(document.Namespace) && absolutePath.StartsWith(document.Namespace + ":"))
                                {
                                    absolutePath = absolutePath.Substring(document.Namespace.Length + 1);
                                }
                                import.CorpusPath = ctx.Corpus.Storage.CreateRelativeCorpusPath(absolutePath, manifest);
                                entity.Imports.Add(import);
                            }
                        }
                    }
                    else
                    {
                        Logger.Warning(nameof(DocumentPersistence), ctx, $"Entity {cdmEntity.GetName()} is not inside a document or its owner is not a document.");
                    }
                    return entity;
                }
                else
                {
                    Logger.Error(nameof(DocumentPersistence), ctx, "There was an error while trying to fetch cdm entity doc.");
                    return null;
                }
            }

            return null;
        }
Пример #25
0
 public static void Copy(string sourceFileName, string destinationFileName, CopyOptions copyOptions, PathFormat pathFormat)
 {
    CopyMoveInternal(false, null, sourceFileName, destinationFileName, false, copyOptions, null, null, null, pathFormat);
 }
Пример #26
0
        public static Projection ToData(CdmProjection instance, ResolveOptions resOpt, CopyOptions options)
        {
            if (instance == null)
            {
                return(null);
            }

            dynamic source = null;

            if (instance.Source != null && instance.Source.GetType() == typeof(string))
            {
                source = instance.Source;
            }
            else if (instance.Source != null && !string.IsNullOrWhiteSpace(instance.Source.NamedReference) && instance.Source.ExplicitReference == null)
            {
                source = instance.Source.NamedReference;
            }
            else if (instance.Source != null && instance.Source.GetType() == typeof(CdmEntityReference))
            {
                source = EntityReferencePersistence.ToData(instance.Source, resOpt, options);
            }

            List <OperationBase> operations = null;

            if (instance.Operations != null && instance.Operations.Count > 0)
            {
                operations = new List <OperationBase>();
                foreach (CdmOperationBase operation in instance.Operations)
                {
                    switch (operation.ObjectType)
                    {
                    case CdmObjectType.OperationAddCountAttributeDef:
                        OperationAddCountAttribute addCountAttributeOp = OperationAddCountAttributePersistence.ToData(operation as CdmOperationAddCountAttribute, resOpt, options);
                        operations.Add(addCountAttributeOp);
                        break;

                    case CdmObjectType.OperationAddSupportingAttributeDef:
                        OperationAddSupportingAttribute addSupportingAttributeOp = OperationAddSupportingAttributePersistence.ToData(operation as CdmOperationAddSupportingAttribute, resOpt, options);
                        operations.Add(addSupportingAttributeOp);
                        break;

                    case CdmObjectType.OperationAddTypeAttributeDef:
                        OperationAddTypeAttribute addTypeAttributeOp = OperationAddTypeAttributePersistence.ToData(operation as CdmOperationAddTypeAttribute, resOpt, options);
                        operations.Add(addTypeAttributeOp);
                        break;

                    case CdmObjectType.OperationExcludeAttributesDef:
                        OperationExcludeAttributes excludeAttributesOp = OperationExcludeAttributesPersistence.ToData(operation as CdmOperationExcludeAttributes, resOpt, options);
                        operations.Add(excludeAttributesOp);
                        break;

                    case CdmObjectType.OperationArrayExpansionDef:
                        OperationArrayExpansion arrayExpansionOp = OperationArrayExpansionPersistence.ToData(operation as CdmOperationArrayExpansion, resOpt, options);
                        operations.Add(arrayExpansionOp);
                        break;

                    case CdmObjectType.OperationCombineAttributesDef:
                        OperationCombineAttributes combineAttributesOp = OperationCombineAttributesPersistence.ToData(operation as CdmOperationCombineAttributes, resOpt, options);
                        operations.Add(combineAttributesOp);
                        break;

                    case CdmObjectType.OperationRenameAttributesDef:
                        OperationRenameAttributes renameAttributesOp = OperationRenameAttributesPersistence.ToData(operation as CdmOperationRenameAttributes, resOpt, options);
                        operations.Add(renameAttributesOp);
                        break;

                    case CdmObjectType.OperationReplaceAsForeignKeyDef:
                        OperationReplaceAsForeignKey replaceAsForeignKeyOp = OperationReplaceAsForeignKeyPersistence.ToData(operation as CdmOperationReplaceAsForeignKey, resOpt, options);
                        operations.Add(replaceAsForeignKeyOp);
                        break;

                    case CdmObjectType.OperationIncludeAttributesDef:
                        OperationIncludeAttributes includeAttributesOp = OperationIncludeAttributesPersistence.ToData(operation as CdmOperationIncludeAttributes, resOpt, options);
                        operations.Add(includeAttributesOp);
                        break;

                    case CdmObjectType.OperationAddAttributeGroupDef:
                        OperationAddAttributeGroup addAttributeGroupOp = OperationAddAttributeGroupPersistence.ToData(operation as CdmOperationAddAttributeGroup, resOpt, options);
                        operations.Add(addAttributeGroupOp);
                        break;

                    default:
                        OperationBase baseOp = new OperationBase();
                        baseOp.Type = OperationTypeConvertor.OperationTypeToString(CdmOperationType.Error);
                        operations.Add(baseOp);
                        break;
                    }
                }
            }

            return(new Projection
            {
                Explanation = instance.Explanation,
                Source = source,
                Operations = operations,
                Condition = instance.Condition,
                RunSequentially = instance.RunSequentially
            });
        }
Пример #27
0
 public static void Copy(KernelTransaction transaction, string sourceFileName, string destinationFileName, CopyOptions copyOptions, bool preserveDates, PathFormat pathFormat)
 {
    CopyMoveInternal(false, transaction, sourceFileName, destinationFileName, preserveDates, copyOptions, null, null, null, pathFormat);
 }
Пример #28
0
 public override dynamic CopyData(ResolveOptions resOpt, CopyOptions options)
 {
     return(CdmObjectBase.CopyData <CdmE2ERelationship>(this, resOpt, options));
 }
Пример #29
0
        public static void PersistCorpusFolder(string rootPath, CdmFolderDefinition cdmFolder, AttributeResolutionDirectiveSet directiveSet, CopyOptions options = null)
        {
            if (cdmFolder != null)
            {
                string folderPath = rootPath + cdmFolder.FolderPath;
                Directory.CreateDirectory(folderPath);

                if (cdmFolder.Documents != null)
                {
                    cdmFolder.Documents.AllItems.ForEach(doc =>
                    {
                        ResolveOptions resOpt = new ResolveOptions {
                            WrtDoc = doc, Directives = directiveSet
                        };
                        PersistDocument(rootPath, resOpt, options);
                    });
                }

                if (cdmFolder.ChildFolders != null)
                {
                    cdmFolder.ChildFolders.AllItems.ForEach(f =>
                    {
                        PersistCorpusFolder(rootPath, f, directiveSet, options);
                    });
                }
            }
        }
Пример #30
0
 public override dynamic CopyData(ResolveOptions resOpt = null, CopyOptions options = null)
 {
     return(CdmObjectBase.CopyData <CdmOperationAddTypeAttribute>(this, resOpt, options));
 }
Пример #31
0
        public static DataPartition ToData(CdmDataPartitionDefinition instance, ResolveOptions resOpt, CopyOptions options)
        {
            var argumentsCopy = new List <Argument>();

            if (instance.Arguments != null)
            {
                foreach (var argumentList in instance.Arguments)
                {
                    foreach (var argumentValue in argumentList.Value)
                    {
                        argumentsCopy.Add(
                            new Argument()
                        {
                            Name  = argumentList.Key,
                            Value = argumentValue
                        }
                            );
                    }
                }
            }

            var result = new DataPartition
            {
                Name     = instance.Name,
                Location = instance.Location,
                LastFileStatusCheckTime = TimeUtils.GetFormattedDateString(instance.LastFileStatusCheckTime),
                LastFileModifiedTime    = TimeUtils.GetFormattedDateString(instance.LastFileModifiedTime),
                ExhibitsTraits          = CopyDataUtils.ListCopyData(resOpt, instance.ExhibitsTraits, options),
                Arguments         = argumentsCopy.Count() > 0 ? argumentsCopy : null,
                SpecializedSchema = instance.SpecializedSchema
            };

            return(result);
        }
Пример #32
0
 /// <summary>
 /// Copies a directory to a new location
 /// </summary>
 /// <param name="Source">Source directory</param>
 /// <param name="Destination">Destination to move the directory to</param>
 /// <param name="Recursive">If true it will go through all sub directories, otherwise it wont</param>
 /// <param name="Options">Copy options, can be set to copy if newer, always copy, or do not overwrite</param>
 public static void CopyDirectory(string Source, string Destination, bool Recursive,CopyOptions Options)
 {
     try
     {
         DirectoryInfo SourceInfo = new DirectoryInfo(Source);
         DirectoryInfo DestinationInfo = new DirectoryInfo(Destination);
         if (!DirectoryExists(Destination))
         {
             CreateDirectory(Destination);
         }
         List<FileInfo> Files = FileList(Source);
         foreach (FileInfo File in Files)
         {
             if (Options == CopyOptions.CopyAlways)
             {
                 File.CopyTo(Path.Combine(DestinationInfo.FullName, File.Name), true);
             }
             else if (Options == CopyOptions.CopyIfNewer)
             {
                 if (FileExists(Path.Combine(DestinationInfo.FullName, File.Name)))
                 {
                     FileInfo FileInfo = new FileInfo(Path.Combine(DestinationInfo.FullName, File.Name));
                     if (FileInfo.LastWriteTime.CompareTo(File.LastWriteTime) < 0)
                     {
                         File.CopyTo(Path.Combine(DestinationInfo.FullName, File.Name), true);
                     }
                 }
                 else
                 {
                     File.CopyTo(Path.Combine(DestinationInfo.FullName, File.Name), true);
                 }
             }
             else if (Options == CopyOptions.DoNotOverwrite)
             {
                 File.CopyTo(Path.Combine(DestinationInfo.FullName, File.Name), false);
             }
         }
         if (Recursive)
         {
             List<DirectoryInfo> Directories = DirectoryList(SourceInfo.FullName);
             foreach (DirectoryInfo Directory in Directories)
             {
                 CopyDirectory(Directory.FullName, Path.Combine(DestinationInfo.FullName, Directory.Name), Recursive, Options);
             }
         }
     }
     catch (Exception a)
     {
         throw a;
     }
 }
Пример #33
0
        public static async Task <SingleKeyRelationship> ToData(CdmE2ERelationship instance, ResolveOptions resOpt, CopyOptions options)
        {
            var fromAttribute = new AttributeReference
            {
                EntityName    = GetEntityName(instance.FromEntity),
                AttributeName = instance.FromEntityAttribute
            };

            var toAttribute = new AttributeReference
            {
                EntityName    = GetEntityName(instance.ToEntity),
                AttributeName = instance.ToEntityAttribute
            };

            var result = new SingleKeyRelationship
            {
                Type          = "SingleKeyRelationship",
                Description   = instance.Explanation,
                Name          = instance.Name,
                FromAttribute = fromAttribute,
                ToAttribute   = toAttribute
            };

            await Utils.ProcessTraitsAndAnnotationsToData(instance.Ctx, result, instance.ExhibitsTraits);

            return(result);
        }
Пример #34
0
        public static ManifestContent ToData(CdmManifestDefinition instance, ResolveOptions resOpt, CopyOptions options)
        {
            var documentContent = DocumentPersistence.ToData(instance, resOpt, options);
            var manifestContent = new ManifestContent()
            {
                ManifestName = instance.ManifestName,
                JsonSchemaSemanticVersion = documentContent.JsonSchemaSemanticVersion,
                Schema          = documentContent.Schema,
                Imports         = documentContent.Imports,
                DocumentVersion = documentContent.DocumentVersion
            };

            manifestContent.ManifestName              = instance.ManifestName;
            manifestContent.LastFileStatusCheckTime   = TimeUtils.GetFormattedDateString(instance.LastFileStatusCheckTime);
            manifestContent.LastFileModifiedTime      = TimeUtils.GetFormattedDateString(instance.LastFileModifiedTime);
            manifestContent.LastChildFileModifiedTime = TimeUtils.GetFormattedDateString(instance.LastChildFileModifiedTime);
            manifestContent.Entities       = CopyDataUtils.ListCopyData(resOpt, instance.Entities, options);
            manifestContent.SubManifests   = Utils.ListCopyData <ManifestDeclaration>(resOpt, instance.SubManifests, options);
            manifestContent.Explanation    = instance.Explanation;
            manifestContent.ExhibitsTraits = CopyDataUtils.ListCopyData(resOpt, instance.ExhibitsTraits, options);

            if (instance.Relationships != null && instance.Relationships.Count > 0)
            {
                manifestContent.Relationships = instance.Relationships.Select(relationship => { return(E2ERelationshipPersistence.ToData(relationship, resOpt, options)); }).ToList();
            }

            return(manifestContent);
        }
 internal static extern bool CopyFileTransacted([MarshalAs(UnmanagedType.LPWStr)] string lpExistingFileName,
    [MarshalAs(UnmanagedType.LPWStr)] string lpNewFileName, NativeCopyMoveProgressRoutine lpProgressRoutine,
    IntPtr lpData, [MarshalAs(UnmanagedType.Bool)] out bool pbCancel, CopyOptions dwCopyFlags,
    SafeHandle hTransaction);
 public CopyMoveResult CopyTo(string destinationPath, CopyOptions copyOptions, CopyMoveProgressRoutine progressHandler, object userProgressData, PathFormat pathFormat)
 {
    string destinationPathLp;
    CopyMoveResult cmr = CopyToMoveToInternal(destinationPath, copyOptions, null, progressHandler, userProgressData, out destinationPathLp, pathFormat);
    CopyToMoveToInternalRefresh(destinationPath, destinationPathLp);
    return cmr;
 }
        public static EntityDeclarationDefinition ToData(CdmReferencedEntityDeclarationDefinition instance, ResolveOptions resOpt, CopyOptions options)
        {
            var result = new EntityDeclarationDefinition
            {
                Type = EntityDeclarationDefinitionType.ReferencedEntity,
                LastFileStatusCheckTime = TimeUtils.GetFormattedDateString(instance.LastFileStatusCheckTime),
                LastFileModifiedTime    = TimeUtils.GetFormattedDateString(instance.LastFileModifiedTime),
                Explanation             = instance.Explanation,
                EntityName     = instance.EntityName,
                EntityPath     = instance.EntityPath,
                ExhibitsTraits = CopyDataUtils.ListCopyData(resOpt, instance.ExhibitsTraits, options)
            };

            return(result);
        }
Пример #38
0
 internal override Element CopyInto(Container container, CopyOptions options)
 {
     throw new Exception("Root elements cannot be copied.");
 }
 public DirectoryInfo CopyTo(string destinationPath, CopyOptions copyOptions, PathFormat pathFormat)
 {
    string destinationPathLp;
    CopyToMoveToInternal(destinationPath, copyOptions, null, null, null, out destinationPathLp, pathFormat);
    return new DirectoryInfo(Transaction, destinationPathLp, PathFormat.LongFullPath);
 }
Пример #40
0
 public override dynamic CopyData(ResolveOptions resOpt, CopyOptions options)
 {
     return(CdmObjectBase.CopyData <CdmAttributeReference>(this, resOpt, options));
 }
      private CopyMoveResult CopyToMoveToInternal(string destinationPath, CopyOptions? copyOptions, MoveOptions? moveOptions, CopyMoveProgressRoutine progressHandler, object userProgressData, out string longFullPath, PathFormat pathFormat)
      {
         string destinationPathLp = Path.GetExtendedLengthPathInternal(null, destinationPath, pathFormat, GetFullPathOptions.TrimEnd | GetFullPathOptions.RemoveTrailingDirectorySeparator | GetFullPathOptions.FullCheck);
         longFullPath = destinationPathLp;

         // Returns false when CopyMoveProgressResult is PROGRESS_CANCEL or PROGRESS_STOP.
         return Directory.CopyMoveInternal(Transaction, LongFullName, destinationPathLp, copyOptions, moveOptions, progressHandler, userProgressData, PathFormat.LongFullPath);
      }
Пример #42
0
 public static dynamic ToData(CdmPurposeReference instance, ResolveOptions resOpt, CopyOptions options)
 {
     return(CdmObjectRefPersistence.ToData(instance, resOpt, options));
 }
Пример #43
0
 public static void Copy(string sourcePath, string destinationPath, CopyOptions copyOptions, PathFormat pathFormat)
 {
    CopyMoveInternal(null, sourcePath, destinationPath, copyOptions, null, null, null, pathFormat);
 }
Пример #44
0
        override internal async Task <bool> SaveLinkedDocuments(CopyOptions options = null)
        {
            if (options == null)
            {
                options = new CopyOptions();
            }

            if (this.Imports != null)
            {
                foreach (CdmImport imp in this.Imports)
                {
                    if (await SaveDirtyLink(imp.CorpusPath, options) == false)
                    {
                        Logger.Error(nameof(CdmManifestDefinition), this.Ctx as ResolveContext, $"failed saving imported document {imp.AtCorpusPath}", "SaveLinkedDocuments");
                        return(false);
                    }
                }
            }
            if (this.Entities != null)
            {
                // only the local entity declarations please
                foreach (CdmEntityDeclarationDefinition def in this.Entities)
                {
                    if (def.ObjectType == CdmObjectType.LocalEntityDeclarationDef)
                    {
                        CdmLocalEntityDeclarationDefinition defImp = def as CdmLocalEntityDeclarationDefinition;
                        if (await SaveDirtyLink(defImp.EntityPath, options) == false)
                        {
                            Logger.Error(nameof(CdmManifestDefinition), this.Ctx as ResolveContext, $"failed saving local entity schema document {defImp.EntityPath}", "SaveLinkedDocuments");
                            return(false);
                        }

                        // also, partitions can have their own schemas
                        if (defImp.DataPartitions != null)
                        {
                            foreach (CdmDataPartitionDefinition part in defImp.DataPartitions)
                            {
                                if (part.SpecializedSchema != null)
                                {
                                    if (await SaveDirtyLink(defImp.EntityPath, options) == false)
                                    {
                                        Logger.Error(nameof(CdmManifestDefinition), this.Ctx as ResolveContext, $"failed saving local entity schema document {defImp.EntityPath}", "SaveLinkedDocuments");
                                        return(false);
                                    }
                                }
                            }
                        }
                        // so can patterns
                        if (defImp.DataPartitionPatterns != null)
                        {
                            foreach (CdmDataPartitionPatternDefinition part in defImp.DataPartitionPatterns)
                            {
                                if (part.SpecializedSchema != null)
                                {
                                    if (await SaveDirtyLink(part.SpecializedSchema, options) == false)
                                    {
                                        Logger.Error(nameof(CdmManifestDefinition), this.Ctx as ResolveContext, $"failed saving partition schema document {part.SpecializedSchema}", "SaveLinkedDocuments");
                                        return(false);
                                    }
                                }
                            }
                        }
                    }
                }
            }
            if (this.SubManifests != null)
            {
                foreach (CdmManifestDeclarationDefinition sub in this.SubManifests)
                {
                    if (await SaveDirtyLink(sub.Definition, options) == false)
                    {
                        Logger.Error(nameof(CdmManifestDefinition), this.Ctx as ResolveContext, $"failed saving sub-manifest document {sub.Definition}", "SaveLinkedDocuments");
                        return(false);
                    }
                }
            }

            return(true);
        }
Пример #45
0
 public static void Copy(KernelTransaction transaction, string sourcePath, string destinationPath, CopyOptions copyOptions, PathFormat pathFormat)
 {
    CopyMoveInternal(transaction, sourcePath, destinationPath, copyOptions, null, null, null, pathFormat);
 }
 public override dynamic CopyData(ResolveOptions resOpt = null, CopyOptions options = null)
 {
     return(CdmObjectBase.CopyData <CdmOperationReplaceAsForeignKey>(this, resOpt, options));
 }
Пример #47
0
      internal static CopyMoveResult CopyMoveInternal(KernelTransaction transaction, string sourcePath, string destinationPath, CopyOptions? copyOptions, MoveOptions? moveOptions, CopyMoveProgressRoutine progressHandler, object userProgressData, PathFormat pathFormat)
      {
         #region Setup

         if (pathFormat == PathFormat.RelativePath)
         {
            Path.CheckValidPath(sourcePath, true, true);
            Path.CheckValidPath(destinationPath, true, true);
         }
         else
         {
            // MSDN:. NET 3.5+: NotSupportedException: Path contains a colon character (:) that is not part of a drive label ("C:\").
            Path.CheckValidPath(sourcePath, false, false);
            Path.CheckValidPath(destinationPath, false, false);
         }

         // MSDN: .NET 4+ Trailing spaces are removed from the end of the path parameters before moving the directory.
         // TrimEnd() is also applied for AlphaFS implementation of method Directory.Copy(), .NET does not have this method.

         var options = GetFullPathOptions.TrimEnd | GetFullPathOptions.RemoveTrailingDirectorySeparator;

         string sourcePathLp = Path.GetExtendedLengthPathInternal(transaction, sourcePath, pathFormat, options);
         string destinationPathLp = Path.GetExtendedLengthPathInternal(transaction, destinationPath, pathFormat, options);

         // MSDN: .NET3.5+: IOException: The sourceDirName and destDirName parameters refer to the same file or directory.
         if (sourcePathLp.Equals(destinationPathLp, StringComparison.OrdinalIgnoreCase))
            NativeError.ThrowException(Win32Errors.ERROR_SAME_DRIVE, destinationPathLp);


         // Determine Copy or Move action.
         bool doCopy = copyOptions != null;
         bool doMove = !doCopy && moveOptions != null;

         if ((!doCopy && !doMove) || (doCopy && doMove))
            throw new NotSupportedException(Resources.UndeterminedCopyMoveAction);

         bool overwrite = doCopy
            ? (((CopyOptions)copyOptions & CopyOptions.FailIfExists) != CopyOptions.FailIfExists)
            : (((MoveOptions)moveOptions & MoveOptions.ReplaceExisting) == MoveOptions.ReplaceExisting);

         var cmr = new CopyMoveResult(sourcePathLp, destinationPathLp, true, doMove, false, (int)Win32Errors.ERROR_SUCCESS);

         #endregion //Setup

         #region Copy

         if (doCopy)
         {
            CreateDirectoryInternal(transaction, destinationPathLp, null, null, false, PathFormat.LongFullPath);

            foreach (var fsei in EnumerateFileSystemEntryInfosInternal<FileSystemEntryInfo>(transaction, sourcePathLp, Path.WildcardStarMatchAll, DirectoryEnumerationOptions.FilesAndFolders, PathFormat.LongFullPath))
            {
               string newDestinationPathLp = Path.CombineInternal(false, destinationPathLp, fsei.FileName);

               cmr = fsei.IsDirectory
                  ? CopyMoveInternal(transaction, fsei.LongFullPath, newDestinationPathLp, copyOptions, null, progressHandler, userProgressData, PathFormat.LongFullPath)
                  : File.CopyMoveInternal(false, transaction, fsei.LongFullPath, newDestinationPathLp, false, copyOptions, null, progressHandler, userProgressData, PathFormat.LongFullPath);

               if (cmr.IsCanceled)
                  return cmr;
            }
         }

         #endregion // Copy

         #region Move

         else
         {
            // MSDN: .NET3.5+: IOException: An attempt was made to move a directory to a different volume.
            if (((MoveOptions)moveOptions & MoveOptions.CopyAllowed) != MoveOptions.CopyAllowed)
               if (!Path.GetPathRoot(sourcePathLp, false).Equals(Path.GetPathRoot(destinationPathLp, false), StringComparison.OrdinalIgnoreCase))
                  NativeError.ThrowException(Win32Errors.ERROR_NOT_SAME_DEVICE, destinationPathLp);


            // MoveOptions.ReplaceExisting: This value cannot be used if lpNewFileName or lpExistingFileName names a directory.
            if (overwrite && File.ExistsInternal(true, transaction, destinationPathLp, PathFormat.LongFullPath))
               DeleteDirectoryInternal(null, transaction, destinationPathLp, true, true, false, true, PathFormat.LongFullPath);


            // Moves a file or directory, including its children.
            // Copies an existing directory, including its children to a new directory.
            cmr = File.CopyMoveInternal(true, transaction, sourcePathLp, destinationPathLp, false, null, moveOptions, progressHandler, userProgressData, PathFormat.LongFullPath);
         }

         #endregion // Move

         // The copy/move operation succeeded or was canceled.
         return cmr;
      }
Пример #48
0
 public override dynamic CopyData(ResolveOptions resOpt, CopyOptions options)
 {
     return(CdmObjectBase.CopyData <CdmReferencedEntityDeclarationDefinition>(this, resOpt, options));
 }
Пример #49
0
      internal static CopyMoveResult CopyMoveInternal(bool isFolder, KernelTransaction transaction, string sourceFileName, string destinationFileName, bool preserveDates, CopyOptions? copyOptions, MoveOptions? moveOptions, CopyMoveProgressRoutine progressHandler, object userProgressData, PathFormat pathFormat)
      {
         #region Setup

         if (pathFormat == PathFormat.RelativePath)
         {
            Path.CheckValidPath(sourceFileName, true, true);
            Path.CheckValidPath(destinationFileName, true, true);
         }
         else
         {
            // MSDN:. NET 3.5+: NotSupportedException: Path contains a colon character (:) that is not part of a drive label ("C:\").
            Path.CheckValidPath(sourceFileName, false, false);
            Path.CheckValidPath(destinationFileName, false, false);
         }

         string sourceFileNameLp = Path.GetExtendedLengthPathInternal(transaction, sourceFileName, pathFormat, GetFullPathOptions.RemoveTrailingDirectorySeparator);
         string destFileNameLp = Path.GetExtendedLengthPathInternal(transaction, destinationFileName, pathFormat, GetFullPathOptions.RemoveTrailingDirectorySeparator);


         // MSDN: If this flag is set to TRUE during the copy/move operation, the operation is canceled.
         // Otherwise, the copy/move operation will continue to completion.
         bool cancel = false;

         // Determine Copy or Move action.
         bool doCopy = copyOptions != null;
         bool doMove = !doCopy && moveOptions != null;

         if ((!doCopy && !doMove) || (doCopy && doMove))
            throw new NotSupportedException(Resources.UndeterminedCopyMoveAction);

         bool overwrite = doCopy
            ? (((CopyOptions) copyOptions & CopyOptions.FailIfExists) != CopyOptions.FailIfExists)
            : (((MoveOptions) moveOptions & MoveOptions.ReplaceExisting) == MoveOptions.ReplaceExisting);

         bool raiseException = progressHandler == null;

         // Setup callback function for progress notifications.
         var routine = (progressHandler != null)
            ? (totalFileSize, totalBytesTransferred, streamSize, streamBytesTransferred, dwStreamNumber, dwCallbackReason, hSourceFile, hDestinationFile, lpData)
               =>
               progressHandler(totalFileSize, totalBytesTransferred, streamSize, streamBytesTransferred, dwStreamNumber, dwCallbackReason, userProgressData)
            : (NativeMethods.NativeCopyMoveProgressRoutine) null;

         #endregion //Setup

         startCopyMove:

         uint lastError = Win32Errors.ERROR_SUCCESS;

         #region Win32 Copy/Move

         if (!(transaction == null || !NativeMethods.IsAtLeastWindowsVista
            ? doMove
               // MoveFileWithProgress() / MoveFileTransacted()
               // In the ANSI version of this function, the name is limited to MAX_PATH characters.
               // To extend this limit to 32,767 wide characters, call the Unicode version of the function and prepend "\\?\" to the path.
               // 2013-04-15: MSDN confirms LongPath usage.

               // CopyFileEx() / CopyFileTransacted()
               // In the ANSI version of this function, the name is limited to MAX_PATH characters.
               // To extend this limit to 32,767 wide characters, call the Unicode version of the function and prepend "\\?\" to the path.
               // 2013-04-15: MSDN confirms LongPath usage.

               ? NativeMethods.MoveFileWithProgress(sourceFileNameLp, destFileNameLp, routine, IntPtr.Zero, (MoveOptions) moveOptions)
               : NativeMethods.CopyFileEx(sourceFileNameLp, destFileNameLp, routine, IntPtr.Zero, out cancel, (CopyOptions) copyOptions)

            : doMove
               ? NativeMethods.MoveFileTransacted(sourceFileNameLp, destFileNameLp, routine, IntPtr.Zero, (MoveOptions) moveOptions, transaction.SafeHandle)
               : NativeMethods.CopyFileTransacted(sourceFileNameLp, destFileNameLp, routine, IntPtr.Zero, out cancel, (CopyOptions) copyOptions, transaction.SafeHandle)))
         {
            lastError = (uint) Marshal.GetLastWin32Error();

            if (lastError == Win32Errors.ERROR_REQUEST_ABORTED)
            {
               // If lpProgressRoutine returns PROGRESS_CANCEL due to the user canceling the operation,
               // CopyFileEx will return zero and GetLastError will return ERROR_REQUEST_ABORTED.
               // In this case, the partially copied destination file is deleted.
               //
               // If lpProgressRoutine returns PROGRESS_STOP due to the user stopping the operation,
               // CopyFileEx will return zero and GetLastError will return ERROR_REQUEST_ABORTED.
               // In this case, the partially copied destination file is left intact.

               cancel = true;
            }

            else if (raiseException)
            {
               #region Win32Errors

               switch (lastError)
               {
                  case Win32Errors.ERROR_FILE_NOT_FOUND:
                     // File.Copy()
                     // File.Move()
                     // MSDN: .NET 3.5+: FileNotFoundException: sourceFileName was not found. 
                     NativeError.ThrowException(lastError, sourceFileNameLp);
                     break;

                  case Win32Errors.ERROR_PATH_NOT_FOUND:
                     // File.Copy()
                     // File.Move()
                     // Directory.Move()
                     // MSDN: .NET 3.5+: DirectoryNotFoundException: The path specified in sourceFileName or destinationFileName is invalid (for example, it is on an unmapped drive).
                     NativeError.ThrowException(lastError, sourceFileNameLp);
                     break;

                  case Win32Errors.ERROR_FILE_EXISTS:
                     // File.Copy()
                     // Directory.Copy()
                     NativeError.ThrowException(lastError, destFileNameLp);
                     break;

                  default:
                     // For a number of error codes (sharing violation, path not found, etc)
                     // we don't know if the problem was with the source or dest file.

                     // Check if destination directory already exists.
                     // Directory.Move()
                     // MSDN: .NET 3.5+: IOException: destDirName already exists. 
                     if (ExistsInternal(true, transaction, destFileNameLp, PathFormat.LongFullPath))
                        NativeError.ThrowException(Win32Errors.ERROR_ALREADY_EXISTS, destFileNameLp);

                     if (doMove)
                     {
                        // Ensure that the source file or directory exists.
                        // Directory.Move()
                        // MSDN: .NET 3.5+: DirectoryNotFoundException: The path specified by sourceDirName is invalid (for example, it is on an unmapped drive). 
                        if (!ExistsInternal(isFolder, transaction, sourceFileNameLp, PathFormat.LongFullPath))
                           NativeError.ThrowException(isFolder ? Win32Errors.ERROR_PATH_NOT_FOUND : Win32Errors.ERROR_FILE_NOT_FOUND, sourceFileNameLp);
                     }


                     // Try reading the source file.
                     string fileNameLp = destFileNameLp;

                     if (!isFolder)
                     {
                        using (SafeFileHandle safeHandle = CreateFileInternal(transaction, sourceFileNameLp, ExtendedFileAttributes.None, null, FileMode.Open, 0, FileShare.Read, false, PathFormat.LongFullPath))
                           if (safeHandle.IsInvalid)
                              fileNameLp = sourceFileNameLp;
                     }

                     if (lastError == Win32Errors.ERROR_ACCESS_DENIED)
                     {
                        // File.Copy()
                        // File.Move()
                        // MSDN: .NET 3.5+: IOException: An I/O error has occurred.
                        //   Directory exists with the same name as the file.
                        if (!isFolder && ExistsInternal(true, transaction, destFileNameLp, PathFormat.LongFullPath))
                           NativeError.ThrowException(lastError, string.Format(CultureInfo.CurrentCulture, Resources.DirectoryExistsWithSameNameSpecifiedByPath, destFileNameLp));

                        else
                        {
                           var data = new NativeMethods.Win32FileAttributeData();
                           FillAttributeInfoInternal(transaction, destFileNameLp, ref data, false, true);

                           if (data.FileAttributes != (FileAttributes) (-1))
                           {
                              if ((data.FileAttributes & FileAttributes.ReadOnly) == FileAttributes.ReadOnly)
                              {
                                 // MSDN: .NET 3.5+: IOException: The directory specified by path is read-only.

                                 if (overwrite)
                                 {
                                    // Reset file system object attributes.
                                    SetAttributesInternal(isFolder, transaction, destFileNameLp, FileAttributes.Normal, true, PathFormat.LongFullPath);
                                    goto startCopyMove;
                                 }

                                 // MSDN: .NET 3.5+: UnauthorizedAccessException: destinationFileName is read-only.
                                 // MSDN: Win32 CopyFileXxx: This function fails with ERROR_ACCESS_DENIED if the destination file already exists
                                 // and has the FILE_ATTRIBUTE_HIDDEN or FILE_ATTRIBUTE_READONLY attribute set.

                                 throw new FileReadOnlyException(destFileNameLp);
                              }

                              // MSDN: Win32 CopyFileXxx: This function fails with ERROR_ACCESS_DENIED if the destination file already exists
                              // and has the FILE_ATTRIBUTE_HIDDEN or FILE_ATTRIBUTE_READONLY attribute set.
                              if ((data.FileAttributes & FileAttributes.Hidden) == FileAttributes.Hidden)
                                 NativeError.ThrowException(lastError, string.Format(CultureInfo.CurrentCulture, Resources.FileHidden, destFileNameLp));
                           }

                           // Observation: .NET 3.5+: For files: UnauthorizedAccessException: The caller does not have the required permission.
                           // Observation: .NET 3.5+: For directories: IOException: The caller does not have the required permission.
                           NativeError.ThrowException(lastError, destFileNameLp);
                        }
                     }

                     // MSDN: .NET 3.5+: An I/O error has occurred. 
                     // File.Copy(): IOException: destinationFileName exists and overwrite is false.
                     // File.Move(): The destination file already exists or sourceFileName was not found.
                     NativeError.ThrowException(lastError, fileNameLp);
                     break;
               }

               #endregion // Win32Errors
            }
         }

         #endregion // Win32 Copy/Move

         #region Transfer Timestamps

         // Apply original Timestamps if requested.
         // MoveFileWithProgress() / MoveFileTransacted() automatically preserve Timestamps.
         // File.Copy()
         if (preserveDates && doCopy && lastError == Win32Errors.ERROR_SUCCESS)
         {
            // Currently preserveDates is only used with files.
            var data = new NativeMethods.Win32FileAttributeData();
            int dataInitialised = FillAttributeInfoInternal(transaction, sourceFileNameLp, ref data, false, true);

            if (dataInitialised == Win32Errors.ERROR_SUCCESS && data.FileAttributes != (FileAttributes) (-1))
               SetFsoDateTimeInternal(false, transaction, destFileNameLp, DateTime.FromFileTimeUtc(data.CreationTime),
                  DateTime.FromFileTimeUtc(data.LastAccessTime), DateTime.FromFileTimeUtc(data.LastWriteTime), PathFormat.LongFullPath);
         }

         #endregion // Transfer Timestamps

         // The copy/move operation succeeded, failed or was canceled.
         return new CopyMoveResult(sourceFileNameLp, destFileNameLp, isFolder, doMove, cancel, (int) lastError);
      }
Пример #50
0
        public async Task <FileCopyOperationSummary> DownloadFile(string serverPath, FileInfo localDestination, CancellationToken cancellationToken = default(CancellationToken), CopyOptions downloadOptions = null)
        {
            serverPath = GetValidatedServerPath(serverPath);

            var fromUri = new Uri(this.baseUri, serverPath);
            var options = downloadOptions ?? DefaultDownloadOptions;

            // if a non-empty local file exists but we cannot overwrite files
            if (!options.OverwriteFiles && localDestination.Exists && localDestination.Length > 0)
            {
                throw new ArgumentException($"Local file '{localDestination.FullName}' already exists!", nameof(localDestination));
            }

            FileCopyOperationSummary ret = new FileCopyOperationSummary(fromUri, localDestination, options);
            var swTotal  = Stopwatch.StartNew();
            int tryCount = 1;

            while (true)
            {
                try
                {
                    var fromMetadata = await GetFileMetadata(serverPath, cancellationToken);

                    var sw = Stopwatch.StartNew();

                    this.ftpClient.RetryAttempts = downloadOptions.MaxRetryCount;
                    var success = await this.ftpClient.DownloadFileAsync(localDestination.FullName, serverPath, FtpLocalExists.Overwrite, FtpVerify.Retry | FtpVerify.Throw, null, cancellationToken);
                } catch (Exception ex)
                {
                    if (tryCount > options.MaxRetryCount)
                    {
                        ret.Error = ex;
                        ret.
                    }
                }
            }

            return(ret);
        }
Пример #51
0
 public static void Copy(string sourceFileName, string destinationFileName, CopyOptions copyOptions, bool preserveDates)
 {
    CopyMoveInternal(false, null, sourceFileName, destinationFileName, preserveDates, copyOptions, null, null, null, PathFormat.RelativePath);
 }
Пример #52
0
        public static async Task <Partition> ToData(CdmDataPartitionDefinition instance, ResolveOptions resOpt, CopyOptions options)
        {
            var result = new Partition
            {
                Name        = instance.Name,
                Description = instance.Description,
                Location    = instance.Ctx.Corpus.Storage.CorpusPathToAdapterPath(
                    instance.Ctx.Corpus.Storage.CreateAbsoluteCorpusPath(
                        instance.Location, instance.InDocument)),
                RefreshTime             = instance.RefreshTime,
                FileFormatSettings      = null,
                LastFileModifiedTime    = instance.LastFileModifiedTime,
                LastFileStatusCheckTime = instance.LastFileStatusCheckTime
            };

            if (string.IsNullOrEmpty(result.Location))
            {
                Logger.Warning(nameof(DataPartitionPersistence), instance.Ctx, $"Couldn't find data partition's location for partition {result.Name}.", nameof(ToData));
            }

            await Utils.ProcessAnnotationsToData(instance.Ctx, result, instance.ExhibitsTraits);

            var t2pm = new TraitToPropertyMap(instance);

            var isHiddenTrait = t2pm.FetchTraitReference("is.hidden");

            if (isHiddenTrait != null)
            {
                result.IsHidden = true;
            }

            var csvTrait = t2pm.FetchTraitReference("is.partition.format.CSV");

            if (csvTrait != null)
            {
                var csvFormatSettings = Utils.CreateCsvFormatSettings(csvTrait);

                if (csvFormatSettings != null)
                {
                    result.FileFormatSettings      = csvFormatSettings;
                    result.FileFormatSettings.Type = "CsvFormatSettings";
                }
                else
                {
                    Logger.Error(nameof(DataPartitionPersistence), instance.Ctx,
                                 "There was a problem while processing csv format trait inside data partition.");

                    return(null);
                }
            }

            return(result);
        }
Пример #53
0
 public static void Copy(KernelTransaction transaction, string sourceFileName, string destinationFileName, CopyOptions copyOptions)
 {
    CopyMoveInternal(false, transaction, sourceFileName, destinationFileName, false, copyOptions, null, null, null, PathFormat.RelativePath);
 }
Пример #54
0
        override internal async Task <bool> SaveLinkedDocuments(CopyOptions options = null)
        {
            HashSet <string> links = new HashSet <string>();

            if (options == null)
            {
                options = new CopyOptions();
            }

            if (this.Imports != null)
            {
                this.Imports.ToList().ForEach(x => links.Add(x.CorpusPath));
            }
            if (this.Entities != null)
            {
                // only the local entity declarations please
                foreach (CdmEntityDeclarationDefinition def in this.Entities)
                {
                    if (def.ObjectType == CdmObjectType.LocalEntityDeclarationDef)
                    {
                        CdmLocalEntityDeclarationDefinition defImp = def as CdmLocalEntityDeclarationDefinition;
                        links.Add(defImp.EntityPath);

                        // also, partitions can have their own schemas
                        if (defImp.DataPartitions != null)
                        {
                            foreach (CdmDataPartitionDefinition part in defImp.DataPartitions)
                            {
                                if (part.SpecializedSchema != null)
                                {
                                    links.Add(part.SpecializedSchema);
                                }
                            }
                        }
                        // so can patterns
                        if (defImp.DataPartitionPatterns != null)
                        {
                            foreach (CdmDataPartitionPatternDefinition part in defImp.DataPartitionPatterns)
                            {
                                if (part.SpecializedSchema != null)
                                {
                                    links.Add(part.SpecializedSchema);
                                }
                            }
                        }
                    }
                }
            }

            // Get all Cdm documents sequentially
            List <CdmDocumentDefinition> docs = new List <CdmDocumentDefinition>();

            foreach (var link in links)
            {
                CdmDocumentDefinition document = await FetchDocumentDefinition(link);

                if (document == null)
                {
                    return(false);
                }
                docs.Add(document);
            }

            // Save all dirty Cdm documents in parallel
            IEnumerable <Task <bool> > tasks = docs.Select(doc => SaveDocumentIfDirty(doc, options));
            var results = await Task.WhenAll(tasks);

            if (this.SubManifests != null)
            {
                foreach (var subDeclaration in this.SubManifests)
                {
                    CdmManifestDefinition subManifest = await FetchDocumentDefinition(subDeclaration.Definition) as CdmManifestDefinition;

                    if (subManifest == null || !await SaveDocumentIfDirty(subManifest, options))
                    {
                        return(false);
                    }
                }
            }

            return(true);
        }
Пример #55
0
 public static CopyMoveResult Copy(KernelTransaction transaction, string sourceFileName, string destinationFileName, CopyOptions copyOptions, bool preserveDates, CopyMoveProgressRoutine progressHandler, object userProgressData, PathFormat pathFormat)
 {
    return CopyMoveInternal(false, transaction, sourceFileName, destinationFileName, preserveDates, copyOptions, null, progressHandler, userProgressData, pathFormat);
 }
Пример #56
0
 public FileInfo CopyTo(string destinationPath, CopyOptions copyOptions)
 {
    string destinationPathLp;
    CopyToMoveToInternal(destinationPath, false, copyOptions, null, null, null, out destinationPathLp, PathFormat.RelativePath);
    return new FileInfo(Transaction, destinationPathLp, PathFormat.LongFullPath);
 }