Пример #1
0
 public folderProcessArgs(folderProcessor processor, fsItem folder,string status, string message)
 {
     this.message = message;
     this.status = status;
     this.processor = processor;
     this.folder = folder;
 }
Пример #2
0
        internal fsItem[] getDuplicateFiles()
        {
            DBConnection = new SQLiteConnection(connectionString);
            DBCommand = new SQLiteCommand();
            DBCommand.Connection = DBConnection;
            string s = "";
            string sql = "SELECT hash, count(*) FROM folders WHERE isFile=1 group by hash having count(*)>1";
            DBCommand.CommandText = sql;
            List<string> duplicateHash = new List<string>();
            List<fsItem> duplicateItem = new List<fsItem>();
            int c = 0;
            SQLiteDataReader dr = null;
            try
            {
                DBConnection.Open();
                dr = DBCommand.ExecuteReader();

                while (dr.Read())
                {

                    duplicateHash.Add("'" + dr.GetString(0) + "'");
                }
                if (duplicateHash.Count > 0)
                {
                    s = duplicateHash.Aggregate((i, j) => i + "," + j);
                    dr.Close();
                    duplicateHash.Clear();
                    sql = @"select id,path,size,isFile from folders where hash in (" + s + ") order by hash";
                    DBCommand.CommandText = sql;
                    dr = DBCommand.ExecuteReader();
                    while (dr.Read())
                    {
                        c++;
                        //duplicateHash.Add(dr.GetString(0)+": '" + dr.GetString(1) + "'");
                        //lets do not show the hash, just in case
                        duplicateHash.Add(c.ToString() + ": '" + dr.GetString(1) + "'");
                        fsItem fi = new fsItem();
                        fi.Id = dr.GetInt32(0);
                        fi.isFile = dr.GetBoolean(3);
                        fi.Path = dr.GetString(1);
                        fi.Size = dr.GetInt32(2);
                        duplicateItem.Add(fi);

                    }
                    //duplicateHash.ToArray();
                    if (duplicateHash.Count > 0)
                    {
                        s = "duplicate files\r\n" + duplicateHash.Aggregate((i, j) => i + "\r\n" + j);
                    }
                    dr.Close();

                }
                else
                {
                    s = "no duplicate files found";
                    return null;
                }
            }
            catch (Exception ex)
            {
                throw new Exception("Error while getting duplicate files, MEssage:" + ex.Message);
            }
            finally
            {
                DBConnection.Close();
                //dr.Close();
                duplicateHash.Clear();
            }
            return duplicateItem.ToArray<fsItem>();
        }
Пример #3
0
        private void updateFolder(ref fsItem folder)
        {
            string sql = @"UPDATE folders SET hash='" + folder.Hash + "',size=" + folder.Size.ToString() + ",access="+folder.Access+" WHERE id=" + folder.Id.ToString();
            DBCommand.CommandText = sql;
            try
            {
                DBCommand.ExecuteNonQuery();
            }
            catch (Exception ex)
            {
                string e = ex.Message;
            }

            //DBCommand.CommandText = @"select last_insert_rowid()";
            //fi.Id = (int)(long)DBCommand.ExecuteScalar(); // Need to type-cast since `ExecuteScalar` returns an object.
        }
Пример #4
0
 private void storeFileHash(ref fsItem fi)
 {
     string sql = "INSERT INTO folders (parentId,path,hash,size,isFile) ";
     sql += " values (" + fi.ParentId.ToString() + ",'" + processSQLTestParam(fi.Path) + "','" + fi.Hash + "'," + fi.Size.ToString() + ",1)";
     DBCommand.CommandText = sql;
     DBCommand.ExecuteNonQuery();
     DBCommand.CommandText = @"select last_insert_rowid()";
     fi.Id = (int)(long)DBCommand.ExecuteScalar(); // Need to type-cast since `ExecuteScalar` returns an object.
 }
Пример #5
0
 private void processFilesInFolder(ref List<fsItem> folderItems, fsItem folder, FileInfo[] files)
 {
     foreach (FileInfo file in files)
     {
         fsItem fi = new fsItem();
         fi.ParentId = folder.Id;
         fi.isFile = true;
         fi.Path = file.FullName;
         fi.Size = (long)file.Length;
         fi.Hash = getMD5FileHash(fi.Path);
         storeFileHash(ref fi); // atfer getting theshash
         folderItems.Add(fi);
     }
 }
Пример #6
0
        fsItem dirWalk(string sDir, int parentId, string tab)
        {
            /*if (sDir==@"C:\Users\Public\Pictures\felix\Picasa\Exports")
            {
                string s = sDir+"";
            }*/
            fsItem folder = new fsItem();
            folder.Path = sDir;
            folder.isFile = false;
            folder.ParentId = parentId;
            folderProcessArgs newFolderProcess;
            step++;

            // if (step > 1) { stop = true; }
            DirectoryInfo directory = new DirectoryInfo(sDir);

            FileInfo[] files = null;
            string[] folders;
            try
            {

                folders = Directory.GetDirectories(sDir);
                if (folders.Count() > 0)
                {
                    //create a group
                    newFolderProcess = new folderProcessArgs(this, folder, "has-children", "...Ignoring Items with no access, please check!");
                    OnNewFolder(this, newFolderProcess);

                }
                files = directory.GetFiles();
                if ((folders.Count() + files.Count()) > 0)
                {
                    List<fsItem> folderItems = new List<fsItem>();

                    string insertNewItemCommand = "INSERT INTO folders (parentId,path,isFile) values (" + parentId.ToString() + ",'" + processSQLTestParam(sDir) + "',0)";
                    DBCommand.CommandText = insertNewItemCommand;
                    DBCommand.ExecuteNonQuery();
                    DBCommand.CommandText = @"select last_insert_rowid()";
                    folder.Id = (int)(long)DBCommand.ExecuteScalar(); // Need to type-cast since `ExecuteScalar` returns an object.
                    //generate here an event and capture it and call report progress pn the background worker every times an event is captured
                    newFolderProcess = new folderProcessArgs(this, folder, "started", "\r\n" + tab + "Processing Folder :'" + sDir + "'...");
                    OnNewFolder(this, newFolderProcess);

                    if (stop)
                    {
                        folders = new string[0];
                        files = new FileInfo[0];
                    }
                    //process folders
                    foreach (string subfolder in folders)
                    {
                        fsItem fsi = dirWalk(subfolder, folder.Id, tab + "\t");
                        if (fsi != null)
                        {
                            if (fsi.Access > 0)
                            {
                                folder.Access = -1;
                            }
                            folderItems.Add(fsi);
                        }
                    }

                    //process files
                    processFilesInFolder(ref folderItems, folder, files);

                    if (folderItems.Count > 0)
                    {
                        Dictionary<string, object> folderHash = getMD5FolderHash(folderItems);
                        //at the end of the processing the hash and size are calculated and assigned
                        folder.Hash = folderHash["hash"].ToString();
                        folder.Size = (long)folderHash["size"];

                        updateFolder(ref folder);
                        newFolderProcess = new folderProcessArgs(this, folder,"completed", "\r\n" + tab + (this.stop ? "Exited" : "Done!"));
                        OnNewFolder(this, newFolderProcess);
                        return folder;
                    }
                    else
                    {
                        if (this.stop)// the system received a stop signal don't care the rest of the logic
                        {
                            newFolderProcess = new folderProcessArgs(this, folder,"stopping", "\r\n" + tab + "Exited");
                            OnNewFolder(this, newFolderProcess);
                        }
                        return null;
                    }
                }
                else
                {
                    //the folder is empty
                    // calculate an empty folder hash
                    Dictionary<string, object> folderHash = getMD5FolderHash(new List<fsItem>{folder});
                    //at the end of the processing the hash and size are calculated and assigned
                    folder.Hash = folderHash["hash"].ToString();
                    folder.Size = (long)folderHash["size"];

                    updateFolder(ref folder);
                    newFolderProcess = new folderProcessArgs(this, folder, "empty-complete", "\r\n" + tab + (this.stop ? "Exited" : "Done!"));
                    OnNewFolder(this, newFolderProcess);
                    return folder;

                }
            }
            catch (UnauthorizedAccessException ex)
            {
                newFolderProcess = new folderProcessArgs(this, folder,"ignored", "Access denied to folder :'" + sDir + "'" /*+ ex.Message*/);
                folder.Access = -1;
                OnNewFolder(this, newFolderProcess);
                return folder;
                //throw new Exception("\r\nError while getting files from:'" + sDir + "', with message:" + ex.Message);
            }
            catch (Exception ex)
            {
                throw new Exception("\r\nError while processing:'" + sDir + "', with message:" + ex.Message);
            }
        }
Пример #7
0
        internal fsItem[] getDuplicateFolders()
        {
            DBConnection = new SQLiteConnection(connectionString);
            DBCommand = new SQLiteCommand();
            DBCommand.Connection = DBConnection;
            string s = "";
            string sql = "SELECT hash, count(*) FROM folders WHERE isFile=0 group by hash having count(*)>1";
            DBCommand.CommandText = sql;
            List<string> duplicateHash = new List<string>();
            List<fsItem> duplicateItem = new List<fsItem>();
            SQLiteDataReader dr = null;
            try
            {
                DBConnection.Open();
                dr = DBCommand.ExecuteReader();

                while (dr.Read())
                {
                    object val = dr.GetValue(0);
                    duplicateHash.Add("'" + dr.GetString(0) + "'");//cast is not valid here because we are storing access denied items
                }
                if (duplicateHash.Count > 0)
                {
                    s = duplicateHash.Aggregate((i, j) => i + "," + j);
                    dr.Close();
                    duplicateHash.Clear();
                    sql = @"select id,path,size,isFile from folders where hash in (" + s + ") order by hash";
                    DBCommand.CommandText = sql;
                    dr = DBCommand.ExecuteReader();
                    while (dr.Read())
                    {
                        fsItem fi = new fsItem();
                        fi.Id = dr.GetInt32(0);
                        fi.isFile = dr.GetBoolean(3);
                        fi.Path = dr.GetString(1);
                        fi.Size = dr.GetInt32(2);
                        duplicateItem.Add(fi);

                    }
                    //duplicateHash.ToArray();
                    if (duplicateHash.Count > 0)
                    {
                        s = "duplicate folders\r\n" + duplicateHash.Aggregate((i, j) => i + "\r\n" + j);
                    }
                    dr.Close();

                }
                else
                {
                    s = "no duplicates found";
                    return null;
                }
            }
            catch (Exception ex)
            {
                throw new Exception("Error while accessing DB, MEssage:" + ex.Message);
            }
            finally
            {
                DBConnection.Close();
                //dr.Close();
               // duplicateHash.Clear();
            }
            return duplicateItem.ToArray<fsItem>();
            //return s;
        }