Skip to content

Commit

Permalink
added verbose flag for cp, mv and rm commands
Browse files Browse the repository at this point in the history
Signed-off-by: Nikolay Kudryavtsev <n.s.kudriavtsev@gmail.com>
  • Loading branch information
nicky1038 committed Aug 23, 2023
1 parent f7a7c75 commit dd92b29
Show file tree
Hide file tree
Showing 3 changed files with 43 additions and 11 deletions.
8 changes: 8 additions & 0 deletions src/cp.js
Expand Up @@ -12,6 +12,7 @@ common.register('cp', _cp, {
'L': 'followsymlink',
'P': 'noFollowsymlink',
'p': 'preserve',
'v': 'verbose',
},
wrapOutput: false,
});
Expand Down Expand Up @@ -84,6 +85,10 @@ function copyFileSync(srcFile, destFile, options) {
fs.closeSync(fdr);
fs.closeSync(fdw);
}

if (options.verbose) {
console.log("copied '" + srcFile + "' -> '" + destFile + "'");
}
}

// Recursively copies 'sourceDir' into 'destDir'
Expand All @@ -108,6 +113,9 @@ function cpdirSyncRecursive(sourceDir, destDir, currentDepth, opts) {
var checkDir = common.statFollowLinks(sourceDir);
try {
fs.mkdirSync(destDir);
if (opts.verbose) {
console.log("created directory '" + sourceDir + "' -> '" + destDir + "'");
}
} catch (e) {
// if the directory already exists, that's okay
if (e.code !== 'EEXIST') throw e;
Expand Down
8 changes: 6 additions & 2 deletions src/mv.js
Expand Up @@ -8,6 +8,7 @@ common.register('mv', _mv, {
cmdOptions: {
'f': '!no_force',
'n': 'no_force',
'v': 'verbose',
},
});

Expand Down Expand Up @@ -102,15 +103,18 @@ function _mv(options, sources, dest) {

try {
fs.renameSync(src, thisDest);
if (options.verbose) {
console.log("renamed '" + src + "' -> '" + thisDest + "'");
}
} catch (e) {
/* istanbul ignore next */
if (e.code === 'EXDEV') {
// If we're trying to `mv` to an external partition, we'll actually need
// to perform a copy and then clean up the original file. If either the
// copy or the rm fails with an exception, we should allow this
// exception to pass up to the top level.
cp({ recursive: true }, src, thisDest);
rm({ recursive: true, force: true }, src);
cp({ recursive: true, verbose: options.verbose }, src, thisDest);
rm({ recursive: true, force: true, verbose: options.verbose }, src);
}
}
}); // forEach(src)
Expand Down
38 changes: 29 additions & 9 deletions src/rm.js
Expand Up @@ -6,6 +6,7 @@ common.register('rm', _rm, {
'f': 'force',
'r': 'recursive',
'R': 'recursive',
'v': 'verbose',
},
});

Expand All @@ -17,7 +18,7 @@ common.register('rm', _rm, {
//
// Licensed under the MIT License
// http://www.opensource.org/licenses/mit-license.php
function rmdirSyncRecursive(dir, force, fromSymlink) {
function rmdirSyncRecursive(dir, force, verbose, fromSymlink) {
var files;

files = fs.readdirSync(dir);
Expand All @@ -28,11 +29,14 @@ function rmdirSyncRecursive(dir, force, fromSymlink) {
var currFile = common.statNoFollowLinks(file);

if (currFile.isDirectory()) { // Recursive function back to the beginning
rmdirSyncRecursive(file, force);
rmdirSyncRecursive(file, force, verbose);
} else if (force || isWriteable(file)) {
// Assume it's a file - perhaps a try/catch belongs here?
try {
common.unlinkSync(file);
if (verbose) {
console.log("removed '" + file + "'");
}
} catch (e) {
/* istanbul ignore next */
common.error('could not remove file (code ' + e.code + '): ' + file, {
Expand All @@ -59,6 +63,9 @@ function rmdirSyncRecursive(dir, force, fromSymlink) {
try {
result = fs.rmdirSync(dir);
if (fs.existsSync(dir)) throw { code: 'EAGAIN' };
if (verbose) {
console.log("removed directory'" + dir + "'");
}
break;
} catch (er) {
/* istanbul ignore next */
Expand Down Expand Up @@ -98,6 +105,9 @@ function handleFile(file, options) {
if (options.force || isWriteable(file)) {
// -f was passed, or file is writable, so it can be removed
common.unlinkSync(file);
if (options.verbose) {
console.log("removed '" + file + "'");
}
} else {
common.error('permission denied: ' + file, { continue: true });
}
Expand All @@ -106,43 +116,53 @@ function handleFile(file, options) {
function handleDirectory(file, options) {
if (options.recursive) {
// -r was passed, so directory can be removed
rmdirSyncRecursive(file, options.force);
rmdirSyncRecursive(file, options.force, options.verbose);
} else {
common.error('path is a directory', { continue: true });
}
}

function handleSymbolicLink(file, options) {
function _unlink() {
common.unlinkSync(file);
if (options.verbose) {
console.log("removed '" + file + "'");
}
}

var stats;
try {
stats = common.statFollowLinks(file);
} catch (e) {
// symlink is broken, so remove the symlink itself
common.unlinkSync(file);
_unlink();
return;
}

if (stats.isFile()) {
common.unlinkSync(file);
_unlink();
} else if (stats.isDirectory()) {
if (file[file.length - 1] === '/') {
// trailing separator, so remove the contents, not the link
if (options.recursive) {
// -r was passed, so directory can be removed
var fromSymlink = true;
rmdirSyncRecursive(file, options.force, fromSymlink);
rmdirSyncRecursive(file, options.force, options.verbose, fromSymlink);
} else {
common.error('path is a directory', { continue: true });
}
} else {
// no trailing separator, so remove the link
common.unlinkSync(file);
_unlink();
}
}
}

function handleFIFO(file) {
function handleFIFO(file, options) {
common.unlinkSync(file);
if (options.verbose) {
console.log("removed '" + file + "'");
}
}

//@
Expand Down Expand Up @@ -193,7 +213,7 @@ function _rm(options, files) {
} else if (lstats.isSymbolicLink()) {
handleSymbolicLink(file, options);
} else if (lstats.isFIFO()) {
handleFIFO(file);
handleFIFO(file, options);
}
}); // forEach(file)
return '';
Expand Down

0 comments on commit dd92b29

Please sign in to comment.