Skip to content

Commit

Permalink
Avoid reserved Windows filenames like AUX, NUL, PRN and COM1
Browse files Browse the repository at this point in the history
  • Loading branch information
david-04 committed Apr 23, 2022
1 parent 068b00a commit 036ff22
Show file tree
Hide file tree
Showing 5 changed files with 76 additions and 14 deletions.
4 changes: 4 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,9 @@
# Change Log

## [1.0.2](https://github.com/david-04/7-sync/releases/tag/v1.0.2) (2022-04-23)

- Avoid Windows reserved filenames (e.g. `PRN`, `AUX`, `NUL`, `COM1`, ...)

## [1.0.1](https://github.com/david-04/7-sync/releases/tag/v1.0.1) (2022-04-15)

- Fixed link to CHANGELOG
Expand Down
43 changes: 36 additions & 7 deletions package/7-sync.js
Original file line number Diff line number Diff line change
Expand Up @@ -98,7 +98,7 @@ process.on("beforeExit", () => {
Application.main();
}
});
const APPLICATION_VERSION = "1.0.1";
const APPLICATION_VERSION = "1.0.2";
const COPYRIGHT_OWNER = "David Hofmann";
const COPYRIGHT_YEARS = "2022";
class Context {
Expand Down Expand Up @@ -1127,17 +1127,20 @@ class FilenameEnumerator {
next = next ? this.calculateNext(next) : this.firstLetter;
const filename = prefix + next + suffix;
const filenameWithPath = node.path.join(path, filename);
if (FileUtils.exists(filenameWithPath)) {
this.logger.warn(`The next filename is already occupied: ${path} => ${filename}`);
}
else if (!MetadataManager.isMetadataArchiveName(next)) {
return { enumeratedName: next, filename, filenameWithPath };
if (!FilenameEnumerator.RESERVED_NAMES.has(next.toLowerCase())) {
if (FileUtils.exists(filenameWithPath)) {
this.logger.warn(`The next filename is already occupied: ${path} => ${filename}`);
}
else if (!MetadataManager.isMetadataArchiveName(next)) {
return { enumeratedName: next, filename, filenameWithPath };
}
}
}
}
recalculateLastFilename(last, filenames) {
const ext = ".7z";
return filenames
.map(filename => filename.endsWith(".7z") ? filename.substring(0, filename.length - 3) : filename)
.map(filename => filename.endsWith(ext) ? filename.substring(0, filename.length - ext.length) : filename)
.filter(basename => this.isEnumeratedName(basename))
.reduce((a, b) => this.getLastFilename(a, b), this.isEnumeratedName(last) ? last : "");
}
Expand Down Expand Up @@ -1176,6 +1179,32 @@ class FilenameEnumerator {
}
}
FilenameEnumerator.LETTERS = "abcdefghijkmnpqrstuvwxyz123456789";
FilenameEnumerator.RESERVED_NAMES = new Set([
"CON",
"PRN",
"AUX",
"NUL",
"COM1",
"COM2",
"COM3",
"COM4",
"COM5",
"COM6",
"COM7",
"COM8",
"COM9",
"COM0",
"LPT1",
"LPT2",
"LPT3",
"LPT4",
"LPT5",
"LPT6",
"LPT7",
"LPT8",
"LPT9",
"LPT0"
].map(name => name.toLowerCase()));
var _a;
class InteractivePrompt {
static prompt(options) {
Expand Down
2 changes: 1 addition & 1 deletion package/package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "7-sync",
"version": "1.0.1",
"version": "1.0.2",
"description": "A wrapper for 7-Zip to create cloud-friendly encrypted backups",
"main": "index.js",
"repository": {
Expand Down
39 changes: 34 additions & 5 deletions src/features/filename-enumerator.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,32 @@
class FilenameEnumerator {

private static readonly LETTERS = "abcdefghijkmnpqrstuvwxyz123456789"; // cspell: disable-line
private static readonly RESERVED_NAMES = new Set([
"CON",
"PRN",
"AUX",
"NUL",
"COM1",
"COM2",
"COM3",
"COM4",
"COM5",
"COM6",
"COM7",
"COM8",
"COM9",
"COM0",
"LPT1",
"LPT2",
"LPT3",
"LPT4",
"LPT5",
"LPT6",
"LPT7",
"LPT8",
"LPT9",
"LPT0"
].map(name => name.toLowerCase()));

private readonly firstLetter;
private readonly nextLetter;
Expand Down Expand Up @@ -99,10 +125,12 @@ class FilenameEnumerator {
next = next ? this.calculateNext(next) : this.firstLetter;
const filename = prefix + next + suffix;
const filenameWithPath = node.path.join(path, filename);
if (FileUtils.exists(filenameWithPath)) {
this.logger.warn(`The next filename is already occupied: ${path} => ${filename}`);
} else if (!MetadataManager.isMetadataArchiveName(next)) {
return { enumeratedName: next, filename, filenameWithPath };
if (!FilenameEnumerator.RESERVED_NAMES.has(next.toLowerCase())) {
if (FileUtils.exists(filenameWithPath)) {
this.logger.warn(`The next filename is already occupied: ${path} => ${filename}`);
} else if (!MetadataManager.isMetadataArchiveName(next)) {
return { enumeratedName: next, filename, filenameWithPath };
}
}
}
}
Expand All @@ -112,8 +140,9 @@ class FilenameEnumerator {
//------------------------------------------------------------------------------------------------------------------

public recalculateLastFilename(last: string, filenames: string[]) {
const ext = ".7z";
return filenames
.map(filename => filename.endsWith(".7z") ? filename.substring(0, filename.length - 3) : filename)
.map(filename => filename.endsWith(ext) ? filename.substring(0, filename.length - ext.length) : filename)
.filter(basename => this.isEnumeratedName(basename))
.reduce((a, b) => this.getLastFilename(a, b), this.isEnumeratedName(last) ? last : "");
}
Expand Down
2 changes: 1 addition & 1 deletion src/version.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
const APPLICATION_VERSION = "1.0.1"
const APPLICATION_VERSION = "1.0.2"
const COPYRIGHT_OWNER = "David Hofmann";
const COPYRIGHT_YEARS = "2022";

0 comments on commit 036ff22

Please sign in to comment.