/// <summary> /// 验证文件是否一致 /// </summary> /// <param name="origin">源文件</param> /// <param name="destination">目标文件</param> /// <param name="STACHK_flag">验证标识</param> /// <returns></returns> private bool _File_Validating(string origin, string destination, int STACHK_flag) { var ofi = new FileInfo(origin); var dfi = new FileInfo(destination); //todo: 文件时间的容差范围(1s) if ((STACHK_flag & FLAG_STACHK_CREATED_TIME) != 0) { if (!Date_Time_Equation(ofi.CreationTime, dfi.CreationTime, 1000)) { return(false); } } if ((STACHK_flag & FLAG_STACHK_LAST_MODIFIED_TIME) != 0) { if (!Date_Time_Equation(ofi.CreationTime, dfi.CreationTime, 2000)) { return(false); } } if ((STACHK_flag & FLAG_STACHK_LAST_ACCESSED_TIME) != 0) { if (!Date_Time_Equation(ofi.LastAccessTime, dfi.LastAccessTime, 86400000)) { return(false); } } if ((STACHK_flag & FLAG_STACHK_LENGTH) != 0) { if (ofi.Length != dfi.Length) { return(false); } } if ((STACHK_flag & FLAG_STACHK_MD5) != 0) { byte[] omd5 = _Get_File_MD5(ofi.FullName), dmd5 = _Get_File_MD5(dfi.FullName); if (omd5 == null || dmd5 == null) { throw new InvalidDataException("计算文件MD5值出错"); } if (Others.Hex(omd5) != Others.Hex(dmd5)) { return(false); } } return(true); }
//创建仓库(支持直接调用) public void Create_repository(string root_location) { //检查文件夹 try { //文件夹不存在,创建 if (!Directory.Exists(root_location)) { Directory.CreateDirectory(root_location); } else { //文件夹存在,选择清空文件夹或是选择其他路径 bool result = false; if (Ask_For_Delete_Directory != null) { Ask_For_Delete_Directory(root_location, ref result); } else { throw new InvalidOperationException("文件夹已存在"); } //再次询问 if (result) { Ask_For_Delete_Directory(root_location, ref result); } else { throw new InvalidOperationException("操作已取消"); } if (result) { //清空目标文件夹 Delete_Directory(root_location, false, true); Delete_Directory(root_location, true, false); } } } catch (Exception) { _stat_failed = true; throw new InvalidDataException("创建文件夹失败,是否为非法路径?或为权限不足"); } //创建文件架构 try { Directory.CreateDirectory(root_location + "/index"); Directory.CreateDirectory(root_location + "/backup"); Directory.CreateDirectory(root_location + "/data"); for (int i = 0; i < 256; i++) { Directory.CreateDirectory(root_location + "/data/" + Others.Hex(new byte[] { (byte)i })); } Directory.CreateDirectory(root_location + "/local_caches"); FileStream fs = new FileStream(root_location + "/说明README.txt", FileMode.Create, FileAccess.Write); using (StreamWriter sw = new StreamWriter(fs)) { sw.Write("本目录由软件自动管理,请勿移动或删除任何一个文件夹,否则软件出错及数据丢失后果自负"); } fs.Close(); fs = new FileStream(root_location + "/global_settings", FileMode.Create, FileAccess.Write); var root_json = new JObject(); root_json.Add("name", "repository"); _repo_name = "repository"; root_json.Add("description", ""); using (var sw = new StreamWriter(fs)) { sw.Write(JsonConvert.SerializeObject(root_json)); } fs.Close(); File.Create(root_location + "/sign").Close(); //创建sql文件 Create_sql_table(root_location); } catch (Exception) { _stat_failed = true; throw; } }