Ejemplo n.º 1
0
        private ulong calc_size(FileInfoEx info)
        {
            var ret = 0UL;

            if (((options & CopyEngineOptions.CopyFilesRecursive) == CopyEngineOptions.CopyFilesRecursive) &&
                (info.Directory))
            {
                if ((destination_remote) || (((options & CopyEngineOptions.CopySymlinkAsSymlink) != CopyEngineOptions.CopySymlinkAsSymlink) || (info.LinkInformation == null)))
                {
                    var dir_enum = new FileInfoExEnumerable
                                       (Path.Combine(info.FullName, "*"),
                                       true,
                                       true,
                                       true,
                                       true);
                    foreach (var ch_info in dir_enum)
                    {
                        ret += calc_size(ch_info);
                    }
                }
            }
            else
            {
                if (Wildcard.Match(initial_mask, info.FileName, false))
                {
                    ret += info.Size;
                }
            }
            return(ret);
        }
Ejemplo n.º 2
0
        public ItemColors GetColors(string item_name, ItemState state, ItemCategory category)
        {
            ItemColorState finded = ItemColorState.SystemDefault;
            bool           match  = false;

            //Color c = Color.Black;


            foreach (ItemColorSchemeEntry entry in internal_list)
            {
                if ((entry.Category & category) == category)
                {
                    match = false;
                    foreach (string mask in entry.Masks)
                    {
                        if (Wildcard.Match(mask, item_name, false))
                        {
                            finded = entry.ItemColorState;
                            match  = true;
                            break;
                        }
                    }
                    if (match)
                    {
                        break;
                    }
                }
            }

            switch (state)
            {
            case ItemState.None:
                return(finded.DefaultState);

            case ItemState.Focused | ItemState.Selected:
                return(finded.SelectedFocusedState);

            case ItemState.Focused:
                return(finded.FocusedState);

            case ItemState.Selected:
                return(finded.SelectedState);

            default:
                return(finded.DefaultState);
            }
        }
Ejemplo n.º 3
0
        private void create_delete_list(List <ZipEntry> list, List <string> root_srcs, string mask, ZipFile zf)
        {
            foreach (ZipEntry one_entry in zf)
            {
                if (!Wildcard.Match(mask, one_entry.Name))
                {
                    continue;
                }

                foreach (var root_src in root_srcs)
                {
                    if (one_entry.Name.StartsWith(root_src))
                    {
                        if (!list.Contains(one_entry))
                        {
                            list.Add(one_entry);
                            break;
                        }
                    }
                }
            }
        }
Ejemplo n.º 4
0
        private void copy_one_file(FileInfoEx source_info, string destination_file)
        {
            //check mask
            if (!Wildcard.Match(initial_mask, source_info.FileName))
            {
                return;
            }

            var success = false;

            //now prepare callback buffer
            if (callback_data != IntPtr.Zero)
            {
                Marshal.FreeHGlobal(callback_data);
            }
            callback_data = IOhelper.strings_to_buffer(source_info.FullName, destination_file);

            //prepare uni names
            var source_uni = IOhelper.GetUnicodePath(source_info.FullName);
            var dest_uni   = IOhelper.GetUnicodePath(destination_file);

            //string source_uni = source_info.FullName;
            //string dest_uni = destination_file;

            //and call CopyFileEx first time
            var res = WinApiFS.CopyFileEx
                          (source_uni,
                          dest_uni,
                          copy_progress_routine_delegate_holder,
                          callback_data,
                          IntPtr.Zero,
                          copyFileEx_options);

            if (res == 0)
            {
                var win_err = Marshal.GetLastWin32Error();
                //process win_err and do second take if needed
                var temp_copy_options = copyFileEx_options;
                if (process_CopyFileEx_errors(source_info, destination_file, win_err, ref temp_copy_options))
                {
                    //second take
                    res = WinApiFS.CopyFileEx
                              (source_uni,
                              dest_uni,
                              copy_progress_routine_delegate_holder,
                              callback_data,
                              IntPtr.Zero,
                              temp_copy_options);
                    if (res == 0)
                    {
                        win_err = Marshal.GetLastWin32Error();
                        var win_ex_2 = new Win32Exception(win_err);
                        if (!process_error
                                (string.Format
                                    (Options.GetLiteral(Options.LANG_CANNOT_COPY_0_ARROW_1),
                                    source_info.FullName,
                                    destination_file),
                                win_ex_2))
                        {
                            stop();
                        }
                    }
                    else
                    {
                        success = true;
                    }
                }
            }//end of res==0 brunch
            else
            {
                //copy success
                success = true;
            }

            if (success)
            {
                //update counts
                total_bytes_transferred       += current_file_bytes_transferred;
                current_file_bytes_transferred = 0;
                total_files_copied++;

                //time to notify current panel -> unset selection
                var e_file_done = new ItemEventArs(source_info.FileName);
                OnCopyItemDone(e_file_done);

                //set sec attributes if needed
                if ((options & CopyEngineOptions.CopySecurityAttributes) == CopyEngineOptions.CopySecurityAttributes)
                {
                    try
                    {
                        File.SetAccessControl(destination_file, File.GetAccessControl(source_info.FullName));
                    }
                    catch (Exception ex)
                    {
                        if (!process_error
                                (string.Format
                                    (Options.GetLiteral(Options.LANG_CANNOT_SET_SEC_ATTRIBUTES_0),
                                    destination_file),
                                ex))
                        {
                            stop();
                        }
                    }
                }
            }
        }
        private void delete_entry(FileInfoEx entry)
        {
            /*
             * We first try to delete directory (if set flag DeleteEmptyDirectories)
             * if it is link, DeleteFile finction deletes link, not target, even if target not empty
             * and return.
             * If flag DeleteEmptyDirectories not setted, we check LinkInformation and
             * if it not null (that is entry is soft link) -> return, else
             * remove its contains.
             */

            int win_err = 0;
            int res     = 0;

            if (abort)
            {
                return;
            }

            if (!check_readonly(entry))
            {
                return;
            }

            if (entry.Directory)
            {
                if (!check_readonly(entry))
                {
                    return;
                }

                //first try to delete dir, assume it is empty
                if ((opts & DeleteFileOptions.DeleteEmptyDirectories) == DeleteFileOptions.DeleteEmptyDirectories)
                {
                    res = WinApiFS.RemoveDirectory(entry.FullName);
                    if (res == 0)
                    {
                        win_err = Marshal.GetLastWin32Error();
                        if (win_err == WinApiFS.ERROR_DIR_NOT_EMPTY)
                        {
                            //directory not empty
                            if ((opts & DeleteFileOptions.RecursiveDeleteFiles) == DeleteFileOptions.RecursiveDeleteFiles)
                            {
                                //recursive delete needed
                                FileInfoExEnumerable dir_enum = new FileInfoExEnumerable
                                                                    (Path.Combine(entry.FullName, "*"), false, false, false, true);
                                try
                                {
                                    foreach (FileInfoEx info in dir_enum)
                                    {
                                        delete_entry(info);
                                    }
                                }
                                catch (Exception ex)
                                {
                                    abort = !process_errors
                                                (string.Format(Options.GetLiteral(Options.LANG_CANNOT_DELETE_0), entry.FullName),
                                                ex);
                                }
                            }//end of dir enum
                            //now dir probably empty
                            res = WinApiFS.RemoveDirectory(entry.FullName);
                            if (res == 0)
                            {
                                //not success
                                win_err = Marshal.GetLastWin32Error();
                                if (win_err != WinApiFS.ERROR_DIR_NOT_EMPTY)
                                //supress -> when delete on mask,
                                //dir may be not empty
                                {
                                    abort = !process_errors
                                                (string.Format(Options.GetLiteral(Options.LANG_CANNOT_DELETE_0), entry.FullName),
                                                new Win32Exception(win_err));
                                }
                            }
                        }//end of dir not empty
                        else
                        {
                            abort = !process_errors
                                        (string.Format(Options.GetLiteral(Options.LANG_CANNOT_DELETE_0), entry.FullName),
                                        new Win32Exception(win_err));
                        }
                    }//end of error process
                }
                else
                {
                    //not to delete empty dirs
                    //recursive delete needed (if not link) but directory not will remove
                    //check reparse tag
                    if ((opts & DeleteFileOptions.RecursiveDeleteFiles) == DeleteFileOptions.RecursiveDeleteFiles)
                    {
                        if (entry.LinkInformation == null)
                        {
                            FileInfoExEnumerable dir_enum = new FileInfoExEnumerable
                                                                (Path.Combine(entry.FullName, "*"), false, false, false, true);
                            try
                            {
                                foreach (FileInfoEx info in dir_enum)
                                {
                                    delete_entry(info);
                                }
                            }
                            catch (Exception ex)
                            {
                                abort = !process_errors
                                            (string.Format(Options.GetLiteral(Options.LANG_CANNOT_DELETE_0), entry.FullName),
                                            ex);
                            }
                        }
                    }
                }
            }//end of if(entry.Directory)
            else
            {
                //entry is file

                //check mask
                if (Wildcard.Match(delete_mask, entry.FileName, false))
                {
                    if (check_readonly(entry))
                    {
                        if (main_window != null)
                        {
                            main_window.NotifyLongOperation
                                (string.Format(Options.GetLiteral(Options.LANG_DELETE_NOW_0), entry.FullName),
                                true);
                        }
                        res = WinApiFS.DeleteFile(entry.FullName);
                        if (res == 0)
                        {
                            abort = !process_errors
                                        (string.Format(Options.GetLiteral(Options.LANG_CANNOT_DELETE_0), entry.FullName),
                                        new Win32Exception(Marshal.GetLastWin32Error()));
                        }
                    }
                }
            }
        }
Ejemplo n.º 6
0
        private void move_proc()
        {
            //notify about job begin
            var e_begin = new UpdateProgressArgs();

            e_begin.EnableTotalProgress = false;
            e_begin.Reason = UpdateProgressReason.JobBegin;
            update_progress_safe(e_begin);

            //check source and destination
            //string cur_source = string.Empty;
            //WIN32_FIND_DATA src_data = new WIN32_FIND_DATA();
            //WIN32_FIND_DATA dst_data = new WIN32_FIND_DATA();
            var        dest_info  = new FileInfoEx();
            var        dst_exist  = FileInfoEx.TryGet(innitial_destination, ref dest_info);
            FileInfoEx cur_source = null;

            //bool dst_exist = WinAPiFSwrapper.GetFileInfo(innitial_destination, ref dst_data);

            for (var i = 0; i < initial_source.Count; i++)
            {
                if (AbortJobSafe)
                {
                    break;
                }
                try
                {
                    cur_source = initial_source[i];
                    if (!Wildcard.Match(initial_mask, cur_source.FileName))
                    {
                        continue;
                    }
                    //WinAPiFSwrapper.GetFileInfo(cur_source, ref src_data);
                    if (cur_source.Directory)
                    {
                        //src is directory
                        if (!dst_exist)
                        {
                            //destination not exist - treat as new directory
                            move_one_item(cur_source, innitial_destination);
                            dst_exist = FileInfoEx.TryGet(innitial_destination, ref dest_info);
                        }
                        else
                        {
                            //destination exist
                            if (dest_info.Directory)
                            {
                                //and it is dir -> move cur_source into initial_destination
                                move_one_item(cur_source, Path.Combine(innitial_destination, cur_source.FileName));
                            }
                            else
                            {
                                //move dir to existing file -> not normally
                                AbortJobSafe = !process_errors
                                                   (string.Format
                                                       (Options.GetLiteral(Options.LANG_WRONG_DESTINATION_SOURCE_0_DIRECTORY_DESTINATION_1_FILE),
                                                       cur_source.FileName,
                                                       innitial_destination),
                                                   null);
                                continue;
                            }
                        }
                    }
                    else
                    {
                        //source is file
                        if (!dst_exist)
                        {
                            //dest not exists
                            if (initial_source.Count == 1)
                            {
                                //treat dest as file
                                move_one_item(cur_source, innitial_destination);
                            }
                            else
                            {
                                //treat dest as dir., move source into new dir
                                Directory.CreateDirectory(innitial_destination);
                                dst_exist = FileInfoEx.TryGet(innitial_destination, ref dest_info);
                                move_one_item(cur_source, Path.Combine(innitial_destination, cur_source.FileName));
                            }
                        }
                        else
                        {
                            //dst exists
                            if (dest_info.Directory)
                            {
                                //move file into initial_destination
                                move_one_item(cur_source, Path.Combine(innitial_destination, cur_source.FileName));
                            }
                            else
                            {
                                //dst exists and it is existing file
                                if (initial_source.Count == 1)
                                {
                                    //move file with new name, overwriting existing
                                    move_one_item(cur_source, innitial_destination);
                                }
                                else
                                {
                                    //not mormal: more then one item into one existing file
                                    AbortJobSafe = !process_errors
                                                       (string.Format
                                                           (Options.GetLiteral(Options.LANG_WRONG_DESTINATION_CANNOT_MOVE_MANY_ENTRIES_INTO_ONE_FILE_0),
                                                           innitial_destination),
                                                       null);
                                    continue;
                                }
                            }
                        } //end of dest exists brunch
                    }     //end of src is file
                }         //end of try

                catch (Exception ex)
                {
                    AbortJobSafe = !process_errors
                                       (string.Format
                                           (Options.GetLiteral(Options.LANG_CANNOT_MOVE_0_ARROW_1),
                                           initial_source[i].FullName,
                                           innitial_destination),
                                       ex);
                }
            } //end of for...

            //notify about end of job
            var e_end = new UpdateProgressArgs();

            e_end.Reason = UpdateProgressReason.JobDone;
            update_progress_safe(e_end);
            if (Done != null)
            {
                Done(this, new EventArgs());
            }
        } //end of proc
Ejemplo n.º 7
0
        private void add_to_zip(QueryPanelInfoEventArgs e_current, QueryPanelInfoEventArgs e_other)
        {
            var main_window = (mainForm)Program.MainWindow;

            try
            {
                //this is sync operation
                var zd = (ZipDirectory)e_other.ItemCollection;
                var dl = (DirectoryList)e_current.ItemCollection;

                //show add zip dialog
                var dialog = new ArchiveAddDialog();
                dialog.Text = Options.GetLiteral(Options.LANG_ARCHIVE_ADD);
                dialog.textBoxSourceMask.Text = "*";
                dialog.ArchiveAddOptions      = Options.ArchiveAddOptions;
                if (dialog.ShowDialog() != DialogResult.OK)
                {
                    return;
                }
                var options = dialog.ArchiveAddOptions;
                Options.ArchiveAddOptions = options;

                //notify main window
                main_window.NotifyLongOperation(Options.GetLiteral(Options.LANG_CREATING_FILE_LIST_TO_ARCHIVE), true);

                //create file list to add (only files with path)
                var one_file_list = new string[0];
                var root_paths    = new List <string>();
                var file_list     = new List <string>();
                if (e_current.SelectedIndices.Length == 0)
                {
                    if (dl[e_current.FocusedIndex].FileName == "..")
                    {
                        Messages.ShowMessage(Options.GetLiteral(Options.LANG_WRONG_SOURCE));
                        return;
                    }
                    root_paths.Add(dl[e_current.FocusedIndex].FullName);
                }
                else
                {
                    for (var i = 0; i < e_current.SelectedIndices.Length; i++)
                    {
                        root_paths.Add(dl[e_current.SelectedIndices[i]].FullName);
                    }
                }
                foreach (var one_path in root_paths)
                {
                    if ((Directory.Exists(one_path)) && ((options & ArchiveAddOptions.Recursive) == ArchiveAddOptions.Recursive))
                    {
                        one_file_list = Directory.GetFiles(one_path, dialog.textBoxSourceMask.Text, SearchOption.AllDirectories);
                    }
                    else if ((File.Exists(one_path)) && (Wildcard.Match(dialog.textBoxSourceMask.Text, one_path)))
                    {
                        one_file_list = new string[] { one_path };
                    }
                    else
                    {
                        one_file_list = new string[0];
                    }
                    file_list.AddRange(one_file_list);
                }

                //call ZipFile.BeginUpdate
                var zf = zd.ZipFile;


                //for each source list item:
                //create zip file name from real file path, trimmed from dl current directory
                //call ZipFile.Add
                //notify main window
                var current_dir = dl.DirectoryPath;
                var zip_path    = string.Empty;
                var zip_index   = 0;
                foreach (var one_file in file_list)
                {
                    try
                    {
                        zip_path = one_file.Substring(current_dir.Length);
                        if (zd.CurrentZipDirectory != string.Empty)
                        {
                            zip_path = zd.CurrentZipDirectory + "/" + zip_path;
                        }
                        zip_path = ZipEntry.CleanName(zip_path);

                        //check for exist
                        zip_index = zf.FindEntry(zip_path, true);
                        if (zip_index != -1)
                        {
                            //entry with same name already exists
                            if ((options & ArchiveAddOptions.NeverRewrite) == ArchiveAddOptions.NeverRewrite)
                            {
                                //skip
                                continue;
                            }
                            if ((options & ArchiveAddOptions.RewriteIfSourceNewer) == ArchiveAddOptions.RewriteIfSourceNewer)
                            {
                                //check mod date
                                if (File.GetLastWriteTime(one_file) < zf[zip_index].DateTime)
                                {
                                    continue;
                                }
                                else
                                {
                                    //remove entry
                                    zf.BeginUpdate();
                                    zf.Delete(zf[zip_index]);
                                    zf.CommitUpdate();
                                }
                            }
                            if ((options & ArchiveAddOptions.RewriteAlways) == ArchiveAddOptions.RewriteAlways)
                            {
                                zf.BeginUpdate();
                                zf.Delete(zf[zip_index]);
                                zf.CommitUpdate();
                            }
                        }

                        //open source file
                        main_window.NotifyLongOperation
                            (string.Format
                                (Options.GetLiteral(Options.LANG_ARCHIVING_0_1),
                                one_file,
                                zip_path),
                            true);
                        InternalStreamSource static_source = null;
                        try
                        {
                            //TODO избавиться от флага SaveAttributes
                            //будем всегда сохранять - DONE
                            zf.BeginUpdate();
                            zf.Add(one_file, zip_path);
                            main_window.NotifyLongOperation(Options.GetLiteral(Options.LANG_COMMIT_ARCHIVE_UPDATES), true);
                            zf.CommitUpdate();
                        }
                        catch (Exception ex)
                        {
                            if ((options & ArchiveAddOptions.SupressErrors) != ArchiveAddOptions.SupressErrors)
                            {
                                var err_message = string.Format
                                                      (Options.GetLiteral(Options.LANG_FAILED_ARCHIVE_0),
                                                      zip_path);
                                if (Messages.ShowExceptionContinue(ex, err_message))
                                {
                                    continue;
                                }
                                else
                                {
                                    break;
                                }
                            }
                        }
                        finally
                        {
                            if (static_source != null)
                            {
                                static_source.Close();
                            }
                        }
                    }
                    catch (Exception ex)
                    {
                        if ((options & ArchiveAddOptions.SupressErrors) != ArchiveAddOptions.SupressErrors)
                        {
                            var err_message = string.Format
                                                  (Options.GetLiteral(Options.LANG_FAILED_ARCHIVE_0),
                                                  zip_path);
                            if (Messages.ShowExceptionContinue(ex, err_message))
                            {
                                continue;
                            }
                            else
                            {
                                break;
                            }
                        }
                    }
                }//end of foreach

                //Refill zd
                zd.Refill();

                //notify main window when done
            }
            catch (Exception ex)
            {
                Messages.ShowException(ex);
            }
            finally
            {
                main_window.NotifyLongOperation("Done", false);
            }
        }
Ejemplo n.º 8
0
        public override int[] FindItems()
        {
            //show filter and find within current collection
            DirectoryListFilterDialog filter_dialog = new DirectoryListFilterDialog();

            if (filter_dialog.ShowDialog() == System.Windows.Forms.DialogResult.OK)
            {
                DirectoryListFilter filter       = filter_dialog.DirectoryListFilter;
                List <int>          retList      = new List <int>();
                WIN32_FIND_DATA     current_data = new WIN32_FIND_DATA();
                bool pass = true;

                for (int i = 0; i < ItemCount; i++)
                {
                    current_data = internal_list.Keys[i];
                    pass         = true;
                    for (int j = 0; j < filter.Masks.Length; j++)
                    {
                        if (!Wildcard.Match(filter.Masks[j], current_data.cFileName, false))
                        {
                            pass = false;
                            break;
                        }
                    }

                    if (!pass)
                    {
                        continue;
                    }

                    if ((!filter.IgnoreFileAttributes) && ((current_data.dwFileAttributes & filter.FileAttributes) == 0))
                    {
                        pass = false;
                        continue;
                    }

                    if (!filter.IgnoreSize)
                    {
                        switch (filter.FilterSizeCriteria)
                        {
                        case FilterSizeCriteria.Between:
                            if ((current_data.FileSize < (ulong)filter.SizeMinimum) || (current_data.FileSize > (ulong)filter.SizeMaximum))
                            {
                                pass = false;
                                continue;
                            }
                            break;

                        case FilterSizeCriteria.Equal:
                            if (current_data.FileSize != (ulong)filter.SizeMinimum)
                            {
                                pass = false;
                                continue;
                            }
                            break;

                        case FilterSizeCriteria.Greater:
                            if (current_data.FileSize <= (ulong)filter.SizeMinimum)
                            {
                                pass = false;
                                continue;
                            }
                            break;

                        case FilterSizeCriteria.GreaterOrEqual:
                            if (current_data.FileSize < (ulong)filter.SizeMinimum)
                            {
                                pass = false;
                                continue;
                            }
                            break;

                        case FilterSizeCriteria.Less:
                            if (current_data.FileSize >= (ulong)filter.SizeMinimum)
                            {
                                pass = false;
                                continue;
                            }
                            break;

                        case FilterSizeCriteria.LessOrEqual:
                            if (current_data.FileSize > (ulong)filter.SizeMinimum)
                            {
                                pass = false;
                                continue;
                            }
                            break;
                        }
                    }

                    if (!filter.IgnoreTimeAccess)
                    {
                        if ((current_data.ftLastAccessTime < filter.AccessBegin.ToFileTime()) || (current_data.ftLastAccessTime > filter.AccessEnd.ToFileTime()))
                        {
                            pass = false;
                            continue;
                        }
                    }

                    if (!filter.IgnoreTimeCreate)
                    {
                        if ((current_data.ftCreationTime < filter.CreateBegin.ToFileTime()) || (current_data.ftCreationTime > filter.CreateEnd.ToFileTime()))
                        {
                            pass = false;
                            continue;
                        }
                    }

                    if (!filter.IgnoreTimeModification)
                    {
                        if ((current_data.ftLastWriteTime < filter.ModificationBegin.ToFileTime()) || (current_data.ftLastWriteTime > filter.ModificationEnd.ToFileTime()))
                        {
                            pass = false;
                            continue;
                        }
                    }

                    //pass=true
                    if (pass)
                    {
                        retList.Add(i);
                    }
                }
                return(retList.ToArray());
            }
            else
            {
                return(new int[] { });
            }
        }