Skip to content

Commit

Permalink
Update to v1.0.1
Browse files Browse the repository at this point in the history
  • Loading branch information
HubTou authored Feb 25, 2022
1 parent 569bffd commit afbc0b2
Show file tree
Hide file tree
Showing 4 changed files with 44 additions and 18 deletions.
15 changes: 11 additions & 4 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,11 @@
# Installation
Depending on if you want only this tool, the full set of PNU tools, or PNU plus a selection of additional third-parties tools, use one of these commands:

pip install [pnu-dcmp](https://pypi.org/project/pnu-dcmp/)
<br>
pip install [PNU](https://pypi.org/project/PNU/)
<br>
pip install [pytnix](https://pypi.org/project/pytnix/)

# DCMP(1)

Expand All @@ -20,7 +26,7 @@ directory1 directory2

## DESCRIPTION
The **dcmp** utility compares two directories contents and writes the results to the standard output.
By default, dcmp is silent if the directories are the same;
By default, **dcmp** is silent if the directories are the same;
if they differ, the differences are reported with a leading:
* '!' if files contents are different
* '+' if the file or directory appears in directory1 but not in directory2
Expand All @@ -30,16 +36,17 @@ if they differ, the differences are reported with a leading:
If you use the *-s|--silent|--quiet* option, differences won't be reported and the overall result will be returned though the exit status
(thus stopping the comparison at the first difference).

By default, dcmp follows symlinks to directories, unless you tell it not to do so with the *-h|--nosymlinks* option,
or you use the *--dedup* option for deduplicating the first directory, removing identical files and empty directories sitting there.
By default, **dcmp** follows symbolic links to directories, unless you tell it not to do so with the *-h|--nosymlinks* option,
or you use the *--dedup* option for deduplicating the first directory, removing identical files
(ie. those having the same [SHA-256 message digest](https://en.wikipedia.org/wiki/SHA-2)) and symbolic links, as well as empty directories sitting there.

### OPTIONS
The following options are available:

Options | Use
------- | ---
--dedup|In dir1, remove empty dirs and files which are identical to those in dir2
-h\|--nosymlinks|Do not follow symlinks
-h\|--nosymlinks|Do not follow symbolic links
-s\|--silent\|--quiet|Print nothing for differing dirs; return exit status only
-v\|--verbose|Print identical dirs and files names
--debug|Enable debug mode
Expand Down
8 changes: 4 additions & 4 deletions man/dcmp.1
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
.Dd February 20, 2022
.Dd February 25, 2022
.Dt DCMP 1
.Os
.Sh NAME
Expand Down Expand Up @@ -42,21 +42,21 @@ option, differences won't be reported and the overall result will be returned th
.Pp
By default,
.Nm
follows symlinks to directories, unless you tell it not to do so with the
follows symbolic links to directories, unless you tell it not to do so with the
.Fl h|--nosymlinks
option, or you use the
.Fl -dedup
option for deduplicating the
.Em first
directory, removing identical files and empty directories sitting there.
directory, removing identical files (ie. those having the same SHA-256 message digest) and symbolic links, as well as empty directories sitting there.
.Ss OPTIONS
The following options are available:
.Pp
.Op Fl -dedup
In dir1, remove empty dirs and files which are identical to those in dir2
.Pp
.Op Fl h|--nosymlinks
Do not follow symlinks
Do not follow symbolic links
.Pp
.Op Fl s|--silent|--quiet
Print nothing for differing dirs; return exit status only
Expand Down
2 changes: 1 addition & 1 deletion setup.cfg
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ name = pnu_dcmp
description = compare two directories
long_description = file: README.md
long_description_content_type = text/markdown
version = 1.0.0
version = 1.0.1
license = BSD 3-Clause License
license_files = License
author = Hubert Tournier
Expand Down
37 changes: 28 additions & 9 deletions src/dcmp/main.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@
import sys

# Version string used by the what(1) and ident(1) commands:
ID = "@(#) $Id: dcmp - compare two directories, deduplicating if needed v1.0.0 (February 20, 2022) by Hubert Tournier $"
ID = "@(#) $Id: dcmp - compare two directories, deduplicating if needed v1.0.1 (February 25, 2022) by Hubert Tournier $"

BLOCK = 1048576

Expand Down Expand Up @@ -125,7 +125,7 @@ def _process_command_line():

if option == "--dedup":
parameters["Dedup mode"] = True
# caution:
# Let's be cautious against symlink attacks:
parameters["Follow symlinks"] = False

elif option in ("-h", "--nosymlinks"):
Expand Down Expand Up @@ -234,27 +234,46 @@ def main():
exit_status = DIFFERENCES

for file in files1:
file1 = root1 + os.sep + file
if file in files2:
if _hash_file(root1 + os.sep + file) == _hash_file(root2 + os.sep + file):
file2 = root2 + os.sep + file

if os.path.islink(file1):
if os.path.islink(file2) and os.path.readlink(file1) == os.path.readlink(file2):
if parameters["Verbose mode"]:
print(IDENTICAL + " " + file1)
if parameters["Dedup mode"]:
try:
os.remove(file1)
logging.info("Removed symlink: %s", file1)
except:
logging.error("Failed removing symlink: %s", file1)
else:
if parameters["Silent mode"]:
sys.exit(DIFFERENCES)
else:
print(DIFFERENT + " " + file1)
exit_status = DIFFERENCES
elif _hash_file(file1) == _hash_file(file2):
if parameters["Verbose mode"]:
print(IDENTICAL + " " + root1 + os.sep + file)
print(IDENTICAL + " " + file1)
if parameters["Dedup mode"]:
try:
os.remove(root1 + os.sep + file)
logging.info("Removed file: %s", root1 + os.sep + file)
os.remove(file1)
logging.info("Removed file: %s", file1)
except:
logging.error("Failed removing file: %s", root1 + os.sep + file)
logging.error("Failed removing file: %s", file1)
else:
if parameters["Silent mode"]:
sys.exit(DIFFERENCES)
else:
print(DIFFERENT + " " + root1 + os.sep + file)
print(DIFFERENT + " " + file1)
exit_status = DIFFERENCES
else:
if parameters["Silent mode"]:
sys.exit(DIFFERENCES)
else:
print(MISSING_IN_DIR2 + " " + root1 + os.sep + file)
print(MISSING_IN_DIR2 + " " + file1)
exit_status = DIFFERENCES

for file in files2:
Expand Down

0 comments on commit afbc0b2

Please sign in to comment.