Skip to content

Commit

Permalink
resolve relative directories before creating symlinks to them (cross-…
Browse files Browse the repository at this point in the history
…seed#537)

I noticed a bug with symbolic links while I was testing cross-seed#503—symlinks
were being created pointing to relative paths from the POV of
cross-seed's working directory.

Some other changes with mkdir— `recursive: true` will silently succeed
if the directory already exists, so we are able to skip some
`existsSync` precondition checks and some `try/catch`es as well.
  • Loading branch information
mmgoodnow authored Nov 3, 2023
1 parent 9c802e9 commit 3a7e385
Show file tree
Hide file tree
Showing 3 changed files with 29 additions and 28 deletions.
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,7 @@ output/
input/
*.heapsnapshot
qbit/
docker/
cache.json
docker-compose.yml
utils.zsh
Expand Down
5 changes: 5 additions & 0 deletions .idea/codeStyles/Project.xml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

51 changes: 23 additions & 28 deletions src/action.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ import {
statSync,
symlinkSync,
} from "fs";
import path from "path";
import { join, resolve, dirname, basename } from "path";
import { getClient } from "./clients/TorrentClient.js";
import {
Action,
Expand Down Expand Up @@ -48,21 +48,19 @@ export async function performAction(
} else if (decision == Decision.MATCH_SIZE_ONLY) {
// Size only matching is only supported for single file or
// single, nested file torrents.
const candidateParentDir = path.dirname(newMeta.files[0].path);
const candidateParentDir = dirname(newMeta.files[0].path);
let correctedlinkDir = linkDir;

// Candidate is single, nested file
if (candidateParentDir != ".") {
if (!existsSync(path.join(linkDir, candidateParentDir))) {
mkdirSync(path.join(linkDir, candidateParentDir), {
recursive: true,
});
}
correctedlinkDir = path.join(linkDir, candidateParentDir);
if (candidateParentDir !== ".") {
mkdirSync(join(linkDir, candidateParentDir), {
recursive: true,
});
correctedlinkDir = join(linkDir, candidateParentDir);
}
linkFile(
searchee.path,
path.join(correctedlinkDir, newMeta.files[0].name)
join(correctedlinkDir, newMeta.files[0].name)
);
}
}
Expand Down Expand Up @@ -125,32 +123,29 @@ function linkExact(oldPath: string, newPath: string) {
return;
}
if (statSync(oldPath).isFile()) {
if (!existsSync(path.join(newPath, path.basename(oldPath)))) {
linkFile(oldPath, path.join(newPath, path.basename(oldPath)));
if (!existsSync(join(newPath, basename(oldPath)))) {
linkFile(oldPath, join(newPath, basename(oldPath)));
}
return;
}

try {
mkdirSync(path.join(newPath, path.basename(oldPath)));
} catch (e) {
// skip if it already exists
}
mkdirSync(join(newPath, basename(oldPath)), { recursive: true });
readdirSync(oldPath).forEach((file) => {
linkExact(
path.join(oldPath, file),
path.join(newPath, path.basename(oldPath))
);
linkExact(join(oldPath, file), join(newPath, basename(oldPath)));
});
}

function linkFile(oldPath: string, newPath: string) {
const { linkType } = getRuntimeConfig();
if (existsSync(newPath)) return;

if (linkType === LinkType.HARDLINK) {
linkSync(oldPath, newPath);
} else {
symlinkSync(oldPath, newPath);
try {
if (linkType === LinkType.HARDLINK) {
linkSync(oldPath, newPath);
} else {
// we need to resolve because symlinks are resolved outside
// the context of cross-seed's working directory
symlinkSync(oldPath, resolve(newPath));
}
} catch (e) {
if (e.code === "EEXIST") return;
throw e;
}
}

0 comments on commit 3a7e385

Please sign in to comment.