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
Add '-p' (preserve) option to 'cp', fixes #771 #841
Changes from 3 commits
bafc9a9
d84236d
3ede418
4760a76
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -756,3 +756,29 @@ test('should not overwrite recently created files (not give error no-force mode) | |
// Ensure First file is copied | ||
t.is(shell.cat(`${t.context.tmp}/file1`).toString(), 'test1'); | ||
}); | ||
|
||
test('cp -p should preserve mode, ownership, and timestamp', t => { | ||
const result = shell.cp('-p', 'test/resources/file1', `${t.context.tmp}/preservedFile1`); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I'll follow-up to this to We should probably also chmod the file too, to double-check the mode gets copied correctly (and it isn't the default mode). |
||
const stat = common.statFollowLinks('test/resources/file1'); | ||
const statOfResult = common.statFollowLinks(`${t.context.tmp}/preservedFile1`); | ||
|
||
t.is(result.code, 0); | ||
t.is(stat.mtime.getTime(), statOfResult.mtime.getTime()); | ||
t.is(stat.atime.getTime(), statOfResult.atime.getTime()); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. https://travis-ci.org/shelljs/shelljs/builds/374922671?utm_source=github_status&utm_medium=notification There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Probably a race. Try adding a sleep to the test (see |
||
t.is(stat.mode, statOfResult.mode); | ||
t.is(stat.uid, statOfResult.uid); | ||
t.is(stat.gid, statOfResult.gid); | ||
}); | ||
|
||
test('cp -p should preserve mode, ownership, and timestamp of symlink', t => { | ||
const result = shell.cp('-p', 'test/resources/link', `${t.context.tmp}/copiedLink`); | ||
const stat = common.statFollowLinks('test/resources/link'); | ||
const statOfResult = common.statFollowLinks(`${t.context.tmp}/copiedLink`); | ||
|
||
t.is(result.code, 0); | ||
t.is(stat.mtime.getTime(), statOfResult.mtime.getTime()); | ||
t.is(stat.atime.getTime(), statOfResult.atime.getTime()); | ||
t.is(stat.mode, statOfResult.mode); | ||
t.is(stat.uid, statOfResult.uid); | ||
t.is(stat.rid, statOfResult.rid); | ||
}); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
On linux,
fs.utimesSync()
always produces timestamps rounded off toseconds
but original files could have timestamps of millisecond level. But I still don't why is that so I probably need more time to figure it out.However
fs.futimesSync()
could produce timestamps of millisecond level. So I put it this way for now.Also I tried to add sleep in test but it didn't work.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Huh, ok. This implementation is fine. I will add a comment explaining that
utimesSync
doesn't work butfutimesSync
does.