Example #1
0
        /// <summary>
        /// Checks the given file path against the filter.
        /// </summary>
        /// <param name="filePath">The file path to test.</param>
        /// <returns>True if the filter matches the <paramref name="filePath"/>; otherwise false.</returns>
        public bool IsMatch(string filePath)
        {
            if (log.IsDebugEnabled)
            {
                log.DebugFormat("Performing IsMatch on `{0}` for filePath `{1}`.", this, filePath);
            }

            var s = FileFilterHelper.SanitizeFilePath(filePath);

            return(_regex.IsMatch(s));
        }
Example #2
0
        /// <summary>
        /// Runs the clean-up routines.
        /// </summary>
        void Cleanup()
        {
            State = UpdateClientState.CleaningUp;

            // Delete files from the update process
            try
            {
                if (_versionFileList != null)
                {
                    if (Directory.Exists(Settings.TargetPath))
                    {
                        var ffc = FileFilterHelper.CreateCollection(_versionFileList.Filters);
                        if (ffc.Count > 0)
                        {
                            var pathTrimLen = Settings.TargetPath.Length;
                            if (!Settings.TargetPath.EndsWith(Path.DirectorySeparatorChar.ToString()) &&
                                !Settings.TargetPath.EndsWith(Path.AltDirectorySeparatorChar.ToString()))
                            {
                                pathTrimLen++;
                            }

                            var files = Directory.GetFiles(Settings.TargetPath);
                            foreach (var f in files)
                            {
                                var relativePath = f.Substring(pathTrimLen);

                                try
                                {
                                    if (_versionFileList.ContainsFile(relativePath))
                                    {
                                        continue;
                                    }

                                    if (ffc.IsMatch("\\" + relativePath))
                                    {
                                        if (log.IsDebugEnabled)
                                        {
                                            log.DebugFormat(
                                                "Skipping deleting outdated client file `{0}` - matches one or more delete skip filters.",
                                                f);
                                        }
                                        continue;
                                    }

                                    if (log.IsInfoEnabled)
                                    {
                                        log.InfoFormat("Deleting outdated client file: {0}", f);
                                    }

                                    File.Delete(f);
                                }
                                catch (Exception ex)
                                {
                                    const string errmsg =
                                        "Unexpected error while checking if file `{0}` should be deleted. Exception: {1}";
                                    if (log.IsErrorEnabled)
                                    {
                                        log.ErrorFormat(errmsg, f, ex);
                                    }
                                    Debug.Fail(string.Format(errmsg, f, ex));
                                }
                            }
                        }
                    }
                }
                else
                {
                    const string errmsg = "_versionFileList was null, but wasn't expected to be.";
                    if (log.IsErrorEnabled)
                    {
                        log.Error(errmsg);
                    }
                    Debug.Fail(errmsg);
                }
            }
            catch (Exception ex)
            {
                const string errmsg = "Failed to delete old files. Exception: {0}";
                if (log.IsErrorEnabled)
                {
                    log.ErrorFormat(errmsg, ex);
                }
                Debug.Fail(string.Format(errmsg, ex));
            }

            // Delete temp files
            try
            {
                if (Directory.Exists(Settings.TempPath))
                {
                    var tempFiles = Directory.GetFiles(Settings.TempPath);

                    // Delete each file individually first, even though Directory.Delete can do this, just to make sure that
                    // we delete as much as we possibly can if there are errors
                    foreach (var f in tempFiles)
                    {
                        try
                        {
                            if (log.IsDebugEnabled)
                            {
                                log.DebugFormat("Deleting temp file `{0}`.", f);
                            }
                            File.Delete(f);
                        }
                        catch (Exception ex)
                        {
                            const string errmsg = "Failed to delete temporary file `{0}`. Exception: {1}";
                            Debug.Fail(string.Format(errmsg, f, ex));
                        }
                    }

                    // Delete the directory
                    if (log.IsDebugEnabled)
                    {
                        log.DebugFormat("Deleting directory `{0}`.", Settings.TempPath);
                    }
                    Directory.Delete(Settings.TempPath, true);
                }
            }
            catch (Exception ex)
            {
                const string errmsg = "Failed to delete temp files from path `{0}`. Exception: {1}";
                if (log.IsErrorEnabled)
                {
                    log.ErrorFormat(errmsg, Settings.TempPath, ex);
                }
                Debug.Fail(string.Format(errmsg, Settings.TempPath, ex));
            }
        }