Skip to content

Commit

Permalink
fix: escape rsync special characters in filenames on windows (#4434)
Browse files Browse the repository at this point in the history
Co-authored-by: Steffen Neubauer <[email protected]>
  • Loading branch information
Walther and stefreak committed May 25, 2023
1 parent 6d61c23 commit 17656f7
Show file tree
Hide file tree
Showing 2 changed files with 53 additions and 2 deletions.
9 changes: 7 additions & 2 deletions core/src/util/sync.ts
Original file line number Diff line number Diff line change
Expand Up @@ -52,8 +52,13 @@ export async function syncWithOptions({
// Workaround for this issue: https://stackoverflow.com/questions/1813907
opts.push("--include-from=-", "--exclude=*", "--delete-excluded")

// -> Make sure the file list is anchored (otherwise filenames are matched as patterns)
files = files.map((f) => "/" + f)
files = files.map((f) => {
// -> Make sure the file list is anchored (otherwise filenames are matched as patterns)
let filename = "/" + f
// -> Escape rsync include/exclude wildcard characters https://linux.die.net/man/1/rsync
filename = filename.replaceAll(/(\[|\?|\*)/g, "\\$1")
return filename
})

input = "/**/\n" + files.join("\n")
}
Expand Down
46 changes: 46 additions & 0 deletions core/test/unit/src/build-staging/build-staging.ts
Original file line number Diff line number Diff line change
Expand Up @@ -656,5 +656,51 @@ function commonSyncTests(legacyBuildSync: boolean) {

expect(await listFiles(targetRoot)).to.eql(["subdir/a"])
})

it("correctly handles special characters in the file name, withDelete true", async () => {
const specialFilenames = ["[id].vue", "*foo", "bla?"]
const a = join(tmpPath, "a")
const b = join(tmpPath, "b")
await ensureDir(a)
for (const filename of specialFilenames) {
await writeFile(join(a, filename), "foo")
}
await ensureDir(b)
await sync({
log,
sourceRoot: tmpPath,
sourceRelPath: "a/",
files: specialFilenames,
targetRoot: b,
withDelete: true,
})
for (const filename of specialFilenames) {
const data = (await readFile(join(b, filename))).toString()
expect(data).to.equal("foo")
}
})

it("correctly handles special characters in the file name, withDelete false", async () => {
const specialFilenames = ["[id].vue", "*foo", "bla?"]
const a = join(tmpPath, "a")
const b = join(tmpPath, "b")
await ensureDir(a)
for (const filename of specialFilenames) {
await writeFile(join(a, filename), "foo")
}
await ensureDir(b)
await sync({
log,
sourceRoot: tmpPath,
sourceRelPath: "a/",
files: specialFilenames,
targetRoot: b,
withDelete: false,
})
for (const filename of specialFilenames) {
const data = (await readFile(join(b, filename))).toString()
expect(data).to.equal("foo")
}
})
})
}

0 comments on commit 17656f7

Please sign in to comment.