Example #1
0
        public bool FileExists(string file)
        {
            try
            {
                FDCFileInfo fi = GetFileInfo(file);

                if (fi != null)
                {
                    return(true);
                }
            }
            catch (Exception ex)
            {
                STEM.Sys.EventLog.WriteEntry("Authentication.FileExists", ex.ToString(), STEM.Sys.EventLog.EventLogEntryType.Error);
            }

            return(false);
        }
        public override DateTime GetAgeBasis(string initiationSource)
        {
            FDCFileInfo fi = GetFileInfo(initiationSource);

            if (fi != null)
            {
                switch (SelectedOrigin)
                {
                case STEM.Surge.AgeOrigin.LastWriteTime:
                    return(fi.LastWriteTimeUtc);

                case STEM.Surge.AgeOrigin.LastAccessTime:
                    return(fi.LastAccessTimeUtc);

                case STEM.Surge.AgeOrigin.CreationTime:
                    return(fi.CreationTimeUtc);
                }
            }

            FDCDirectoryInfo di = GetDirectoryInfo(initiationSource);

            if (di != null)
            {
                switch (SelectedOrigin)
                {
                case STEM.Surge.AgeOrigin.LastWriteTime:
                    return(di.LastWriteTimeUtc);

                case STEM.Surge.AgeOrigin.LastAccessTime:
                    return(di.LastAccessTimeUtc);

                case STEM.Surge.AgeOrigin.CreationTime:
                    return(di.CreationTimeUtc);
                }
            }

            return(DateTime.MinValue);
        }
Example #3
0
        bool Execute()
        {
            try
            {
                if (Direction == S3Direction.ToS3Bucket)
                {
                    ExpandDestination = false;
                }
                else
                {
                    ExpandSource = false;
                }

                List <string> sources = new List <string>();
                if (ExpandSource)
                {
                    sources = STEM.Sys.IO.Path.ExpandRangedPath(SourcePath);
                }
                else
                {
                    sources.Add(SourcePath);
                }

                List <string> destinations = new List <string>();
                if (ExpandDestination)
                {
                    destinations = STEM.Sys.IO.Path.ExpandRangedPath(DestinationPath);
                }
                else
                {
                    destinations.Add(DestinationPath);
                }

                List <string> sourceFiles = new List <string>();

                int filesActioned = 0;

                foreach (string src in sources)
                {
                    List <S3Object> items = new List <S3Object>();

                    if (Direction == S3Direction.ToS3Bucket)
                    {
                        sourceFiles = STEM.Sys.IO.Directory.STEM_GetFiles(src, FileFilter, DirectoryFilter, (RecurseSource ? SearchOption.AllDirectories : SearchOption.TopDirectoryOnly), false);
                    }
                    else
                    {
                        string bucket = Authentication.BucketFromPath(src);
                        string prefix = Authentication.PrefixFromPath(src);

                        items = Authentication.ListObjects(bucket, prefix, S3ListType.File, RecurseSource, DirectoryFilter, FileFilter);

                        sourceFiles = items.Select(i => Authentication.ToString(i)).ToList();
                    }

                    foreach (string s in sourceFiles)
                    {
                        try
                        {
                            bool success = false;

                            Exception lastEX = null;

                            foreach (string d in destinations)
                            {
                                try
                                {
                                    string dFile = "";

                                    try
                                    {
                                        if (PopulatePostMortemMeta)
                                        {
                                            PostMortemMetaData["SourceIP"]      = STEM.Sys.IO.Path.IPFromPath(s);
                                            PostMortemMetaData["DestinationIP"] = STEM.Sys.IO.Path.IPFromPath(d);

                                            if (Direction == S3Direction.ToS3Bucket)
                                            {
                                                PostMortemMetaData["FileSize"] = new FileInfo(s).Length.ToString();
                                            }
                                            else
                                            {
                                                PostMortemMetaData["FileSize"] = Authentication.GetFileInfo(s).Size.ToString();
                                            }
                                        }
                                    }
                                    catch { }

                                    if (Direction == S3Direction.ToS3Bucket)
                                    {
                                        string dPath = STEM.Sys.IO.Path.AdjustPath(d);
                                        if (RecurseSource && RecreateTree)
                                        {
                                            dPath = System.IO.Path.Combine(dPath, STEM.Sys.IO.Path.GetDirectoryName(s).Replace(STEM.Sys.IO.Path.AdjustPath(src), "").Trim(System.IO.Path.DirectorySeparatorChar));
                                        }

                                        dPath = System.IO.Path.Combine(dPath, DestinationFilename);

                                        if (dPath.Contains("*.*"))
                                        {
                                            dPath = dPath.Replace("*.*", STEM.Sys.IO.Path.GetFileName(s));
                                        }

                                        if (dPath.Contains("*"))
                                        {
                                            dPath = dPath.Replace("*", STEM.Sys.IO.Path.GetFileNameWithoutExtension(s));
                                        }

                                        if (!Authentication.DirectoryExists(STEM.Sys.IO.Path.GetDirectoryName(dPath)))
                                        {
                                            Authentication.CreateDirectory(STEM.Sys.IO.Path.GetDirectoryName(dPath));
                                        }

                                        if (Authentication.FileExists(dPath))
                                        {
                                            switch (ExistsAction)
                                            {
                                            case Sys.IO.FileExistsAction.Overwrite:
                                                Authentication.DeleteFile(dPath);
                                                dFile = dPath;
                                                break;

                                            case Sys.IO.FileExistsAction.OverwriteIfNewer:

                                                if (Authentication.GetFileInfo(dPath).LastWriteTimeUtc >= File.GetLastWriteTimeUtc(s))
                                                {
                                                    continue;
                                                }

                                                Authentication.DeleteFile(dPath);
                                                dFile = dPath;
                                                break;

                                            case Sys.IO.FileExistsAction.Skip:
                                                continue;

                                            case Sys.IO.FileExistsAction.Throw:
                                                throw new IOException("Destination file exists. (" + dPath + ")");

                                            case Sys.IO.FileExistsAction.MakeUnique:
                                                dFile = Authentication.UniqueFilename(dPath);
                                                break;
                                            }
                                        }
                                        else
                                        {
                                            dFile = dPath;
                                        }

                                        string bucket = Authentication.BucketFromPath(dFile);
                                        string prefix = Authentication.PrefixFromPath(dFile);

                                        TransferUtility ftu = new TransferUtility(Authentication.Client);

                                        TransferUtilityUploadRequest request = new TransferUtilityUploadRequest
                                        {
                                            BucketName = bucket,
                                            Key        = prefix,
                                            FilePath   = STEM.Sys.IO.Path.AdjustPath(s),
                                            PartSize   = 10485760
                                        };

                                        ftu.UploadAsync(request).Wait();
                                    }
                                    else
                                    {
                                        string dPath = STEM.Sys.IO.Path.AdjustPath(d);
                                        if (RecurseSource && RecreateTree)
                                        {
                                            dPath = System.IO.Path.Combine(dPath, STEM.Sys.IO.Path.GetDirectoryName(s).Replace(STEM.Sys.IO.Path.AdjustPath(src), "").Trim(System.IO.Path.DirectorySeparatorChar));
                                        }

                                        dPath = System.IO.Path.Combine(dPath, DestinationFilename);

                                        if (dPath.Contains("*.*"))
                                        {
                                            dPath = dPath.Replace("*.*", STEM.Sys.IO.Path.GetFileName(s));
                                        }

                                        if (dPath.Contains("*"))
                                        {
                                            dPath = dPath.Replace("*", STEM.Sys.IO.Path.GetFileNameWithoutExtension(s));
                                        }

                                        if (File.Exists(dPath))
                                        {
                                            switch (ExistsAction)
                                            {
                                            case Sys.IO.FileExistsAction.Overwrite:
                                                File.Delete(dPath);
                                                dFile = dPath;
                                                break;

                                            case Sys.IO.FileExistsAction.OverwriteIfNewer:

                                                if (File.GetLastWriteTimeUtc(dPath) >= Authentication.GetFileInfo(s).LastWriteTimeUtc)
                                                {
                                                    continue;
                                                }

                                                File.Delete(dPath);
                                                dFile = dPath;
                                                break;

                                            case Sys.IO.FileExistsAction.Skip:
                                                continue;

                                            case Sys.IO.FileExistsAction.Throw:
                                                throw new IOException("Destination file exists. (" + dPath + ")");

                                            case Sys.IO.FileExistsAction.MakeUnique:
                                                dFile = STEM.Sys.IO.File.UniqueFilename(dPath);
                                                break;
                                            }
                                        }
                                        else
                                        {
                                            dFile = dPath;
                                        }

                                        if (!Directory.Exists(STEM.Sys.IO.Path.GetDirectoryName(dFile)))
                                        {
                                            Directory.CreateDirectory(STEM.Sys.IO.Path.GetDirectoryName(dFile));
                                        }

                                        string bucket = Authentication.BucketFromPath(s);
                                        string prefix = Authentication.PrefixFromPath(s);

                                        FDCFileInfo fi = Authentication.GetFileInfo(s);

                                        if (fi != null)
                                        {
                                            string tmp = "";
                                            try
                                            {
                                                tmp = Path.Combine(STEM.Sys.IO.Path.GetDirectoryName(dFile), "TEMP");

                                                if (!Directory.Exists(tmp))
                                                {
                                                    Directory.CreateDirectory(tmp);
                                                }

                                                tmp = Path.Combine(tmp, STEM.Sys.IO.Path.GetFileName(dFile));

                                                TransferUtility ftu = new TransferUtility(Authentication.Client);

                                                TransferUtilityDownloadRequest request = new TransferUtilityDownloadRequest
                                                {
                                                    BucketName = bucket,
                                                    Key        = prefix,
                                                    FilePath   = tmp
                                                };

                                                ftu.DownloadAsync(request).Wait();

                                                try
                                                {
                                                    File.SetLastWriteTimeUtc(tmp, fi.LastWriteTimeUtc);
                                                }
                                                catch { }

                                                File.Move(tmp, STEM.Sys.IO.Path.AdjustPath(dFile));
                                            }
                                            finally
                                            {
                                                try
                                                {
                                                    if (File.Exists(tmp))
                                                    {
                                                        File.Delete(tmp);
                                                    }
                                                }
                                                catch { }
                                            }
                                        }
                                        else
                                        {
                                            throw new System.IO.FileNotFoundException(s);
                                        }
                                    }

                                    if (!String.IsNullOrEmpty(dFile))
                                    {
                                        filesActioned++;

                                        _FilesActioned[s] = dFile;

                                        if (Action == ActionType.Move)
                                        {
                                            AppendToMessage(s + " moved to " + dFile);
                                        }
                                        else
                                        {
                                            AppendToMessage(s + " copied to " + dFile);
                                        }
                                    }

                                    success = true;

                                    if (DestinationActionRule == DestinationRule.FirstSuccess)
                                    {
                                        break;
                                    }
                                }
                                catch (Exception ex)
                                {
                                    lastEX = ex;

                                    if (DestinationActionRule == DestinationRule.AllOrNone)
                                    {
                                        throw ex;
                                    }
                                }
                            }

                            if (!success)
                            {
                                throw new Exception("No successful actions taken for " + s, lastEX); // + "\r\n" + ((lastEX == null) ? "No additional information." : lastEX.ToString()));
                            }
                            if (Action == ActionType.Move)
                            {
                                if (Direction == S3Direction.ToS3Bucket)
                                {
                                    File.Delete(s);
                                }
                                else
                                {
                                    Authentication.DeleteFile(s);
                                }
                            }
                        }
                        catch (AggregateException ex)
                        {
                            foreach (Exception e in ex.InnerExceptions)
                            {
                                AppendToMessage(e.Message);
                                Exceptions.Add(e);
                            }
                        }
                        catch (Exception ex)
                        {
                            AppendToMessage(ex.Message);
                            Exceptions.Add(ex);
                        }
                    }
                }

                if (PopulatePostMortemMeta)
                {
                    PostMortemMetaData["FilesActioned"] = filesActioned.ToString();
                }
            }
            catch (AggregateException ex)
            {
                foreach (Exception e in ex.InnerExceptions)
                {
                    AppendToMessage(e.Message);
                    Exceptions.Add(e);
                }
            }
            catch (Exception ex)
            {
                AppendToMessage(ex.Message);
                Exceptions.Add(ex);
            }

            if (_FilesActioned.Count == 0)
            {
                switch (ZeroFilesAction)
                {
                case FailureAction.SkipRemaining:
                    SkipRemaining();
                    return(true);

                case FailureAction.SkipNext:
                    SkipNext();
                    return(true);

                case FailureAction.SkipToLabel:
                    SkipForwardToFlowControlLabel(FailureActionLabel);
                    return(true);

                case FailureAction.Rollback:
                    RollbackAllPreceedingAndSkipRemaining();
                    break;

                case FailureAction.Continue:
                    return(true);
                }

                Message = "0 Files Actioned\r\n" + Message;
            }

            return(Exceptions.Count == 0);
        }
Example #4
0
        protected override void _Rollback()
        {
            if (ExecutionMode == ExecuteOn.ForwardExecution)
            {
                foreach (string d in _FilesActioned.Keys)
                {
                    try
                    {
                        string s = _FilesActioned[d];

                        if (Action == ActionType.Move)
                        {
                            if (Direction == S3Direction.ToS3Bucket)
                            {
                                string bucket = Authentication.BucketFromPath(s);
                                string prefix = Authentication.PrefixFromPath(s);

                                FDCFileInfo fi = Authentication.GetFileInfo(s);

                                if (fi != null)
                                {
                                    string tmp = "";

                                    try
                                    {
                                        tmp = Path.Combine(STEM.Sys.IO.Path.GetDirectoryName(d), "TEMP");

                                        if (!Directory.Exists(tmp))
                                        {
                                            Directory.CreateDirectory(tmp);
                                        }

                                        tmp = Path.Combine(tmp, STEM.Sys.IO.Path.GetFileName(d));

                                        TransferUtility ftu = new TransferUtility(Authentication.Client);

                                        TransferUtilityDownloadRequest request = new TransferUtilityDownloadRequest
                                        {
                                            BucketName = bucket,
                                            Key        = prefix,
                                            FilePath   = tmp
                                        };

                                        ftu.DownloadAsync(request).Wait();

                                        try
                                        {
                                            File.SetLastWriteTimeUtc(tmp, fi.LastWriteTimeUtc);
                                        }
                                        catch { }

                                        File.Move(tmp, STEM.Sys.IO.Path.AdjustPath(d));
                                    }
                                    finally
                                    {
                                        try
                                        {
                                            if (File.Exists(tmp))
                                            {
                                                File.Delete(tmp);
                                            }
                                        }
                                        catch { }
                                    }
                                }
                                else
                                {
                                    throw new System.IO.FileNotFoundException(s);
                                }
                            }
                            else
                            {
                                string bucket = Authentication.BucketFromPath(d);
                                string prefix = Authentication.PrefixFromPath(d);

                                TransferUtility ftu = new TransferUtility(Authentication.Client);

                                TransferUtilityUploadRequest request = new TransferUtilityUploadRequest
                                {
                                    BucketName = bucket,
                                    Key        = prefix,
                                    FilePath   = STEM.Sys.IO.Path.AdjustPath(s),
                                    PartSize   = 10485760
                                };

                                ftu.UploadAsync(request).Wait();
                            }
                        }

                        if (Direction == S3Direction.ToS3Bucket)
                        {
                            Authentication.DeleteFile(s);
                        }
                        else
                        {
                            STEM.Sys.IO.File.STEM_Delete(s, false, Retry, RetryDelaySeconds);
                        }
                    }
                    catch { }
                }
            }
            else
            {
                Execute();
            }
        }