Beispiel #1
0
        private object deleteSingleFile(delctx ctx, FileInfo f)
        {
            if (ctx.nameFilter != null && (!ctx.nameFilter.IsMatch(f.FullName) || !CheckHidden(f)))
            {
                VerboseMessage("{0} did not pass filter", f.FullName);
                return(null);
            }
            bool skip = false;

            if (!ReadOnly && ((f.Attributes & FileAttributes.ReadOnly) != 0))
            {
                skip = true;
            }

            object ret = ProcessComplete(new FileOrDirectoryInfo(f), null, skip, skipNew =>
            {
                if (!skipNew)
                {
                    VerboseMessage("{0} {1}", Wipe?"Wiping":"Deleting", f.FullName);
                    if (ReadOnly)
                    {
                        f.Attributes = f.Attributes & ~(FileAttributes.System | FileAttributes.Hidden | FileAttributes.ReadOnly);
                    }
                    if (Wipe)
                    {
                        var len = f.Length;
                        var buf = new byte[512];
                        using (var fi = new FileStream(f.FullName, FileMode.OpenOrCreate, FileAccess.Write, FileShare.None, 512, FileOptions.WriteThrough))
                        {
                            for (var offset = 0L; offset < len; offset += buf.Length)
                            {
                                fi.Write(buf, 0, buf.Length);
                            }
                        }
                    }
                    f.Delete();
                }
                return(null);
            });

            return(ret);
        }
Beispiel #2
0
        /// Execute action
        public override object Execute()
        {
            string fromExpanded = Context.TransformStr(From, Transform);

            if (string.IsNullOrEmpty(fromExpanded))
            {
                return(null);
            }

            delctx ctx = new delctx();

            ctx.dirFilter  = new FullPathFilter(Syntax, Context.TransformStr(DirectoryFilter, Transform), BackslashOption.Add);
            ctx.nameFilter = new FileNameOnlyFilter(Syntax, Context.TransformStr(Filter, Transform));

            object ret = null;

            if (File.Exists(fromExpanded))
            {
                FileInfo fr = new FileInfo(fromExpanded);
                ret = deleteSingleFile(ctx, fr);
            }
            else
            {
                DirectoryInfo dir = new DirectoryInfo(fromExpanded);
                if (dir.Exists)
                {
                    ret = delete(ctx, dir, dir);
                }
                else
                {
                    VerboseMessage("{0} not found.", fromExpanded);
                }
            }

            if (ReturnValue.IsBreak(ret))
            {
                return(null);
            }
            ReturnValue.RethrowIfException(ret);
            return(ret);
        }
Beispiel #3
0
        private object delete(delctx ctx, DirectoryInfo root, DirectoryInfo dir)
        {
            bool isRoot       = (root == dir);
            bool isVisible    = (isRoot || CheckHidden(dir));
            bool processFiles = isVisible;

            if (processFiles && (ctx.dirFilter != null && !ctx.dirFilter.IsMatch(dir.FullName)))
            {
                processFiles = false;
                VerboseMessage("{0} did not pass directory filter", dir.FullName);
            }
            if (processFiles)
            {
                foreach (FileInfo f in dir.GetFiles())
                {
                    object ret = deleteSingleFile(ctx, f);
                    if (ret != null)
                    {
                        return(ret);
                    }
                }
            }
            if (Recursive)
            {
                foreach (DirectoryInfo d in dir.GetDirectories())
                {
                    object ret = delete(ctx, dir, d);
                    if (ret != null)
                    {
                        return(ret);
                    }
                }
            }
            object r = null;

            if (processFiles && (Root || root.FullName != dir.FullName))
            {
                bool notEmpty = (dir.GetFiles().Length != 0 || dir.GetDirectories().Length != 0);
                if (notEmpty)
                {
                    VerboseMessage("Directory {0} contains files. Skipped.", dir.FullName);
                }
                else
                {
                    bool skip = false;
                    if (!ReadOnly && ((dir.Attributes & FileAttributes.ReadOnly) != 0))
                    {
                        skip = true;
                    }

                    r = ProcessComplete(new FileOrDirectoryInfo(dir), null, skip, skNew =>
                    {
                        skip = skNew;
                        if (!skip)
                        {
                            dir.Attributes = dir.Attributes & ~(FileAttributes.System | FileAttributes.Hidden | FileAttributes.ReadOnly);
                            dir.Delete();
                        }
                        return(null);
                    });
                }
            }

            return(r);
        }