2021-01-11 12:13:41 -05:00
|
|
|
// Copyright 2018-2021 the Deno authors. All rights reserved. MIT license.
|
2021-10-11 09:21:18 -04:00
|
|
|
import {
|
|
|
|
assert,
|
|
|
|
assertEquals,
|
|
|
|
assertRejects,
|
|
|
|
assertThrows,
|
|
|
|
unitTest,
|
|
|
|
} from "./test_util.ts";
|
2019-04-08 09:11:32 -04:00
|
|
|
|
2020-03-04 11:31:14 -05:00
|
|
|
unitTest(
|
2021-09-22 19:50:50 -04:00
|
|
|
{ permissions: { read: true, write: true } },
|
2021-08-05 07:08:58 -04:00
|
|
|
function linkSyncSuccess() {
|
2020-03-04 11:31:14 -05:00
|
|
|
const testDir = Deno.makeTempDirSync();
|
|
|
|
const oldData = "Hardlink";
|
|
|
|
const oldName = testDir + "/oldname";
|
|
|
|
const newName = testDir + "/newname";
|
|
|
|
Deno.writeFileSync(oldName, new TextEncoder().encode(oldData));
|
|
|
|
// Create the hard link.
|
|
|
|
Deno.linkSync(oldName, newName);
|
|
|
|
// We should expect reading the same content.
|
|
|
|
const newData = new TextDecoder().decode(Deno.readFileSync(newName));
|
|
|
|
assertEquals(oldData, newData);
|
|
|
|
// Writing to newname also affects oldname.
|
|
|
|
const newData2 = "Modified";
|
|
|
|
Deno.writeFileSync(newName, new TextEncoder().encode(newData2));
|
|
|
|
assertEquals(
|
|
|
|
newData2,
|
2020-07-14 15:24:17 -04:00
|
|
|
new TextDecoder().decode(Deno.readFileSync(oldName)),
|
2020-03-04 11:31:14 -05:00
|
|
|
);
|
|
|
|
// Writing to oldname also affects newname.
|
|
|
|
const newData3 = "ModifiedAgain";
|
|
|
|
Deno.writeFileSync(oldName, new TextEncoder().encode(newData3));
|
|
|
|
assertEquals(
|
|
|
|
newData3,
|
2020-07-14 15:24:17 -04:00
|
|
|
new TextDecoder().decode(Deno.readFileSync(newName)),
|
2020-03-04 11:31:14 -05:00
|
|
|
);
|
|
|
|
// Remove oldname. File still accessible through newname.
|
|
|
|
Deno.removeSync(oldName);
|
|
|
|
const newNameStat = Deno.statSync(newName);
|
2020-04-16 01:40:30 -04:00
|
|
|
assert(newNameStat.isFile);
|
|
|
|
assert(!newNameStat.isSymlink); // Not a symlink.
|
2020-03-04 11:31:14 -05:00
|
|
|
assertEquals(
|
|
|
|
newData3,
|
2020-07-14 15:24:17 -04:00
|
|
|
new TextDecoder().decode(Deno.readFileSync(newName)),
|
2020-03-04 11:31:14 -05:00
|
|
|
);
|
2020-07-14 15:24:17 -04:00
|
|
|
},
|
2020-03-04 11:31:14 -05:00
|
|
|
);
|
2019-04-08 09:11:32 -04:00
|
|
|
|
2020-03-04 11:31:14 -05:00
|
|
|
unitTest(
|
2021-09-22 19:50:50 -04:00
|
|
|
{ permissions: { read: true, write: true } },
|
2021-08-05 07:08:58 -04:00
|
|
|
function linkSyncExists() {
|
2020-03-04 11:31:14 -05:00
|
|
|
const testDir = Deno.makeTempDirSync();
|
|
|
|
const oldName = testDir + "/oldname";
|
|
|
|
const newName = testDir + "/newname";
|
|
|
|
Deno.writeFileSync(oldName, new TextEncoder().encode("oldName"));
|
|
|
|
// newname is already created.
|
|
|
|
Deno.writeFileSync(newName, new TextEncoder().encode("newName"));
|
2019-04-08 09:11:32 -04:00
|
|
|
|
2021-10-11 09:21:18 -04:00
|
|
|
assertThrows(
|
|
|
|
() => {
|
|
|
|
Deno.linkSync(oldName, newName);
|
|
|
|
},
|
|
|
|
Deno.errors.AlreadyExists,
|
|
|
|
`link '${oldName}' -> '${newName}'`,
|
|
|
|
);
|
2020-07-14 15:24:17 -04:00
|
|
|
},
|
2020-03-04 11:31:14 -05:00
|
|
|
);
|
2019-04-08 09:11:32 -04:00
|
|
|
|
2020-03-04 11:31:14 -05:00
|
|
|
unitTest(
|
2021-09-22 19:50:50 -04:00
|
|
|
{ permissions: { read: true, write: true } },
|
2021-08-05 07:08:58 -04:00
|
|
|
function linkSyncNotFound() {
|
2020-03-04 11:31:14 -05:00
|
|
|
const testDir = Deno.makeTempDirSync();
|
|
|
|
const oldName = testDir + "/oldname";
|
|
|
|
const newName = testDir + "/newname";
|
2019-04-08 09:11:32 -04:00
|
|
|
|
2021-10-11 09:21:18 -04:00
|
|
|
assertThrows(
|
|
|
|
() => {
|
|
|
|
Deno.linkSync(oldName, newName);
|
|
|
|
},
|
|
|
|
Deno.errors.NotFound,
|
|
|
|
`link '${oldName}' -> '${newName}'`,
|
|
|
|
);
|
2020-07-14 15:24:17 -04:00
|
|
|
},
|
2020-03-04 11:31:14 -05:00
|
|
|
);
|
2019-04-08 09:11:32 -04:00
|
|
|
|
2020-03-04 11:31:14 -05:00
|
|
|
unitTest(
|
2021-09-22 19:50:50 -04:00
|
|
|
{ permissions: { read: false, write: true } },
|
2021-08-05 07:08:58 -04:00
|
|
|
function linkSyncReadPerm() {
|
2020-06-24 18:57:08 -04:00
|
|
|
assertThrows(() => {
|
2020-03-04 11:31:14 -05:00
|
|
|
Deno.linkSync("oldbaddir", "newbaddir");
|
2020-06-24 18:57:08 -04:00
|
|
|
}, Deno.errors.PermissionDenied);
|
2020-07-14 15:24:17 -04:00
|
|
|
},
|
2020-03-04 11:31:14 -05:00
|
|
|
);
|
2019-08-13 09:39:01 -04:00
|
|
|
|
2020-03-04 11:31:14 -05:00
|
|
|
unitTest(
|
2021-09-22 19:50:50 -04:00
|
|
|
{ permissions: { read: true, write: false } },
|
2021-08-05 07:08:58 -04:00
|
|
|
function linkSyncWritePerm() {
|
2020-06-24 18:57:08 -04:00
|
|
|
assertThrows(() => {
|
2020-03-04 11:31:14 -05:00
|
|
|
Deno.linkSync("oldbaddir", "newbaddir");
|
2020-06-24 18:57:08 -04:00
|
|
|
}, Deno.errors.PermissionDenied);
|
2020-07-14 15:24:17 -04:00
|
|
|
},
|
2020-03-04 11:31:14 -05:00
|
|
|
);
|
2019-04-08 09:11:32 -04:00
|
|
|
|
2020-03-04 11:31:14 -05:00
|
|
|
unitTest(
|
2021-09-22 19:50:50 -04:00
|
|
|
{ permissions: { read: true, write: true } },
|
2021-08-05 07:08:58 -04:00
|
|
|
async function linkSuccess() {
|
2020-03-04 11:31:14 -05:00
|
|
|
const testDir = Deno.makeTempDirSync();
|
|
|
|
const oldData = "Hardlink";
|
|
|
|
const oldName = testDir + "/oldname";
|
|
|
|
const newName = testDir + "/newname";
|
|
|
|
Deno.writeFileSync(oldName, new TextEncoder().encode(oldData));
|
|
|
|
// Create the hard link.
|
|
|
|
await Deno.link(oldName, newName);
|
|
|
|
// We should expect reading the same content.
|
|
|
|
const newData = new TextDecoder().decode(Deno.readFileSync(newName));
|
|
|
|
assertEquals(oldData, newData);
|
|
|
|
// Writing to newname also affects oldname.
|
|
|
|
const newData2 = "Modified";
|
|
|
|
Deno.writeFileSync(newName, new TextEncoder().encode(newData2));
|
|
|
|
assertEquals(
|
|
|
|
newData2,
|
2020-07-14 15:24:17 -04:00
|
|
|
new TextDecoder().decode(Deno.readFileSync(oldName)),
|
2020-03-04 11:31:14 -05:00
|
|
|
);
|
|
|
|
// Writing to oldname also affects newname.
|
|
|
|
const newData3 = "ModifiedAgain";
|
|
|
|
Deno.writeFileSync(oldName, new TextEncoder().encode(newData3));
|
|
|
|
assertEquals(
|
|
|
|
newData3,
|
2020-07-14 15:24:17 -04:00
|
|
|
new TextDecoder().decode(Deno.readFileSync(newName)),
|
2020-03-04 11:31:14 -05:00
|
|
|
);
|
|
|
|
// Remove oldname. File still accessible through newname.
|
|
|
|
Deno.removeSync(oldName);
|
|
|
|
const newNameStat = Deno.statSync(newName);
|
2020-04-16 01:40:30 -04:00
|
|
|
assert(newNameStat.isFile);
|
|
|
|
assert(!newNameStat.isSymlink); // Not a symlink.
|
2020-03-04 11:31:14 -05:00
|
|
|
assertEquals(
|
|
|
|
newData3,
|
2020-07-14 15:24:17 -04:00
|
|
|
new TextDecoder().decode(Deno.readFileSync(newName)),
|
2020-03-04 11:31:14 -05:00
|
|
|
);
|
2020-07-14 15:24:17 -04:00
|
|
|
},
|
2020-03-04 11:31:14 -05:00
|
|
|
);
|
2021-10-11 09:21:18 -04:00
|
|
|
|
|
|
|
unitTest(
|
|
|
|
{ permissions: { read: true, write: true } },
|
|
|
|
async function linkExists() {
|
|
|
|
const testDir = Deno.makeTempDirSync();
|
|
|
|
const oldName = testDir + "/oldname";
|
|
|
|
const newName = testDir + "/newname";
|
|
|
|
Deno.writeFileSync(oldName, new TextEncoder().encode("oldName"));
|
|
|
|
// newname is already created.
|
|
|
|
Deno.writeFileSync(newName, new TextEncoder().encode("newName"));
|
|
|
|
|
|
|
|
await assertRejects(
|
|
|
|
async () => {
|
|
|
|
await Deno.link(oldName, newName);
|
|
|
|
},
|
|
|
|
Deno.errors.AlreadyExists,
|
|
|
|
`link '${oldName}' -> '${newName}'`,
|
|
|
|
);
|
|
|
|
},
|
|
|
|
);
|
|
|
|
|
|
|
|
unitTest(
|
|
|
|
{ permissions: { read: true, write: true } },
|
|
|
|
async function linkNotFound() {
|
|
|
|
const testDir = Deno.makeTempDirSync();
|
|
|
|
const oldName = testDir + "/oldname";
|
|
|
|
const newName = testDir + "/newname";
|
|
|
|
|
|
|
|
await assertRejects(
|
|
|
|
async () => {
|
|
|
|
await Deno.link(oldName, newName);
|
|
|
|
},
|
|
|
|
Deno.errors.NotFound,
|
|
|
|
`link '${oldName}' -> '${newName}'`,
|
|
|
|
);
|
|
|
|
},
|
|
|
|
);
|
|
|
|
|
|
|
|
unitTest(
|
|
|
|
{ permissions: { read: false, write: true } },
|
|
|
|
async function linkReadPerm() {
|
|
|
|
await assertRejects(async () => {
|
|
|
|
await Deno.link("oldbaddir", "newbaddir");
|
|
|
|
}, Deno.errors.PermissionDenied);
|
|
|
|
},
|
|
|
|
);
|
|
|
|
|
|
|
|
unitTest(
|
|
|
|
{ permissions: { read: true, write: false } },
|
|
|
|
async function linkWritePerm() {
|
|
|
|
await assertRejects(async () => {
|
|
|
|
await Deno.link("oldbaddir", "newbaddir");
|
|
|
|
}, Deno.errors.PermissionDenied);
|
|
|
|
},
|
|
|
|
);
|