tesses-backup/TessesDedup/Backup.cs

117 lines
3.6 KiB
C#

using System;
using System.Collections.Generic;
using System.IO;
using Newtonsoft.Json;
using Newtonsoft.Json.Converters;
using Newtonsoft.Json.Serialization;
using Tesses.VirtualFilesystem;
namespace TessesDedup
{
public class Backup
{
[JsonProperty("id")]
public long Id {get;set;}
[JsonProperty("account_id")]
public long AccountId {get;set;}
[JsonProperty("root")]
public FilesystemEntry Root {get;set;}
[JsonProperty("device_name")]
public string DeviceName {get;set;}="";
[JsonProperty("tag")]
public string Tag {get;set;}="";
[JsonProperty("creation_date")]
public DateTime CreationDate {get;set;}
public Backup WithoutRoot()
{
Backup b=new Backup();
b.AccountId = AccountId;
b.Id = Id;
b.CreationDate = CreationDate;
b.DeviceName = DeviceName;
b.Tag = Tag;
b.Root=null;
return b;
}
public Backup WithoutHashes()
{
Backup b=new Backup();
b.AccountId = AccountId;
b.Id = Id;
b.CreationDate = CreationDate;
b.DeviceName = DeviceName;
b.Tag = Tag;
b.Root=Root.WithoutHashes();
return b;
}
public FilesystemEntry GetEntryFromPath(UnixPath path)
{
if(path.Path == "/") return Root;
var gefp=GetEntryFromPath(path.Parent);
if(gefp.Type != FilesystemEntryType.Dir) throw new DirectoryNotFoundException(path.Parent.Path);
foreach(var item in gefp.Entries)
{
if(item.Name == path.Name)
{
switch(item.Type)
{
case FilesystemEntryType.Dir:
case FilesystemEntryType.File:
return item;
case FilesystemEntryType.Symlink:
{
if(item.PointsTo.StartsWith("/"))
{
UnixPath _path = path.Parent;
foreach(var _item in item.PointsTo.Split('/'))
{
if(_item == "..")
{
_path = _path.Parent;
}
else if(_item != ".")
{
_path /= _item;
}
}
return GetEntryFromPath(_path);
}
else
{
UnixPath _path = Special.Root;
foreach(var _item in item.PointsTo.Split('/'))
{
if(_item == "..")
{
_path = _path.Parent;
}
else if(_item != ".")
{
_path /= _item;
}
}
return GetEntryFromPath(_path);
}
}
}
}
}
throw new FileNotFoundException(path.Path);
}
}
}