internal static LocalDirectory[] ListByParentId(Nullable <int> parent_id) { return(Core.FileSystem.UseConnection <LocalDirectory[]>(delegate(IDbConnection connection) { IDbCommand command = connection.CreateCommand(); string query = null; if (parent_id.Equals(null)) { query = "SELECT * FROM directoryitems WHERE parent_id ISNULL AND type = 'D'"; } else { query = "SELECT * FROM directoryitems WHERE parent_id = @parent_id AND type = 'D'"; Core.FileSystem.AddParameter(command, "@parent_id", (int)parent_id); } command.CommandText = query; DataSet ds = Core.FileSystem.ExecuteDataSet(command); LocalDirectory[] results = new LocalDirectory[ds.Tables[0].Rows.Count]; for (int x = 0; x < ds.Tables[0].Rows.Count; x++) { results[x] = LocalDirectory.FromDataRow(ds.Tables[0].Rows[x]); } return results; })); }
private void HandleDirectoryChanged(string path) { // Do these one at a time. lock (directoryChangeLock) { DirectoryInfo info = new DirectoryInfo(path); MFS.IDirectoryItem item = GetFromLocalPath(path); if (item == null && info != null) { // New Directory! MFS.LocalDirectory parentDirectory = GetParentDirectory(info); if (parentDirectory != null) { this.loggingService.LogDebug("NEW DIR !! " + path); parentDirectory.CreateSubDirectory(info.Name, info.FullName); } else { // No parent directory, this happens because // we can get events out of order. this.loggingService.LogDebug("NEW DIR NO PARENT !! " + path); CreateDirectoryForLocalPath(path); } } } }
static LocalFile CreateFile(LocalDirectory parentDirectory, string name, string localpath, long length) { int last_id = -1; string fullPath = PathUtil.Join(parentDirectory.FullPath, name); if (fullPath.Length > 1 && fullPath.EndsWith("/")) { fullPath = fullPath.Substring(0, fullPath.Length - 1); } Core.FileSystem.UseConnection(delegate(IDbConnection connection) { IDbCommand cmd = connection.CreateCommand(); cmd.CommandText = @"INSERT INTO directoryitems (type, name, local_path, parent_id, length, full_path) VALUES ('F', @name, @local_path, @parent_id, @length, @full_path);" ; Core.FileSystem.AddParameter(cmd, "@name", name); Core.FileSystem.AddParameter(cmd, "@local_path", localpath); Core.FileSystem.AddParameter(cmd, "@parent_id", parentDirectory.Id); Core.FileSystem.AddParameter(cmd, "@length", length); Core.FileSystem.AddParameter(cmd, "@full_path", fullPath); Core.FileSystem.ExecuteNonQuery(cmd); cmd = connection.CreateCommand(); cmd.CommandText = "SELECT last_insert_rowid()"; last_id = Convert.ToInt32(Core.FileSystem.ExecuteScalar(cmd)); }, true); return(new LocalFile(last_id, parentDirectory.Id, name, localpath, length, fullPath)); }
public LocalDirectory GetLocalDirectory(string path) { if (path.Length > 1 && path.EndsWith("/")) { path = path.Substring(0, path.Length - 1); } string[] parts = path.Split('/'); if (parts.Length < 3) { return((LocalDirectory)GetDirectory(path)); } else { return(UseConnection <LocalDirectory>(delegate(IDbConnection connection) { string query = "SELECT * FROM directoryitems WHERE type = 'D' AND full_path = @full_path LIMIT 1"; IDbCommand command = connection.CreateCommand(); command.CommandText = query; AddParameter(command, "@full_path", path); DataSet data = ExecuteDataSet(command); if (data.Tables[0].Rows.Count > 0) { return LocalDirectory.FromDataRow(data.Tables[0].Rows[0]); } else { return null; } })); } }
public SearchResultInfo SearchFiles(string query) { IDbCommand command; DataSet ds; int x; SearchResultInfo result; var directories = new List <string>(); var files = new List <SharedFileListing>(); result = new SearchResultInfo(); var queryNode = UserQueryParser.Parse(query, FieldSet); var queryFragment = queryNode.ToSql(FieldSet); var sb = new StringBuilder(); sb.Append("SELECT * FROM directoryitems WHERE "); sb.Append(queryFragment); sb.AppendFormat(" LIMIT {0}", MAX_RESULTS.ToString()); UseConnection(connection => { command = connection.CreateCommand(); command.CommandText = sb.ToString(); ds = ExecuteDataSet(command); for (x = 0; x < ds.Tables[0].Rows.Count; x++) { if (ds.Tables[0].Rows[x]["type"].ToString() == "F") { files.Add(new SharedFileListing(LocalFile.FromDataRow(ds.Tables[0].Rows[x]), false)); } else { LocalDirectory dir = LocalDirectory.FromDataRow(ds.Tables[0].Rows[x]); // FIXME: Ugly: Remove '/local' from begining of path string path = "/" + string.Join("/", dir.FullPath.Split('/').Slice(2)); directories.Add(path); } } }); result.Files = files.ToArray(); result.Directories = directories.ToArray(); return(result); }
public static LocalDirectory ById(int id) { return(Core.FileSystem.UseConnection <LocalDirectory>(delegate(IDbConnection connection) { IDbCommand cmd = connection.CreateCommand(); cmd.CommandText = "SELECT * FROM directoryitems WHERE id=@id AND type = 'D' LIMIT 1"; Core.FileSystem.AddParameter(cmd, "@id", id); DataSet ds = Core.FileSystem.ExecuteDataSet(cmd); if (ds.Tables[0].Rows.Count > 0) { return LocalDirectory.FromDataRow(ds.Tables[0].Rows[0]); } else { return null; } })); }
private void HandleFileChanged(string path) { FileInfo info = new FileInfo(path); MFS.IDirectoryItem item = GetFromLocalPath(path); if (item == null) { // New File! MFS.LocalDirectory parentDirectory = GetParentDirectory(info); this.loggingService.LogDebug("NEW FILE!! IN " + parentDirectory.FullPath); } else { // Updated File! this.loggingService.LogWarning("NOTE: Changed file detected, however handling this is not currently supported. Path: {0}", item.FullPath); } }
internal LocalDirectory CreateSubDirectory(string name, string localPath) { return(LocalDirectory.CreateDirectory(this, name, localPath)); }
private static LocalDirectory CreateDirectory(LocalDirectory parent, string name, string local_path) { string fullPath; int last_id = -1; if (String.IsNullOrEmpty(local_path)) { throw new ArgumentNullException("local_path"); } if (!System.IO.Directory.Exists(local_path)) { throw new ArgumentException("local_path", String.Format("Directory does not exist: '{0}'", local_path)); } if (parent != null) { fullPath = PathUtil.Join(parent.FullPath, name); } else { fullPath = PathUtil.Join("/", name); } if (fullPath.Length > 1 && fullPath.EndsWith("/")) { fullPath = fullPath.Substring(0, fullPath.Length - 1); } Core.FileSystem.UseConnection(delegate (IDbConnection connection) { IDbCommand cmd = connection.CreateCommand(); cmd.CommandText = @"INSERT INTO directoryitems (type, parent_id, name, local_path, full_path) VALUES ('D', @parent_id, @name, @local_path, @full_path);"; Core.FileSystem.AddParameter(cmd, "@name", name); Core.FileSystem.AddParameter(cmd, "@local_path", local_path); Core.FileSystem.AddParameter(cmd, "@full_path", fullPath); if (parent == null) { Core.FileSystem.AddParameter(cmd, "@parent_id", null); } else { Core.FileSystem.AddParameter(cmd, "@parent_id", (int)parent.Id); } Core.FileSystem.ExecuteNonQuery(cmd); cmd = connection.CreateCommand(); cmd.CommandText = "SELECT last_insert_rowid()"; last_id = Convert.ToInt32(Core.FileSystem.ExecuteScalar(cmd)); }, true); if (parent != null) { parent.InvalidateCache(); } int parentId = (parent == null) ? -1 : parent.Id; return new LocalDirectory(last_id, parentId, name, local_path, fullPath); }
private static LocalDirectory CreateDirectory(LocalDirectory parent, string name, string local_path) { string fullPath; int last_id = -1; if (String.IsNullOrEmpty(local_path)) { throw new ArgumentNullException("local_path"); } if (!System.IO.Directory.Exists(local_path)) { throw new ArgumentException("local_path", String.Format("Directory does not exist: '{0}'", local_path)); } if (parent != null) { fullPath = PathUtil.Join(parent.FullPath, name); } else { fullPath = PathUtil.Join("/", name); } if (fullPath.Length > 1 && fullPath.EndsWith("/")) { fullPath = fullPath.Substring(0, fullPath.Length - 1); } Core.FileSystem.UseConnection(delegate(IDbConnection connection) { IDbCommand cmd = connection.CreateCommand(); cmd.CommandText = @"INSERT INTO directoryitems (type, parent_id, name, local_path, full_path) VALUES ('D', @parent_id, @name, @local_path, @full_path);" ; Core.FileSystem.AddParameter(cmd, "@name", name); Core.FileSystem.AddParameter(cmd, "@local_path", local_path); Core.FileSystem.AddParameter(cmd, "@full_path", fullPath); if (parent == null) { Core.FileSystem.AddParameter(cmd, "@parent_id", null); } else { Core.FileSystem.AddParameter(cmd, "@parent_id", (int)parent.Id); } Core.FileSystem.ExecuteNonQuery(cmd); cmd = connection.CreateCommand(); cmd.CommandText = "SELECT last_insert_rowid()"; last_id = Convert.ToInt32(Core.FileSystem.ExecuteScalar(cmd)); }, true); if (parent != null) { parent.InvalidateCache(); } int parentId = (parent == null) ? -1 : parent.Id; return(new LocalDirectory(last_id, parentId, name, local_path, fullPath)); }
public SharedDirectoryInfo(LocalDirectory dir) { m_Name = dir.Name; // FIXME: Ugly: Remove '/local' from begining of path m_FullPath = "/" + String.Join("/", dir.FullPath.Split('/').Slice(2)); }
internal static LocalDirectory[] ListByParentId(Nullable<int> parent_id) { return Core.FileSystem.UseConnection<LocalDirectory[]>(delegate (IDbConnection connection) { IDbCommand command = connection.CreateCommand(); string query = null; if (parent_id.Equals(null)) { query = "SELECT * FROM directoryitems WHERE parent_id ISNULL AND type = 'D'"; } else { query = "SELECT * FROM directoryitems WHERE parent_id = @parent_id AND type = 'D'"; Core.FileSystem.AddParameter(command, "@parent_id", (int)parent_id); } command.CommandText = query; DataSet ds = Core.FileSystem.ExecuteDataSet(command); LocalDirectory[] results = new LocalDirectory[ds.Tables[0].Rows.Count]; for (int x = 0; x < ds.Tables[0].Rows.Count; x++) { results[x] = LocalDirectory.FromDataRow(ds.Tables[0].Rows[x]); } return results; }); }
private void ProcessDirectory(LocalDirectory parentDirectory, IO.DirectoryInfo directoryInfo) { if (parentDirectory == null) { throw new ArgumentNullException("parentDirectory"); } if (directoryInfo == null) { throw new ArgumentNullException("directoryInfo"); } try { if (directoryInfo.Name.StartsWith(".") == false) { LocalDirectory directory = (LocalDirectory)parentDirectory.GetSubdirectory(directoryInfo.Name); if (directory == null) { directory = parentDirectory.CreateSubDirectory(directoryInfo.Name, directoryInfo.FullName); } foreach (IO.FileInfo fileInfo in directoryInfo.GetFiles()) { if (fileInfo.Name.StartsWith(".") == false) { if (IndexingFile != null) IndexingFile(this, fileInfo.FullName); LocalFile file = (LocalFile)directory.GetFile(fileInfo.Name); if (file == null) { file = directory.CreateFile(fileInfo); } else { // XXX: Update file info } if (String.IsNullOrEmpty(file.InfoHash)) { Core.ShareHasher.HashFile(file); } } } foreach (IO.DirectoryInfo subDirectoryInfo in directoryInfo.GetDirectories()) { ProcessDirectory(directory, subDirectoryInfo); } } } catch (ThreadAbortException) { // Canceled, ignore error. } catch (Exception ex) { LoggingService.LogError("Error while re-indexing shared files:", ex); if (ErrorIndexing != null) { ErrorIndexing(this, ex); } } }
public Message CreateRespondDirListingMessage(Node messageTo, LocalDirectory directory) { SharedDirectoryInfo info = new SharedDirectoryInfo(directory); info.Files = directory.Files.Select(f => new SharedFileListing((LocalFile)f, false)).ToArray(); info.Directories = directory.Directories.Select(d => d.Name).ToArray(); Message message = new Message (network, MessageType.RespondDirListing); message.To = messageTo.NodeID; message.Content = info; return message; }
internal static LocalFile CreateFile(LocalDirectory parentDirectory, System.IO.FileInfo info) { return CreateFile(parentDirectory, info.Name, info.FullName, info.Length); }
static LocalFile CreateFile(LocalDirectory parentDirectory, string name, string localpath, long length) { int last_id = -1; string fullPath = PathUtil.Join(parentDirectory.FullPath, name); if (fullPath.Length > 1 && fullPath.EndsWith("/")) { fullPath = fullPath.Substring(0, fullPath.Length - 1); } Core.FileSystem.UseConnection(delegate(IDbConnection connection) { IDbCommand cmd = connection.CreateCommand(); cmd.CommandText = @"INSERT INTO directoryitems (type, name, local_path, parent_id, length, full_path) VALUES ('F', @name, @local_path, @parent_id, @length, @full_path);"; Core.FileSystem.AddParameter(cmd, "@name", name); Core.FileSystem.AddParameter(cmd, "@local_path", localpath); Core.FileSystem.AddParameter(cmd, "@parent_id", parentDirectory.Id); Core.FileSystem.AddParameter(cmd, "@length", length); Core.FileSystem.AddParameter(cmd, "@full_path", fullPath); Core.FileSystem.ExecuteNonQuery(cmd); cmd = connection.CreateCommand(); cmd.CommandText = "SELECT last_insert_rowid()"; last_id = Convert.ToInt32(Core.FileSystem.ExecuteScalar(cmd)); }, true); return new LocalFile(last_id, parentDirectory.Id, name, localpath, length, fullPath); }
static internal LocalFile CreateFile(LocalDirectory parentDirectory, System.IO.FileInfo info) { return(CreateFile(parentDirectory, info.Name, info.FullName, info.Length)); }