mirror of
https://github.com/RGBCube/serenity
synced 2025-05-31 13:38:11 +00:00
rm: Use Core::File::remove
This commit is contained in:
parent
f4569ecf3c
commit
ecafee86f8
1 changed files with 12 additions and 48 deletions
|
@ -24,55 +24,13 @@
|
||||||
* OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
|
* OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
|
||||||
*/
|
*/
|
||||||
|
|
||||||
#include <AK/String.h>
|
|
||||||
#include <AK/StringBuilder.h>
|
#include <AK/StringBuilder.h>
|
||||||
#include <AK/Vector.h>
|
#include <AK/Vector.h>
|
||||||
#include <LibCore/ArgsParser.h>
|
#include <LibCore/ArgsParser.h>
|
||||||
#include <LibCore/DirIterator.h>
|
#include <LibCore/File.h>
|
||||||
#include <dirent.h>
|
|
||||||
#include <stdio.h>
|
#include <stdio.h>
|
||||||
#include <string.h>
|
|
||||||
#include <sys/stat.h>
|
|
||||||
#include <unistd.h>
|
#include <unistd.h>
|
||||||
|
|
||||||
static int remove(bool recursive, bool force, String path)
|
|
||||||
{
|
|
||||||
struct stat path_stat;
|
|
||||||
if (lstat(path.characters(), &path_stat) < 0) {
|
|
||||||
if (!force)
|
|
||||||
perror("lstat");
|
|
||||||
return force ? 0 : 1;
|
|
||||||
}
|
|
||||||
|
|
||||||
if (S_ISDIR(path_stat.st_mode) && recursive) {
|
|
||||||
auto di = Core::DirIterator(path, Core::DirIterator::SkipParentAndBaseDir);
|
|
||||||
if (di.has_error()) {
|
|
||||||
if (!force)
|
|
||||||
fprintf(stderr, "DirIterator: %s\n", di.error_string());
|
|
||||||
return 1;
|
|
||||||
}
|
|
||||||
|
|
||||||
while (di.has_next()) {
|
|
||||||
int s = remove(true, force, di.next_full_path());
|
|
||||||
if (s != 0 && !force)
|
|
||||||
return s;
|
|
||||||
}
|
|
||||||
|
|
||||||
int s = rmdir(path.characters());
|
|
||||||
if (s < 0 && !force) {
|
|
||||||
perror("rmdir");
|
|
||||||
return 1;
|
|
||||||
}
|
|
||||||
} else {
|
|
||||||
int rc = unlink(path.characters());
|
|
||||||
if (rc < 0 && !force) {
|
|
||||||
perror("unlink");
|
|
||||||
return 1;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
return 0;
|
|
||||||
}
|
|
||||||
|
|
||||||
int main(int argc, char** argv)
|
int main(int argc, char** argv)
|
||||||
{
|
{
|
||||||
if (pledge("stdio rpath cpath", nullptr) < 0) {
|
if (pledge("stdio rpath cpath", nullptr) < 0) {
|
||||||
|
@ -92,11 +50,17 @@ int main(int argc, char** argv)
|
||||||
args_parser.add_positional_argument(paths, "Path(s) to remove", "path");
|
args_parser.add_positional_argument(paths, "Path(s) to remove", "path");
|
||||||
args_parser.parse(argc, argv);
|
args_parser.parse(argc, argv);
|
||||||
|
|
||||||
int rc = 0;
|
bool had_errors = false;
|
||||||
for (auto& path : paths) {
|
for (auto& path : paths) {
|
||||||
rc |= remove(recursive, force, path);
|
auto result = Core::File::remove(path, recursive ? Core::File::RecursionMode::Allowed : Core::File::RecursionMode::Disallowed, force);
|
||||||
if (verbose && rc == 0)
|
|
||||||
printf("removed '%s'\n", path);
|
if (result.is_error()) {
|
||||||
|
warnln("rm: cannot remove '{}': {}", path, result.error().error_code);
|
||||||
|
had_errors = true;
|
||||||
|
}
|
||||||
|
|
||||||
|
if (verbose)
|
||||||
|
outln("removed '{}'", path);
|
||||||
}
|
}
|
||||||
return rc;
|
return had_errors ? 1 : 0;
|
||||||
}
|
}
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue