From 6f368f1be421fc4ddb25f0833044f18903892ffa Mon Sep 17 00:00:00 2001 From: Kevin Sawicki Date: Wed, 13 Jun 2012 14:07:23 -0700 Subject: [PATCH] Add initial support for removing path --- Atom-Linux/native_handler.cpp | 46 +++++++++++++++++++++++++++++++++++ Atom-Linux/native_handler.h | 4 +++ 2 files changed, 50 insertions(+) diff --git a/Atom-Linux/native_handler.cpp b/Atom-Linux/native_handler.cpp index 84a0aa564..c901c4f8c 100644 --- a/Atom-Linux/native_handler.cpp +++ b/Atom-Linux/native_handler.cpp @@ -124,6 +124,34 @@ void ListDirectory(string path, vector* paths, bool recursive) { free(children); } +void DeleteContents(string path) { + dirent **children; + const char* dirPath = path.c_str(); + int childrenCount = scandir(dirPath, &children, 0, alphasort); + struct stat statResult; + + for (int i = 0; i < childrenCount; i++) { + if (strcmp(children[i]->d_name, ".") == 0 + || strcmp(children[i]->d_name, "..") == 0) { + free(children[i]); + continue; + } + + string entryPath(path + "/" + children[i]->d_name); + if (stat(entryPath.c_str(), &statResult) != 0) { + free(children[i]); + continue; + } + + if (S_ISDIR(statResult.st_mode)) + DeleteContents(entryPath); + else if (S_ISREG(statResult.st_mode)) + remove(path.c_str()); + } + free(children); + rmdir(dirPath); +} + void NativeHandler::List(const CefString& name, CefRefPtr object, const CefV8ValueList& arguments, CefRefPtr& retval, CefString& exception) { @@ -247,6 +275,22 @@ void NativeHandler::Move(const CefString& name, CefRefPtr object, rename(from.c_str(), to.c_str()); } +void NativeHandler::Remove(const CefString& name, CefRefPtr object, + const CefV8ValueList& arguments, CefRefPtr& retval, + CefString& exception) { + string pathArgument = arguments[0]->GetStringValue().ToString(); + const char* path = pathArgument.c_str(); + + struct stat statInfo; + if (stat(path, &statInfo) != 0) + return; + + if (S_ISREG(statInfo.st_mode)) + remove(path); + else if (S_ISDIR(statInfo.st_mode)) + DeleteContents(pathArgument); +} + bool NativeHandler::Execute(const CefString& name, CefRefPtr object, const CefV8ValueList& arguments, CefRefPtr& retval, CefString& exception) { @@ -280,6 +324,8 @@ bool NativeHandler::Execute(const CefString& name, CefRefPtr object, MakeDirectory(name, object, arguments, retval, exception); else if (name == "move") Move(name, object, arguments, retval, exception); + else if (name == "remove") + Remove(name, object, arguments, retval, exception); else cout << "Unhandled -> " + name.ToString() << " : " << arguments[0]->GetStringValue().ToString() << endl; diff --git a/Atom-Linux/native_handler.h b/Atom-Linux/native_handler.h index ffdbb7697..8b8b8b3fd 100644 --- a/Atom-Linux/native_handler.h +++ b/Atom-Linux/native_handler.h @@ -78,6 +78,10 @@ private: void Move(const CefString& name, CefRefPtr object, const CefV8ValueList& arguments, CefRefPtr& retval, CefString& exception); + + void Remove(const CefString& name, CefRefPtr object, + const CefV8ValueList& arguments, CefRefPtr& retval, + CefString& exception); }; #endif