Skip to content

Commit

Permalink
Try to fix #530 - preserve mod times
Browse files Browse the repository at this point in the history
  • Loading branch information
diamondap committed Aug 12, 2022
1 parent 1c5d001 commit 3edda6a
Show file tree
Hide file tree
Showing 4 changed files with 30 additions and 2 deletions.
3 changes: 3 additions & 0 deletions bagit/bagger.js
Original file line number Diff line number Diff line change
Expand Up @@ -198,6 +198,7 @@ class Bagger extends EventEmitter {
// Wait until entire directory is added before
// attaching finish listener, else queue will
// drain more than once.
this.formatWriter.directories[relDestPath] = stats;
await this._addDirectory(absPath, relDestPath, stats);
}
}
Expand Down Expand Up @@ -285,6 +286,8 @@ class Bagger extends EventEmitter {
let relDestPath = bagger._getRelDestPath(fullPath);
if (entry.fileStat.isFile()) {
bagger._addFile(fullPath, relDestPath, entry.fileStat);
} else if (entry.fileStat.isDirectory()) {
bagger.formatWriter.directories[relDestPath] = entry.fileStat;
}
});
fsReader.on('error', function(err) {
Expand Down
4 changes: 4 additions & 0 deletions bagit/bagger.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -61,6 +61,10 @@ test('create() with one dir', done => {
console.log(result.errors)
}

// DEBUG
// console.log(bagger.formatWriter.directories)
// DEBUG

expect(result.errors.length).toEqual(0);
expect(result.succeeded()).toEqual(true);
expect(result.started).not.toBeNull();
Expand Down
10 changes: 10 additions & 0 deletions plugins/formats/write/base_writer.js
Original file line number Diff line number Diff line change
Expand Up @@ -106,6 +106,16 @@ class BaseWriter extends Plugin {
* @type {number}
*/
this.filesWritten = 0;

/**
* This is a map of directory stats. Key is relDestPath,
* value is fs.Stats object. For example, key will be
* "/data/subdir". Value will be the stats of the original
* directory being bagged. This allows us to preserve directory
* attributes such as uid, gid, mode, and mtime when creating
* tar files.
*/
this.directories = {};
}

/**
Expand Down
15 changes: 13 additions & 2 deletions plugins/formats/write/tar_writer.js
Original file line number Diff line number Diff line change
Expand Up @@ -104,10 +104,21 @@ class TarWriter extends BaseWriter {
let dir = '';
for (let i=0; i < parts.length -1; i++) {
dir = dir + parts[i] + '/';
// We cached the stats for the directory in the
// directories map. The key is relDestPath, minus
// the bag name, with no traling slash. E.g. key for
// "MyBag/data/subdir/" is "/data/subdir".
var key = ""
if (i > 0) {
key = dir.split("/").slice(1).join("/").replace(/\/$/, "")
}
if (!this._directoriesAdded.has(dir)) {
let data = Object.assign({}, template);
var data = this.directories[key]
if (!data) {
data = Object.assign({}, template);
data.mtime = new Date();
}
data.relDestPath = dir;
data.mtime = new Date();
this._mkdir(data);
}
}
Expand Down

0 comments on commit 3edda6a

Please sign in to comment.