Skip to content

Commit

Permalink
fix(tar): directory type bug (denoland/deno#6905)
Browse files Browse the repository at this point in the history
  • Loading branch information
fuxingZhang authored and caspervonb committed Jan 31, 2021
1 parent 1f77fd0 commit a831240
Show file tree
Hide file tree
Showing 2 changed files with 49 additions and 11 deletions.
31 changes: 20 additions & 11 deletions archive/tar.ts
Original file line number Diff line number Diff line change
Expand Up @@ -105,15 +105,16 @@ function pad(num: number, bytes: number, base?: number): string {
return "000000000000".substr(numString.length + 12 - bytes) + numString;
}

const types: { [key: string]: string } = {
"": "file",
"0": "file",
"1": "link",
"2": "symlink",
"3": "character-device",
"4": "block-device",
"5": "directory",
};
enum FileTypes {
"file" = 0,
"link" = 1,
"symlink" = 2,
"character-device" = 3,
"block-device" = 4,
"directory" = 5,
"fifo" = 6,
"contiguous-file" = 7,
}

/*
struct posix_header { // byte offset
Expand Down Expand Up @@ -352,6 +353,10 @@ export class Tar {
let info: Deno.FileInfo | undefined;
if (opts.filePath) {
info = await Deno.stat(opts.filePath);
if (info.isDirectory) {
info.size = 0;
opts.reader = new Deno.Buffer();
}
}

const mode = opts.fileMode || (info && info.mode) ||
Expand All @@ -374,6 +379,10 @@ export class Tar {

const fileSize = info?.size ?? opts.contentSize;
assert(fileSize != null, "fileSize must be set");

const type = opts.type
? FileTypes[opts.type as keyof typeof FileTypes]
: (info?.isDirectory ? FileTypes.directory : FileTypes.file);
const tarData: TarDataWithSource = {
fileName,
fileNamePrefix,
Expand All @@ -383,7 +392,7 @@ export class Tar {
fileSize: pad(fileSize, 11),
mtime: pad(mtime, 11),
checksum: " ",
type: "0", // just a file
type: type.toString(),
ustar,
owner: opts.owner || "",
group: opts.group || "",
Expand Down Expand Up @@ -593,7 +602,7 @@ export class Untar {
);

meta.fileSize = parseInt(decoder.decode(header.fileSize), 8);
meta.type = types[meta.type as string] || meta.type;
meta.type = FileTypes[parseInt(meta.type!)] ?? meta.type;

return meta;
};
Expand Down
29 changes: 29 additions & 0 deletions archive/tar_test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -395,3 +395,32 @@ Deno.test("untarLinuxGeneratedTar", async function (): Promise<void> {

file.close();
});

Deno.test("directoryEntryType", async function (): Promise<void> {
const tar = new Tar();

tar.append("directory/", {
reader: new Deno.Buffer(),
contentSize: 0,
type: "directory",
});

const filePath = resolve("archive", "testdata");
tar.append("archive/testdata/", {
filePath,
});

const outputFile = resolve("archive", "testdata", "directory_type_test.tar");
const file = await Deno.open(outputFile, { create: true, write: true });
await Deno.copy(tar.getReader(), file);
await file.close();

const reader = await Deno.open(outputFile, { read: true });
const untar = new Untar(reader);
for await (const entry of untar) {
assertEquals(entry.type, "directory");
}

await reader.close();
await Deno.remove(outputFile);
});

0 comments on commit a831240

Please sign in to comment.