What does Unify?
Unify is a script that with 2 directories makes a new one with the files. This file consider cases where a file is in dir A and not B, and viceversa. And if there is a file in both dirs, the script will choose the newer.
Why Unify is useful?
Do you have a directory or a disk with all information, and with certain structure, and another directory(with the same structure)?
The idea come to my mind, when I store files in a directory A, and at the end of the year, I copy that directory to another disk driver, and safely delete my files in my computer.
However, the next year I have new files in this directory A, and my backup in a disk driver.
How to know which files are new and what I deleted?
For multiple reasons, you can delete a file, but for me, I can remove everything if I want, because I have a disk driver, so I can delete the files to save space.
So Unify comes to my mind when my disk driver contains 4 backups, each one from different year. And there was multiples redundant files to delete, and new ones.
Unify script process
The idea behind the script is represented by the previous image, I treat all the files from both directories(let's say A/ and B/ ) as a set, and make a intersection, and difference to find the files that are represented in the image.
The MOVE operation it's just a way to move files from A/ or B/ to another new C/ directory. And the VERSION operation just checks the filesize of both files inside each of A/ and B/ directory, then get the metadata to find which is newer.
If both files(one from A/ and other one from B/) has the same filesize I do a MOVE operation, but if difers from the filesize, I move both files but their names change to filename_v1.extension and filename_v2.extension.
Struggles and things to improve
I am a beginner in rust, I feel it's like Python with C, combined. And for that reason I read the rust book(a great resource to start). My struggles were I didnt know about much rust, and if there is a function that can simplify my code, that's why implemented that recursion function to "walk" throught the directory(now I know exists a crate that does exactly this 😆). There are other guys with the same approach also.
Things to improve would be the process, some variables are redundant, and some task can be done with threads, because I am just reading and other things like more expresive syntax.
The github repo is: https://github.com/ealvan/unify (see the last tag, I am coding the modularize version)
Feel free to do a pull request or message me. There are two images below that explains the process.
Unify process part 1
Unify process part 2