public static bool insertDirectory(SQLiteConnection conn, DirectoryStatus newStatus, String filename, String path, DateTime lastModTime) { try { using (SQLiteCommand cmd = conn.CreateCommand()) { cmd.CommandText = @"insert into snapshots (user_id,creation_time,path,filename,last_mod_time,directory) values" + " (@user, @creationTime, @path, @filename, @lastModTime, @directory);"; cmd.Parameters.AddWithValue("@user", newStatus.Username); cmd.Parameters.AddWithValue("@creationTime", newStatus.CreationTime.ToString(date_format)); cmd.Parameters.AddWithValue("@path", path + "\\"); cmd.Parameters.AddWithValue("@filename", filename); cmd.Parameters.AddWithValue("@lastModTime", lastModTime.ToString(date_format)); cmd.Parameters.AddWithValue("@directory", true); cmd.ExecuteNonQuery(); } //add directory DirectoryFile dirFile = new DirectoryFile(path, filename, newStatus.Username, lastModTime, true); newStatus.Files.Add(dirFile.Fullname, dirFile); return true; } catch (SQLiteException) { return false;} }
public static DirectoryStatus getAllSnapshots(String username) { DirectoryStatus ds = new DirectoryStatus(); try { using (var con = new SQLiteConnection(DBmanager.connectionString)) { con.Open(); using (var cmd = con.CreateCommand()) { cmd.CommandText = @"select distinct path,creation_time from snapshots where user_id=@id"; cmd.Parameters.AddWithValue("@id", username); using (var reader = cmd.ExecuteReader()) { while (reader.Read()) { DirectoryFile file = new DirectoryFile(); file.UserId = username; file.Directory = true; file.Path = (String)reader["path"]; file.Path = file.Path.Substring(0, file.Path.LastIndexOf('\\')); DateTime prefix = (DateTime)reader["creation_time"]; file.Fullname = (String)prefix.ToString("dd/MM/yyyyTHH:ss") + "_" + (String)reader["path"]; file.LastModificationTime = (DateTime)reader["creation_time"]; ds.Files.Add(file.Fullname, file); } } } } return ds; } catch (SQLiteException) { return null; } }
public static DirectoryStatus getSnapshotFilesOfDirectory(String directory, String username, DateTime creationTime) { DirectoryStatus ds = new DirectoryStatus(); ds.FolderPath = directory; ds.Username = username; try { using (var con = new SQLiteConnection(connectionString)) { con.Open(); using (var cmd = con.CreateCommand()) { int len = directory.Length; cmd.CommandText = @"select * from snapshots where user_id = @id" + " and path = @dir and creation_time = @date;"; cmd.Parameters.AddWithValue("@id", username); cmd.Parameters.AddWithValue("@dir", directory + "\\"); cmd.Parameters.AddWithValue("@date", creationTime.ToString(date_format)); using (var reader = cmd.ExecuteReader()) { while (reader.Read()) { DirectoryFile file = new DirectoryFile(); file.UserId = username; file.Directory = (Boolean)reader["directory"]; file.Path = (String)reader["path"]; file.Path = file.Path.Substring(0, file.Path.LastIndexOf('\\')); file.Filename = (String)reader["filename"]; file.Deleted = (Boolean)reader["deleted"]; file.LastModificationTime = (DateTime)reader["last_mod_time"]; if (!file.Directory) { file.Id = (Int64)reader["file_id"]; file.Checksum = (String)reader["checksum"]; } file.Fullname = Path.Combine(file.Path, file.Filename); ds.Files.Add(file.Fullname, file); } } } } return ds; } catch (SQLiteException) { return ds; } }
private void OnRenamed(object source, RenamedEventArgs e) { try { //if from .doc -> ~ ignore //if from ~->.doc changed if (Directory.Exists(e.FullPath)) { // a directory is renamed lock (this) { client.renameDirectory(e.OldFullPath, e.FullPath); } } if (File.Exists(e.FullPath)) { // a file is renamed FileInfo fi_old = new FileInfo(e.OldFullPath); FileInfo fi_new = new FileInfo(e.FullPath); if ((fi_old.Name[0].Equals('~') && !fi_new.Name[0].Equals('~')) || (fi_old.Extension.Equals(".tmp") && !fi_new.Name[0].Equals('~')) || (fi_old.Extension.Equals("") && !fi_new.Name[0].Equals('~')) || (fi_old.Name[0].Equals('~') && !fi_new.Extension.Equals("tmp")) || (fi_old.Extension.Equals(".tmp") && !fi_new.Extension.Equals("tmp")) || (fi_old.Extension.Equals("") && !fi_new.Extension.Equals("tmp")) || (fi_old.Name[0].Equals('~') && !fi_new.Extension.Equals("")) || (fi_old.Extension.Equals(".tmp") && !fi_new.Extension.Equals("")) || (fi_old.Extension.Equals("") && !fi_new.Extension.Equals(""))) { //file Microsoft changed lock (this) { DirectoryFile file = new DirectoryFile(); file.Filename = fi_new.Name; file.Fullname = fi_new.FullName; file.Path = fi_new.DirectoryName; file.UserId = client.UserId; file.LastModificationTime = fi_new.LastWriteTime; file.Length = fi_new.Length; client.updateFile(file, true); } } else { if (!fi_new.Name[0].Equals('~') && !fi_new.Name[0].Equals('$') && !fi_new.Name[0].Equals('.') && !fi_new.Extension.Equals(".tmp") && !fi_new.Extension.Equals(".TMP") && !fi_new.Extension.Equals("")) { lock (this) { client.renameFile(fi_old.Name, fi_new.Name, fi_old.DirectoryName); } } } } } catch (Exception) { return; } }
public int getAllSnapshots(DirectoryStatus ds) { try { Socket s = tcpClient.Client; byte[] command = BitConverter.GetBytes((UInt32)Networking.CONNECTION_CODES.GET_SNAP); s.Send(command); byte[] recvBuf = Networking.my_recv(4, s); if (recvBuf == null) return -1; Int32 snapToRecv = BitConverter.ToInt32(recvBuf, 0); for (int i = 0; i < snapToRecv; i++) { DirectoryFile file = new DirectoryFile(); file.Directory = true; file.UserId = userId; recvBuf = Networking.my_recv(4, s); if (recvBuf == null) return -1; byte[] encryptedData = Networking.my_recv(BitConverter.ToInt32(recvBuf, 0), s); if (encryptedData == null) return -1; String path = Encoding.UTF8.GetString(Security.AESDecrypt(aesKey, encryptedData)); recvBuf = Networking.my_recv(8, s); if (recvBuf == null) return -1; DateTime lastModTime = DateTime.FromBinary(BitConverter.ToInt64(recvBuf, 0)); file.Path = path; file.LastModificationTime = lastModTime; file.Fullname = lastModTime.ToString("dd/MM/yyyyTHH:ss") + "_" + path; ds.Files.Add(file.Fullname, file); } return snapToRecv; } catch (SocketException) { return -1; } }
public static DirectoryStatus getPreviousVersion(String path, String username, DateTime date) { DirectoryStatus ds = new DirectoryStatus(); ds.FolderPath = path; ds.Username = username; try { using (var con = new SQLiteConnection(connectionString)) { con.Open(); using (var cmd = con.CreateCommand()) { cmd.CommandText = @"select path,filename,file_id,last_mod_time,content" + " from files where user_id = @id" + " and path = @dir and filename = @filename" + " and last_mod_time < @date;"; cmd.Parameters.AddWithValue("@id", username); cmd.Parameters.AddWithValue("@dir", Path.GetDirectoryName(path) + "\\"); cmd.Parameters.AddWithValue("@filename", Path.GetFileName(path)); cmd.Parameters.AddWithValue("@date", date.ToString(date_format)); using (var reader = cmd.ExecuteReader()) { while (reader.Read()) { DirectoryFile file = new DirectoryFile(); file.UserId = username; file.Path = (String)reader["path"]; file.Path = file.Path.Substring(0, file.Path.LastIndexOf('\\')); file.Filename = (String)reader["filename"]; file.Id = (Int64)reader["file_id"]; file.Checksum = Security.CalculateMD5Hash((byte[])reader["content"]); file.LastModificationTime = (DateTime)reader["last_mod_time"]; file.Deleted = true; file.Fullname = Path.Combine(file.Path, file.Filename); ds.Files.Add(file.NameVersion, file); } } } } return ds; } catch (SQLiteException) { return ds; } }
private void OnCreated(object source, FileSystemEventArgs e) { try { if (Directory.Exists(e.FullPath)) { //directory lock (this) { DirectoryFile file = new DirectoryFile(); file.Directory = true; file.Filename = Path.GetFileName(e.FullPath); file.Fullname = e.FullPath; file.LastModificationTime = Directory.GetCreationTime(e.FullPath); file.Path = Path.GetDirectoryName(e.FullPath); file.UserId = client.UserId; client.addFile(file, true); } } else { //file lock (this) { FileInfo info = new FileInfo(e.FullPath); if (!info.Name[0].Equals('~') && !info.Name[0].Equals('$') && !info.Name[0].Equals('.') && !info.Extension.Equals(".tmp") && !info.Extension.Equals(".TMP") && !info.Extension.Equals("")) { DirectoryFile file = new DirectoryFile(); file.Directory = false; file.Filename = info.Name; file.Fullname = info.FullName; file.LastModificationTime = info.LastWriteTime; file.Path = info.DirectoryName; file.UserId = client.UserId; client.addFile(file, true); } } } } catch (Exception) { return; } }
public int getPreviousVersions(DirectoryStatus dir, String path) { try { Socket s = tcpClient.Client; byte[] command = BitConverter.GetBytes((UInt32)Networking.CONNECTION_CODES.PREV); s.Send(command); byte[] buf = Security.AESEncrypt(aesKey, Encoding.UTF8.GetBytes(path)); s.Send(BitConverter.GetBytes(buf.Length)); s.Send(buf); long date = File.GetLastWriteTime(path).ToBinary(); buf = BitConverter.GetBytes(date); s.Send(buf); command = Networking.my_recv(4, tcpClient.Client); if (command != null && ( ((Networking.CONNECTION_CODES)BitConverter.ToUInt32(command, 0) == Networking.CONNECTION_CODES.OK))) { byte[] recvBuf = Networking.my_recv(4, s); if (recvBuf == null) return -1; Int32 filesInfoToRecv = BitConverter.ToInt32(recvBuf, 0); for (int i = 0; i < filesInfoToRecv; i++) { DirectoryFile file = new DirectoryFile(); recvBuf = Networking.my_recv(8, s); if (recvBuf == null) return -1; DateTime lastModTime = DateTime.FromBinary(BitConverter.ToInt64(recvBuf, 0)); recvBuf = Networking.my_recv(8, s); if (recvBuf == null) return -1; Int64 id = BitConverter.ToInt64(recvBuf, 0); byte[] encryptedData = Networking.my_recv(48, s); if (encryptedData == null) return -1; String checksum = Encoding.UTF8.GetString(Security.AESDecrypt(aesKey, encryptedData)); file.Checksum = checksum; file.Id = id; file.Deleted = true; file.UserId = userId; file.Filename = Path.GetFileName(path); file.Path = Path.GetDirectoryName(path); file.LastModificationTime = lastModTime; file.Fullname = Path.Combine(file.Path, file.Filename); dir.Files.Add(file.NameVersion, file); } return filesInfoToRecv; } } catch (SocketException) { return -1; } return -1; }
public FileListItem(DirectoryFile file) : base(file) { btnContent = (base.Directory || base.Deleted) ? "Restore" : "See older versions"; }
public void addFile(DirectoryFile file, bool openSocket) { Socket s = null; try { if (keepAlive()) { TcpClient client = new TcpClient(); client.Connect(this.server); s = client.Client; s.ReceiveTimeout = Networking.TIME_OUT_SHORT; s.SendTimeout = Networking.TIME_OUT_SHORT; //using client credential with a new socket byte[] command = BitConverter.GetBytes((UInt32)Networking.CONNECTION_CODES.SESSION_WATCH); s.Send(command); byte[] session = BitConverter.GetBytes(this.sessionId); byte[] rand = Networking.my_recv(8, s); SHA1CryptoServiceProvider sha = new SHA1CryptoServiceProvider(); byte[] hash = sha.ComputeHash(Security.XOR(rand, session)); s.Send(hash); command = Networking.my_recv(4, s); if (command != null && ( ((Networking.CONNECTION_CODES)BitConverter.ToUInt32(command, 0) == Networking.CONNECTION_CODES.OK))) { //session is not expired: ok command = BitConverter.GetBytes((UInt32)Networking.CONNECTION_CODES.FS_SYNCH); s.Send(command); command = BitConverter.GetBytes((UInt32)Networking.CONNECTION_CODES.ADD); s.Send(command); byte[] encryptedData = Security.AESEncrypt(aesKey, file.Filename); byte[] buf = BitConverter.GetBytes(encryptedData.Length); s.Send(buf); s.Send(encryptedData); encryptedData = Security.AESEncrypt(aesKey, file.Path); buf = BitConverter.GetBytes(encryptedData.Length); s.Send(buf); s.Send(encryptedData); buf = BitConverter.GetBytes(file.LastModificationTime.ToBinary()); s.Send(buf); if (file.Directory) { command = BitConverter.GetBytes((UInt32)Networking.CONNECTION_CODES.DIR); s.Send(command); command = BitConverter.GetBytes((UInt32)Networking.CONNECTION_CODES.END_SYNCH); s.Send(command); } else { command = BitConverter.GetBytes((UInt32)Networking.CONNECTION_CODES.FILE); s.Send(command); command = Networking.my_recv(4, s); if (command != null && ( ((Networking.CONNECTION_CODES)BitConverter.ToUInt32(command, 0) == Networking.CONNECTION_CODES.OK))) { buf = BitConverter.GetBytes(file.Length); s.Send(buf); using (FileStream reader = File.OpenRead(file.Fullname)) { long left = file.Length; while (left > 0) { int dim = (left > 4096) ? 4096 : (int)left; buf = new byte[dim]; reader.Read(buf, 0, dim); encryptedData = Security.AESEncrypt(aesKey, buf); s.Send(encryptedData); //s.Send(buf); left -= dim; } command = BitConverter.GetBytes((UInt32)Networking.CONNECTION_CODES.END_SYNCH); s.Send(command); } } } } } } catch (SocketException) { return; } finally { if (s != null) s.Close(); } }
private void addFile(DirectoryFile file) { try { Socket s = tcpClient.Client; byte[] command = BitConverter.GetBytes((UInt32)Networking.CONNECTION_CODES.ADD); s.Send(command); byte[] encryptedData = Security.AESEncrypt(aesKey, file.Filename); byte[] buf = BitConverter.GetBytes(encryptedData.Length); s.Send(buf); s.Send(encryptedData); encryptedData = Security.AESEncrypt(aesKey, file.Path); buf = BitConverter.GetBytes(encryptedData.Length); s.Send(buf); s.Send(encryptedData); buf = BitConverter.GetBytes(file.LastModificationTime.ToBinary()); s.Send(buf); if (file.Directory) { command = BitConverter.GetBytes((UInt32)Networking.CONNECTION_CODES.DIR); s.Send(command); } else { command = BitConverter.GetBytes((UInt32)Networking.CONNECTION_CODES.FILE); s.Send(command); command = Networking.my_recv(4, tcpClient.Client); if (command != null && ( ((Networking.CONNECTION_CODES)BitConverter.ToUInt32(command, 0) == Networking.CONNECTION_CODES.OK))) { buf = BitConverter.GetBytes(file.Length); s.Send(buf); using (FileStream reader = File.OpenRead(file.Fullname)) { long left = file.Length; while (left > 0) { int dim = (left > 4096) ? 4096 : (int)left; buf = new byte[dim]; reader.Read(buf, 0, dim); encryptedData = Security.AESEncrypt(aesKey, buf); s.Send(encryptedData); //s.Send(buf); left -= dim; } } } } } catch (SocketException se) { throw se; } }
private void deleteFile(DirectoryFile file) { try { Socket s = tcpClient.Client; byte[] command = BitConverter.GetBytes((UInt32)Networking.CONNECTION_CODES.DEL); s.Send(command); byte[] encryptedData = Security.AESEncrypt(aesKey, file.Filename); byte[] buf = BitConverter.GetBytes(encryptedData.Length); s.Send(buf); s.Send(encryptedData); encryptedData = Security.AESEncrypt(aesKey, file.Path); buf = BitConverter.GetBytes(encryptedData.Length); s.Send(buf); s.Send(encryptedData); buf = BitConverter.GetBytes(file.LastModificationTime.ToBinary()); s.Send(buf); if (file.Directory) { command = BitConverter.GetBytes((UInt32)Networking.CONNECTION_CODES.DIR); s.Send(command); } else { command = BitConverter.GetBytes((UInt32)Networking.CONNECTION_CODES.FILE); s.Send(command); } } catch (SocketException se) { throw se; } }
public void fillDirectoryStatus(DirectoryStatus local, String directory) { try { var entries = Directory.EnumerateFileSystemEntries(directory); foreach (var path in entries) { if (File.Exists(path)) { //entry is a file FileInfo info = new FileInfo(path); DirectoryFile file = new DirectoryFile(); file.Length = info.Length; file.UserId = userId; file.LastModificationTime = info.LastWriteTime; file.Filename = info.Name; file.Path = info.DirectoryName; file.Fullname = info.FullName; file.Checksum = getChecksum(info); local.Files.Add(file.Fullname, file); } if (Directory.Exists(path)) { //entry is a directory DirectoryFile file = new DirectoryFile(); DirectoryInfo info = new DirectoryInfo(path); file.UserId = userId; file.Directory = true; file.Filename = info.Name; file.Path = info.Parent.FullName; file.Fullname = info.FullName; file.LastModificationTime = info.LastWriteTime; local.Files.Add(file.Fullname, file); fillDirectoryStatus(local, info.FullName); } } } catch (UnauthorizedAccessException uae) { throw uae; } catch (IOException ioe) { throw ioe; } }
public Int64 getDirectoryInfo(DirectoryStatus current, String directory) { try { Socket s = tcpClient.Client; byte[] command = BitConverter.GetBytes((UInt32)Networking.CONNECTION_CODES.DIR); s.Send(command); byte[] buf = Security.AESEncrypt(aesKey, Encoding.UTF8.GetBytes(directory)); s.Send(BitConverter.GetBytes(buf.Length)); s.Send(buf); command = Networking.my_recv(4, tcpClient.Client); if (command != null && ( ((Networking.CONNECTION_CODES)BitConverter.ToUInt32(command, 0) == Networking.CONNECTION_CODES.OK))) { byte[] recvBuf = Networking.my_recv(4, s); if (recvBuf == null) return -1; Int32 filesInfoToRecv = BitConverter.ToInt32(recvBuf, 0); for (int i = 0; i < filesInfoToRecv; i++) { DirectoryFile file = new DirectoryFile(); recvBuf = Networking.my_recv(4, s); if (recvBuf == null) return -1; int len = BitConverter.ToInt32(recvBuf, 0); byte[] encryptedData = Networking.my_recv(len, s); if (encryptedData == null) return -1; String path = Encoding.UTF8.GetString(Security.AESDecrypt(aesKey, encryptedData)); recvBuf = Networking.my_recv(4, s); if (recvBuf == null) return -1; len = BitConverter.ToInt32(recvBuf, 0); encryptedData = Networking.my_recv(len, s); if (encryptedData == null) return -1; String filename = Encoding.UTF8.GetString(Security.AESDecrypt(aesKey, encryptedData)); recvBuf = Networking.my_recv(8, s); if (recvBuf == null) return -1; DateTime lastModTime = DateTime.FromBinary(BitConverter.ToInt64(recvBuf, 0)); command = Networking.my_recv(4, s); if (command == null) return -1; if ((Networking.CONNECTION_CODES)BitConverter.ToUInt32(command, 0) == Networking.CONNECTION_CODES.DEL) { file.Deleted = true; } command = Networking.my_recv(4, s); if (command == null) return -1; if ((Networking.CONNECTION_CODES)BitConverter.ToUInt32(command, 0) == Networking.CONNECTION_CODES.DIR) { file.Directory = true; } if ((Networking.CONNECTION_CODES)BitConverter.ToUInt32(command, 0) == Networking.CONNECTION_CODES.FILE) { recvBuf = Networking.my_recv(8, s); if (recvBuf == null) return -1; file.Id = BitConverter.ToInt64(recvBuf, 0); } file.LastModificationTime = lastModTime; file.UserId = userId; file.Filename = filename; file.Path = path; file.Fullname = Path.Combine(path, filename); current.Files.Add(file.Fullname, file); } return filesInfoToRecv; } } catch (SocketException) { return -1; } return -1; }
/***** END CLEAN DB ******/ public static bool insertFile(SQLiteConnection conn, DirectoryStatus newStatus, String filename, String path, byte[] file, DateTime lastModTime) { try { /*****DB cleaning******/ if (!cleanDBfiles(conn, newStatus, filename, path)) return false; /*****END CLEANING******/ using (SQLiteCommand cmd = conn.CreateCommand()) { cmd.CommandText = @"insert into files (user_id,path,filename,content,last_mod_time) values" + " (@user, @path, @filename, @content, @lastModTime);"; cmd.Parameters.AddWithValue("@user", newStatus.Username); cmd.Parameters.AddWithValue("@path", path + "\\"); cmd.Parameters.AddWithValue("@filename", filename); cmd.Parameters.AddWithValue("@content", file); cmd.Parameters.AddWithValue("@lastModTime", lastModTime.ToString(date_format)); cmd.ExecuteNonQuery(); } Int64? fileId = -1; using (SQLiteCommand cmd = conn.CreateCommand()) { cmd.CommandText = "select file_id from files where user_id = @user and path = @path and" + " filename = @filename and last_mod_time = @lastModTime;"; cmd.Parameters.AddWithValue("@user", newStatus.Username); cmd.Parameters.AddWithValue("@path", path + "\\"); cmd.Parameters.AddWithValue("@filename", filename); cmd.Parameters.AddWithValue("@lastModTime", lastModTime.ToString(date_format)); using (var reader = cmd.ExecuteReader()) { while (reader.Read()) { fileId = (Int64?)reader[0]; } } } if (fileId == null || fileId <= 0) return false; String checksum = Security.CalculateMD5Hash(file); using (SQLiteCommand cmd = conn.CreateCommand()) { cmd.CommandText = @"insert into snapshots (user_id,file_id,creation_time,checksum,path,last_mod_time,filename) values" + " (@user, @fileId, @creationTime, @checksum, @path, @lastModTime, @filename);"; cmd.Parameters.AddWithValue("@user", newStatus.Username); cmd.Parameters.AddWithValue("@fileId", fileId); cmd.Parameters.AddWithValue("@creationTime", newStatus.CreationTime.ToString(date_format)); cmd.Parameters.AddWithValue("@checksum", checksum); cmd.Parameters.AddWithValue("@lastModTime", lastModTime.ToString(date_format)); cmd.Parameters.AddWithValue("@path", path + "\\"); cmd.Parameters.AddWithValue("@filename", filename); cmd.ExecuteNonQuery(); } //add file DirectoryFile dirFile = new DirectoryFile((Int64)fileId, path, filename, newStatus.Username, checksum, lastModTime); newStatus.Files.Add(dirFile.Fullname, dirFile); return true; } catch (SQLiteException) { return false;} }
public DirectoryFile clone() { DirectoryFile file = new DirectoryFile(); file.Deleted = (deleted) ? true : false; file.Directory = (directory) ? true : false; file.Filename = (filename == null) ? null : String.Copy(filename); file.Fullname = (fullname == null) ? null : String.Copy(fullname); file.Id = id; file.Length = length; file.Path = (path == null) ? null : String.Copy(path); file.LastModificationTime = lastModificationTime; file.UserId = (userId == null) ? null : String.Copy(userId); file.Checksum = (checksum == null) ? null : String.Copy(checksum); return file; }
public static bool deleteFile(SQLiteConnection conn, DirectoryFile file, DirectoryStatus newStatus) { try { using (SQLiteCommand cmd = conn.CreateCommand()) { cmd.CommandText = @"insert into snapshots (user_id,file_id,creation_time,checksum,path," + " filename,last_mod_time,deleted) values (@user, @fileId, @creationTime, @checksum, @path," + " @filename, @lastModTime, @deleted);"; cmd.Parameters.AddWithValue("@user", file.UserId); cmd.Parameters.AddWithValue("@fileId", file.Id); cmd.Parameters.AddWithValue("@creationTime", newStatus.CreationTime.ToString(date_format)); cmd.Parameters.AddWithValue("@checksum", file.Checksum); cmd.Parameters.AddWithValue("@path", file.Path + "\\"); cmd.Parameters.AddWithValue("@filename", file.Filename); cmd.Parameters.AddWithValue("@lastModTime", file.LastModificationTime.ToString(date_format)); cmd.Parameters.AddWithValue("@deleted", true); cmd.ExecuteNonQuery(); } DirectoryFile dirFile = new DirectoryFile(file.Id, file.Path, file.Filename, file.UserId, file.Checksum, file.LastModificationTime); file.Deleted = true; newStatus.Files.Add(dirFile.Fullname, dirFile); return true; } catch (SQLiteException) { return false; } }
public SnapshotItem(DirectoryFile file) : base(file) { }
public static DirectoryStatus getLastSnapshot(String directory, String username) { DirectoryStatus ds = new DirectoryStatus(); ds.FolderPath = directory; ds.Username = username; directory += "\\"; String date = null; try { using (var con = new SQLiteConnection(connectionString)) { con.Open(); if (!cleanDBsnapshots(con, username, directory)) return ds; using (var cmd = con.CreateCommand()) { int len = directory.Length; cmd.CommandText = @"select max(creation_time) from snapshots where" + " user_id = @id and substr(path,1," + len + ") = @dir;"; cmd.Parameters.AddWithValue("@id", username); cmd.Parameters.AddWithValue("@dir", directory); object val = cmd.ExecuteScalar(); date = (val == System.DBNull.Value) ? null : (String)val; } if (date != null) { ds.CreationTime = DateTime.ParseExact(date, date_format, null); using (var cmd = con.CreateCommand()) { int len = directory.Length; cmd.CommandText = @"select * from snapshots where user_id = @id" + " and substr(path,1," + len + ") = @dir" + " and creation_time = @date;"; cmd.Parameters.AddWithValue("@id", username); cmd.Parameters.AddWithValue("@dir", directory); cmd.Parameters.AddWithValue("@date", date); using (var reader = cmd.ExecuteReader()) { while (reader.Read()) { DirectoryFile file = new DirectoryFile(); file.UserId = username; file.Directory = (Boolean)reader["directory"]; file.Path = (String)reader["path"]; file.Path = file.Path.Substring(0,file.Path.LastIndexOf('\\')); file.Filename = (String)reader["filename"]; file.Deleted = (Boolean)reader["deleted"]; file.LastModificationTime = (DateTime)reader["last_mod_time"]; if (!file.Directory) { file.Id = (Int64)reader["file_id"]; file.Checksum = (String)reader["checksum"]; } file.Fullname = Path.Combine(file.Path, file.Filename); ds.Files.Add(file.Fullname, file); } } } } } return ds; } catch (SQLiteException) { return null; } }
public DirectoryItem(DirectoryFile file) : base(file) { fullname = System.IO.Path.Combine(base.Path, base.Filename); isOnDisk = (base.Deleted) ? false : true; }
private void OnChanged(object source, FileSystemEventArgs e) { try { lock (this) { if (File.Exists(e.FullPath)) { DateTime lastWriteTime = File.GetLastWriteTime(e.FullPath); if (lastWriteTime != lastRead) { //exclude files started from '~','$' and '.'(hidden files) and without extensions FileInfo info = new FileInfo(e.FullPath); if (!info.Name[0].Equals('~') && !info.Name[0].Equals('$') && !info.Name[0].Equals('.') && !info.Extension.Equals(".tmp") && !info.Extension.Equals(".TMP") && !info.Extension.Equals("")) { //file DirectoryFile file = new DirectoryFile(); file.Filename = info.Name; file.Fullname = info.FullName; file.Path = info.DirectoryName; file.UserId = client.UserId; file.LastModificationTime = info.LastWriteTime; file.Length = info.Length; client.updateFile(file, true); } lastRead = lastWriteTime; } } } } catch (Exception) { return; } }
public FileVersionItem(DirectoryFile file) : base(file) { checksum = file.Checksum; }
// Define the event handlers. private void OnDeleted(object source, FileSystemEventArgs e) { DirectoryFile file = new DirectoryFile(); file.Fullname = e.FullPath; file.Filename = Path.GetFileName(e.FullPath); file.Path = Path.GetDirectoryName(e.FullPath); file.UserId = client.UserId; if (!Path.HasExtension(e.FullPath)) { file.Directory = true; //directory: a file with no extension is treated like a directory client.deleteFile(file, true); } else { //file file.Directory = false; if ( !Path.GetFileName(e.FullPath)[0].Equals('~') && !Path.GetFileName(e.FullPath)[0].Equals('$') && !Path.GetFileName(e.FullPath)[0].Equals('.') && !Path.GetExtension(e.FullPath).Equals(".tmp") && !Path.GetExtension(e.FullPath).Equals(".TMP") ) { client.deleteFile(file, true); } } }
public ListItem(DirectoryFile file) { directory = file.Directory; filename = file.Filename; path = file.Path; deleted = file.Deleted; id = file.Id; lastModTime = file.LastModificationTime.ToString("ddd, d MMM yyyy HH:mm:ss"); }