private static void RotateCacheFiles([NotNull] FileInfo cacheFile)
        {
            if (cacheFile.Exists)
            {
                double hours = 999.9;
                if (File.Exists(cacheFile.FullName + ".0"))
                {
                    // see when the last rotate was, and only rotate if its been at least an hour since the last save
                    DateTime dt = File.GetLastWriteTime(cacheFile.FullName + ".0");
                    hours = DateTime.Now.Subtract(dt).TotalHours;
                }

                if (hours >= 24.0) // rotate the save file daily
                {
                    for (int i = 8; i >= 0; i--)
                    {
                        string fn = cacheFile.FullName + "." + i;
                        if (File.Exists(fn))
                        {
                            string fn2 = cacheFile.FullName + "." + (i + 1);
                            if (File.Exists(fn2))
                            {
                                File.Delete(fn2);
                            }

                            File.Move(fn, fn2);
                        }
                    }

                    File.Copy(cacheFile.FullName, cacheFile.FullName + ".0");
                }
            }
        }
        public string ScrapeURLToFile(string url, bool force_download = false, Dictionary <string, string> additional_headers = null)
        {
            string cache_key = StreamMD5.FromText(url);
            string directory = Path.GetFullPath(Path.Combine(base_directory, cache_key.Substring(0, 2)));
            string filename  = Path.GetFullPath(Path.Combine(directory, cache_key));

            if (!File.Exists(filename) || force_download)
            {
                Utilities.Files.DirectoryTools.CreateDirectory(directory);

                // Crude throttle
                if (true)
                {
                    while (DateTime.UtcNow.Subtract(last_scrape_time).TotalMilliseconds < throttle_ms)
                    {
                        Thread.Sleep(50);
                    }
                    last_scrape_time = DateTime.UtcNow;
                }

                Logging.Info("Downloading from {0}", url);
                using (WebClient client = new WebClient())
                {
                    if (null != additional_headers)
                    {
                        foreach (var pair in additional_headers)
                        {
                            client.Headers.Add(pair.Key, pair.Value);
                        }
                    }
                    if (!String.IsNullOrEmpty(userAgent))
                    {
                        client.Headers.Add("User-agent", userAgent);
                    }

                    string temp_filename = filename + ".tmp";
                    try
                    {
                        client.DownloadFile(url, temp_filename);
                    }
                    catch (WebException ex)
                    {
                        File.WriteAllText(temp_filename, ex.ToString());
                    }

                    File.Delete(filename);
                    File.Move(temp_filename, filename);
                }

                string filename_manifest = Path.GetFullPath(Path.Combine(base_directory, @"manifest.txt"));
                string manifest_line     = String.Format("{0}\t{1}", cache_key, url);
                using (StreamWriter sw = File.AppendText(filename_manifest))
                {
                    sw.WriteLine(manifest_line);
                }
            }

            return(filename);
        }
Exemple #3
0
        // ------------------------------------------------------------------------------------------------------------------------------------------------------------------
        // --- The equivalent protobuf versions
        // ------------------------------------------------------------------------------------------------------------------------------------------------------------------

#if !HAS_NO_PROTOBUF
        public static void ProtoSave <T>(string filename, T animal_to_save)
        {
            string redundant_filename = filename + REDUNDANT;

            ProtoSave_NotRedundant <T>(redundant_filename, animal_to_save);
            File.Delete(filename);
            File.Move(redundant_filename, filename);
        }
Exemple #4
0
        public static void SaveRedundant(string filename, object animal_to_save)
        {
            string redundant_filename = filename + REDUNDANT;

            Save(redundant_filename, animal_to_save);
            File.Delete(filename);
            File.Move(redundant_filename, filename);
        }
Exemple #5
0
        // ------------------------------------------------------------------------------------------------------------------------------------------------------------------
        // --- The equivalent text versions
        // ------------------------------------------------------------------------------------------------------------------------------------------------------------------

        public static void TextSave(string filename, string animal_to_save)
        {
            string redundant_filename = filename + REDUNDANT;

            TextSave_NotRedundant(redundant_filename, animal_to_save);
            File.Delete(filename);
            File.Move(redundant_filename, filename);
        }
Exemple #6
0
        private void BackupFile(string file)
        {
            var path = Path.Combine(this._basePath, "BannerLordLauncher Backups");

            try
            {
                if (!Directory.Exists(path))
                {
                    Directory.CreateDirectory(path);
                }
            }
            catch (Exception e)
            {
                this.Log().Error(e);
                return;
            }

            if (!File.Exists(file))
            {
                return;
            }
            var ext     = Path.GetExtension(file);
            var i       = 0;
            var newFile = Path.ChangeExtension(file, $"{ext}.{i:D3}");

            Debug.Assert(newFile != null, nameof(newFile) + " != null");
            newFile = Path.Combine(path, Path.GetFileName(newFile));
            while (File.Exists(newFile))
            {
                i++;
                newFile = Path.ChangeExtension(file, $"{ext}.{i:D3}");
                Debug.Assert(newFile != null, nameof(newFile) + " != null");
                newFile = Path.Combine(path, Path.GetFileName(newFile));
            }

            if (i > 999)
            {
                return;
            }
            try
            {
                Debug.Assert(file != null, nameof(file) + " != null");
                File.Move(file, newFile);
            }
            catch (Exception e)
            {
                this.Log().Error(e);
            }
        }
        public static bool MoveSafelyWithOverwriting(string source, string target)
        {
            if (File.Exists(source))
            {
                if (File.Exists(target))
                {
                    File.Delete(target);
                }

                File.Move(source, target);
                return(true);
            }

            return(false);
        }
Exemple #8
0
        private static void MoveHomeLibraryToGuest()
        {
            try
            {
                string OLD_BASE_PATH = Path.GetFullPath(Path.Combine(Environment.GetFolderPath(Environment.SpecialFolder.ApplicationData), @"Quantisle/Qiqqa"));
                if (Directory.Exists(OLD_BASE_PATH))
                {
                    string NEW_BASE_PATH = Path.GetFullPath(Path.Combine(Environment.GetFolderPath(Environment.SpecialFolder.ApplicationData), @"Quantisle/Qiqqa/Guest"));

                    // Check that we have the Guest directory
                    if (!Directory.Exists(NEW_BASE_PATH))
                    {
                        Logging.Info("Guest directory does not exist, so creating it");
                        Directory.CreateDirectory(NEW_BASE_PATH);
                    }

                    // Move the documents if we have them
                    string old_documents_path = Path.GetFullPath(Path.Combine(OLD_BASE_PATH, @"documents"));
                    if (Directory.Exists(old_documents_path))
                    {
                        Logging.Info("Old style documents path exists, so moving it");
                        string new_documents_path = Path.GetFullPath(Path.Combine(NEW_BASE_PATH, @"documents"));
                        Directory.Move(old_documents_path, new_documents_path);
                    }

                    // Move the application files if we have them
                    string[] application_filenames = new string[]
                    {
                        "Qiqqa.configuration", "Qiqqa.most_recently_read", "Qiqqa.utilisation"
                    };

                    foreach (string application_filename in application_filenames)
                    {
                        string old_filename = OLD_BASE_PATH + application_filename;
                        if (File.Exists(old_filename))
                        {
                            Logging.Info("Old style filename exists, so moving it ({0})", old_filename);
                            string new_filename = Path.GetFullPath(Path.Combine(NEW_BASE_PATH, application_filename));
                            File.Move(old_filename, new_filename);
                        }
                    }
                }
            }
            catch (Exception ex)
            {
                Logging.Error(ex, "Upgrade V003-to-V004 failure");
            }
        }
        public void SyncToDisk()
        {
            if (!_disableDiskCache)
            {
                Utils.Status("Syncing VFS Cache");
                lock (this)
                {
                    if (!_isDirty)
                    {
                        return;
                    }
                    try
                    {
                        _isSyncing = true;

                        if (File.Exists("vfs_cache.bin_new"))
                        {
                            File.Delete("vfs_cache.bin_new");
                        }

                        using (var fs = File.OpenWrite("vfs_cache.bin_new"))
                            using (var bw = new BinaryWriter(fs))
                            {
                                Utils.Log($"Syncing VFS to Disk: {_files.Count} entries");
                                foreach (var f in _files.Values)
                                {
                                    f.Write(bw);
                                }
                            }

                        if (File.Exists("vfs_cache.bin"))
                        {
                            File.Delete("vfs_cache.bin");
                        }

                        File.Move("vfs_cache.bin_new", "vfs_cache.bin");
                        _isDirty = false;
                    }
                    finally
                    {
                        _isSyncing = false;
                    }
                }
            }
        }
Exemple #10
0
        public void SyncToDisk()
        {
            if (!_disableDiskCache)
            {
                Utils.Status("Syncing VFS Cache");
                lock (this)
                {
                    try
                    {
                        _isSyncing = true;

                        if (File.Exists("vfs_cache.bin_new"))
                        {
                            File.Delete("vfs_cache.bin_new");
                        }

                        using (var fs = File.OpenWrite("vfs_cache.bin_new"))
                            using (var bw = new BinaryWriter(fs))
                            {
                                bw.Write(Encoding.ASCII.GetBytes(Magic));
                                bw.Write(FileVersion);

                                Utils.Log($"Syncing VFS to Disk: {_files.Count} entries");
                                foreach (var f in _files.Values)
                                {
                                    f.Write(bw);
                                }
                            }

                        if (File.Exists("vfs_cache.bin"))
                        {
                            File.Delete("vfs_cache.bin");
                        }

                        File.Move("vfs_cache.bin_new", "vfs_cache.bin");
                    }
                    finally
                    {
                        _isSyncing = false;
                    }
                }
            }
        }
Exemple #11
0
 public static void Rotate(string filenameBase)
 {
     if (File.Exists(filenameBase))
     {
         for (int i = 8; i >= 0; i--)
         {
             string fn = filenameBase + "." + i;
             if (File.Exists(fn))
             {
                 string fn2 = filenameBase + "." + (i + 1);
                 if (File.Exists(fn2))
                 {
                     File.Delete(fn2);
                 }
                 File.Move(fn, fn2);
             }
         }
         File.Copy(filenameBase, filenameBase + ".0");
     }
 }
Exemple #12
0
 public static object LoadRedundant(string filename)
 {
     try
     {
         return(Load(filename));
     }
     catch
     {
         // Check if there is a redundant file to fall back on
         string redundant_filename = filename + REDUNDANT;
         if (File.Exists(redundant_filename))
         {
             File.Delete(filename);
             File.Move(redundant_filename, filename);
             return(Load(filename));
         }
         else
         {
             throw;
         }
     }
 }
Exemple #13
0
 public static T ProtoLoad <T>(string filename)
 {
     try
     {
         return(ProtoLoad_NotRedundant <T>(filename));
     }
     catch (Exception ex)
     {
         // Check if there is a redundant file to fall back on
         string redundant_filename = filename + REDUNDANT;
         if (File.Exists(redundant_filename))
         {
             File.Delete(filename);
             File.Move(redundant_filename, filename);
             return(ProtoLoad_NotRedundant <T>(filename));
         }
         else
         {
             throw ex;
         }
     }
 }
Exemple #14
0
        public static object LoadRedundant(string filename)
        {
            try
            {
                return(Load(filename));
            }
            catch (Exception ex)
            {
                Logging.Error(ex, $"LoadRedundant: failed to load '{filename}'. Checking if there's a redundant copy.");

                // Check if there is a redundant file to fall back on
                string redundant_filename = filename + REDUNDANT;
                if (File.Exists(redundant_filename))
                {
                    File.Delete(filename);
                    File.Move(redundant_filename, filename);
                    return(Load(filename));
                }
                else
                {
                    throw;
                }
            }
        }
Exemple #15
0
 public override void Move(string sourceFileName, string destFileName)
 {
     AfsFile.Move(sourceFileName, destFileName);
 }