Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix directory type #6905

Merged
merged 13 commits into from
Jul 30, 2020
6 changes: 5 additions & 1 deletion std/archive/tar.ts
Original file line number Diff line number Diff line change
Expand Up @@ -352,6 +352,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 Down Expand Up @@ -383,7 +387,7 @@ export class Tar {
fileSize: pad(fileSize, 11),
mtime: pad(mtime, 11),
checksum: " ",
type: "0", // just a file
type: info?.isDirectory ?? opts.type === "directory" ? "5" : "0",
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Isn’t there an enum that can be used here? The value “5” and “0” are not very descriptive. (I see that it was using “0” before but that seems like bad code. Now would be a good opportunity to clean this up.)

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I have updated the code, but I am not sure if it is appropriate.

I referenced some websites, and I add "fifo" and "contiguous-file" into Types

https://github.com/mafintosh/tar-stream
http://www.gnu.org/software/tar/manual/html_node/Standard.html

ustar,
owner: opts.owner || "",
group: opts.group || "",
Expand Down
29 changes: 29 additions & 0 deletions std/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);
});