66 lines
2.2 KiB
Python
66 lines
2.2 KiB
Python
#!/usr/bin/env python3
|
|
|
|
import os
|
|
import sys
|
|
import json
|
|
import argparse
|
|
|
|
|
|
# Function to get hard links between source and target directories
|
|
def get_hard_links(source_dir, target_dir):
|
|
# Dictionary to store hard links
|
|
source_links = {}
|
|
target_links = {}
|
|
|
|
# Walk through the source directory to collect hard links
|
|
for dirpath, _, filenames in os.walk(source_dir):
|
|
for filename in filenames:
|
|
filepath = os.path.join(dirpath, filename)
|
|
try:
|
|
inode = os.stat(filepath).st_ino
|
|
if inode not in source_links:
|
|
source_links[inode] = []
|
|
source_links[inode].append(filepath)
|
|
except FileNotFoundError:
|
|
continue
|
|
|
|
# Walk through the target directory to collect hard links
|
|
for dirpath, _, filenames in os.walk(target_dir):
|
|
for filename in filenames:
|
|
filepath = os.path.join(dirpath, filename)
|
|
try:
|
|
inode = os.stat(filepath).st_ino
|
|
if inode not in target_links:
|
|
target_links[inode] = []
|
|
target_links[inode].append(filepath)
|
|
except FileNotFoundError:
|
|
continue
|
|
|
|
# Generate the list of tuples (relative_src, relative_dst)
|
|
hard_links = []
|
|
for inode, target_files in target_links.items():
|
|
if inode in source_links:
|
|
for target_file in target_files:
|
|
relative_target = os.path.relpath(target_file, target_dir)
|
|
for source_file in source_links[inode]:
|
|
relative_source = os.path.relpath(source_file, source_dir)
|
|
hard_links.append([relative_source, relative_target])
|
|
|
|
return hard_links
|
|
|
|
|
|
def main():
|
|
# Argument parser for CLI arguments
|
|
parser = argparse.ArgumentParser(description="Collect hard links between source and target directories.")
|
|
parser.add_argument('source_dir', help="Path to the source directory")
|
|
parser.add_argument('target_dir', help="Path to the target directory")
|
|
|
|
args = parser.parse_args()
|
|
|
|
hard_links = get_hard_links(args.source_dir, args.target_dir)
|
|
print(json.dumps(hard_links, indent=2))
|
|
|
|
|
|
if __name__ == "__main__":
|
|
main()
|