2022-04-28 18:43:14 -07:00
|
|
|
/* jslint node:true */
|
2026-02-14 09:53:14 +01:00
|
|
|
|
|
|
|
|
import * as common from './common.js';
|
|
|
|
|
import DataLayout from '../datalayout.js';
|
|
|
|
|
import * as tgz from '../backupformat/tgz.js';
|
|
|
|
|
const EnsureFileSizeStream = tgz._EnsureFileSizeStream;
|
|
|
|
|
import expect from 'expect.js';
|
|
|
|
|
import fs from 'node:fs';
|
|
|
|
|
import os from 'node:os';
|
|
|
|
|
import path from 'node:path';
|
|
|
|
|
import * as rsync from '../backupformat/rsync.js';
|
|
|
|
|
import safe from 'safetydance';
|
|
|
|
|
import stream from 'node:stream/promises';
|
|
|
|
|
|
2022-04-28 18:43:14 -07:00
|
|
|
/* global it:false */
|
|
|
|
|
/* global describe:false */
|
|
|
|
|
/* global before:false */
|
|
|
|
|
/* global after:false */
|
|
|
|
|
|
|
|
|
|
describe('backuptask', function () {
|
|
|
|
|
const { setup, cleanup, createTree } = common;
|
|
|
|
|
|
|
|
|
|
before(setup);
|
|
|
|
|
after(cleanup);
|
|
|
|
|
|
2024-07-18 15:39:45 +02:00
|
|
|
describe('EnsureFileSizeStream', function () {
|
|
|
|
|
const name = 'eberswalde.txt';
|
|
|
|
|
const data = Buffer.from('This file has 22 bytes');
|
|
|
|
|
before(function () {
|
|
|
|
|
fs.writeFileSync(`/tmp/${name}`, data);
|
|
|
|
|
});
|
|
|
|
|
after(function () {
|
|
|
|
|
fs.rmSync(`/tmp/${name}`);
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
it('correct size', async function () {
|
|
|
|
|
const efs = new EnsureFileSizeStream({ name, size: 22 });
|
|
|
|
|
const ins = fs.createReadStream(`/tmp/${name}`);
|
|
|
|
|
const outs = fs.createWriteStream('/tmp/out.txt');
|
|
|
|
|
const [error] = await safe(stream.pipeline(ins, efs, outs));
|
|
|
|
|
expect(error).to.be(null);
|
|
|
|
|
const out = fs.readFileSync('/tmp/out.txt');
|
|
|
|
|
expect(out).to.eql(data);
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
it('overflow', async function () {
|
|
|
|
|
const efs = new EnsureFileSizeStream({ name, size: 20 });
|
|
|
|
|
const ins = fs.createReadStream(`/tmp/${name}`);
|
|
|
|
|
const outs = fs.createWriteStream('/tmp/out.txt');
|
|
|
|
|
const [error] = await safe(stream.pipeline(ins, efs, outs));
|
|
|
|
|
expect(error).to.be(null);
|
|
|
|
|
const out = fs.readFileSync('/tmp/out.txt');
|
|
|
|
|
expect(out).to.eql(data.subarray(0, 20));
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
it('underflow', async function () {
|
|
|
|
|
const efs = new EnsureFileSizeStream({ name, size: 30 });
|
|
|
|
|
const ins = fs.createReadStream(`/tmp/${name}`);
|
|
|
|
|
const outs = fs.createWriteStream('/tmp/out.txt');
|
|
|
|
|
const [error] = await safe(stream.pipeline(ins, efs, outs));
|
|
|
|
|
expect(error).to.be(null);
|
|
|
|
|
const out = fs.readFileSync('/tmp/out.txt');
|
|
|
|
|
expect(out).to.eql(Buffer.concat([data, Buffer.alloc(8)]));
|
|
|
|
|
});
|
|
|
|
|
});
|
|
|
|
|
|
2022-04-28 18:43:14 -07:00
|
|
|
describe('fs meta data', function () {
|
|
|
|
|
let tmpdir;
|
|
|
|
|
before(function () {
|
|
|
|
|
tmpdir = fs.mkdtempSync(path.join(os.tmpdir(), 'backups-test'));
|
|
|
|
|
});
|
|
|
|
|
after(function () {
|
|
|
|
|
fs.rmSync(tmpdir, { recursive: true, force: true });
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
it('saves special files', async function () {
|
|
|
|
|
createTree(tmpdir, { 'data': { 'subdir': { 'emptydir': { } } }, 'dir2': { 'file': 'stuff' } });
|
|
|
|
|
fs.chmodSync(path.join(tmpdir, 'dir2/file'), parseInt('0755', 8));
|
|
|
|
|
|
2026-02-14 09:53:14 +01:00
|
|
|
const dataLayout = new DataLayout(tmpdir, []);
|
2022-04-28 18:43:14 -07:00
|
|
|
|
|
|
|
|
await rsync._saveFsMetadata(dataLayout, `${dataLayout.localRoot()}/fsmetadata.json`);
|
|
|
|
|
|
|
|
|
|
const emptyDirs = JSON.parse(fs.readFileSync(path.join(tmpdir, 'fsmetadata.json'), 'utf8')).emptyDirs;
|
|
|
|
|
expect(emptyDirs).to.eql(['./data/subdir/emptydir']);
|
|
|
|
|
|
|
|
|
|
const execFiles = JSON.parse(fs.readFileSync(path.join(tmpdir, 'fsmetadata.json'), 'utf8')).execFiles;
|
|
|
|
|
expect(execFiles).to.eql(['./dir2/file']);
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
it('restores special files', async function () {
|
|
|
|
|
fs.rmSync(path.join(tmpdir, 'data'), { recursive: true, force: true });
|
|
|
|
|
|
|
|
|
|
expect(fs.existsSync(path.join(tmpdir, 'data/subdir/emptydir'))).to.be(false); // just make sure rimraf worked
|
|
|
|
|
|
2026-02-14 09:53:14 +01:00
|
|
|
const dataLayout = new DataLayout(tmpdir, []);
|
2022-04-28 18:43:14 -07:00
|
|
|
|
|
|
|
|
await rsync._restoreFsMetadata(dataLayout, `${dataLayout.localRoot()}/fsmetadata.json`);
|
|
|
|
|
|
|
|
|
|
expect(fs.existsSync(path.join(tmpdir, 'data/subdir/emptydir'))).to.be(true);
|
|
|
|
|
const mode = fs.statSync(path.join(tmpdir, 'dir2/file')).mode;
|
|
|
|
|
expect(mode & ~fs.constants.S_IFREG).to.be(parseInt('0755', 8));
|
|
|
|
|
});
|
|
|
|
|
});
|
|
|
|
|
});
|