diff --git a/.gitignore b/.gitignore index 1dd71538..cb680ea0 100755 --- a/.gitignore +++ b/.gitignore @@ -1,3 +1,4 @@ +/NidiumTools konstruct*.log configure*c bin diff --git a/src/Binding/JSCanvas.cpp b/src/Binding/JSCanvas.cpp index 64c79e9d..7b4d3f54 100644 --- a/src/Binding/JSCanvas.cpp +++ b/src/Binding/JSCanvas.cpp @@ -120,7 +120,7 @@ bool JSCanvas::JS_getDimensions(JSContext *cx, JS::CallArgs &args) NIDIUM_JSOBJ_SET_PROP_FLOAT(out, "top", top); args.rval().setObject(*out); - + return true; } @@ -1601,7 +1601,7 @@ JSCanvas *JSCanvas::Constructor(JSContext *cx, JS::CallArgs &args, if (args.length() >= 2 && !args[1].isNullOrUndefined() && !JS::ToNumber(cx, args[1], &height)) { height = NAN; } - + handler = new CanvasHandler(width, height, Context::GetObject(cx), true); diff --git a/src/Binding/JSFS.cpp b/src/Binding/JSFS.cpp index 3c389c41..39974262 100644 --- a/src/Binding/JSFS.cpp +++ b/src/Binding/JSFS.cpp @@ -12,8 +12,11 @@ #include #include +#include "IO/FileSystem.h" + using Nidium::Core::SharedMessages; using Nidium::Core::Task; +using Nidium::IO::FileSystem::mkdirp; namespace Nidium { namespace Binding { @@ -104,6 +107,103 @@ class JSFSAsyncHandler : public JSAsyncHandler // }}} // {{{ Implementation + +bool JSFS::JS_isDir(JSContext *cx, JS::CallArgs &args) +{ + JS::RootedString path(cx); + + if (!JS_ConvertArguments(cx, args, "S", path.address())) { + + args.rval().setBoolean(false); + + return true; + + } + + JSAutoByteString cpath(cx, path); + + struct stat statbuf; + + if (stat(strdup(cpath.ptr()), &statbuf) == -1) { + + args.rval().setBoolean(false); + + return true; + + } + + args.rval().setBoolean(S_ISDIR(statbuf.st_mode)); + + return true; + +} + +bool JSFS::JS_isFile(JSContext *cx, JS::CallArgs &args) +{ + JS::RootedString path(cx); + + if (!JS_ConvertArguments(cx, args, "S", path.address())) { + + args.rval().setBoolean(false); + + return true; + + } + + JSAutoByteString cpath(cx, path); + + struct stat statbuf; + + if (stat(strdup(cpath.ptr()), &statbuf) == -1) { + + args.rval().setBoolean(false); + + return true; + + } + + args.rval().setBoolean(S_ISREG(statbuf.st_mode)); + + return true; + +} + +bool JSFS::JS_createDirSync(JSContext *cx, JS::CallArgs &args) +{ + JS::RootedString path(cx); + + if (!JS_ConvertArguments(cx, args, "S", path.address())) { + + args.rval().setBoolean(false); + + return true; + + } + + + JSAutoByteString cpath(cx, path); + struct stat statbuf; + + stat(strdup(cpath.ptr()), &statbuf); + + if (S_ISDIR(statbuf.st_mode)) { + + args.rval().setBoolean(true); + + return true; + + } + + + mkdirp(cpath.ptr()); + stat(strdup(cpath.ptr()), &statbuf); + + args.rval().setBoolean(S_ISDIR(statbuf.st_mode)); + + return false; + +} + bool JSFS::JS_readDir(JSContext *cx, JS::CallArgs &args) { JS::RootedString path(cx); @@ -130,10 +230,36 @@ bool JSFS::JS_readDir(JSContext *cx, JS::CallArgs &args) return true; } +bool JSFS::JS_removeSync(JSContext *cx, JS::CallArgs &args) +{ + JS::RootedString path(cx); + + if (!JS_ConvertArguments(cx, args, "S", path.address())) { + + args.rval().setBoolean(false); + + return true; + + } + + JSAutoByteString cpath(cx, path); + + int retval = remove(strdup(cpath.ptr())); + + args.rval().setBoolean(retval == 0); + + return true; + +} + JSFunctionSpec *JSFS::ListMethods() { static JSFunctionSpec funcs[] = { + CLASSMAPPER_FN(JSFS, createDirSync, 1), + CLASSMAPPER_FN(JSFS, isDir, 1), + CLASSMAPPER_FN(JSFS, isFile, 1), CLASSMAPPER_FN(JSFS, readDir, 2), + CLASSMAPPER_FN(JSFS, removeSync, 1), JS_FS_END }; diff --git a/src/Binding/JSFS.h b/src/Binding/JSFS.h index ffd61725..de23632a 100644 --- a/src/Binding/JSFS.h +++ b/src/Binding/JSFS.h @@ -81,7 +81,11 @@ class JSFS : public ClassMapper, public Nidium::Core::Managed static void RegisterObject(JSContext *cx); static JSFunctionSpec *ListMethods(); protected: + NIDIUM_DECL_JSCALL(createDirSync); + NIDIUM_DECL_JSCALL(isDir); + NIDIUM_DECL_JSCALL(isFile); NIDIUM_DECL_JSCALL(readDir); + NIDIUM_DECL_JSCALL(removeSync); }; } // namespace Binding diff --git a/src/Binding/JSFile.cpp b/src/Binding/JSFile.cpp index 0af2e090..0923f8a6 100644 --- a/src/Binding/JSFile.cpp +++ b/src/Binding/JSFile.cpp @@ -273,34 +273,6 @@ bool JSFile::JSGetter_filename(JSContext *cx, JS::MutableHandleValue vp) return true; } -bool JSFile::JS_isDir(JSContext *cx, JS::CallArgs &args) -{ - File *file = this->getFile(); - - if (!file->isOpen()) { - JS_ReportError(cx, "File has not been opened"); - return false; - } - - args.rval().setBoolean(file->isDir()); - - return true; -} - -bool JSFile::JS_rm(JSContext *cx, JS::CallArgs &args) -{ - this->getFile()->rm(); - - return true; -} - -bool JSFile::JS_rmrf(JSContext *cx, JS::CallArgs &args) -{ - this->getFile()->rmrf(); - - return true; -} - bool JSFile::JS_listFiles(JSContext *cx, JS::CallArgs &args) { File *file = this->getFile(); @@ -811,11 +783,8 @@ JSFunctionSpec *JSFile::ListMethods() CLASSMAPPER_FN(JSFile, write, 2), CLASSMAPPER_FN(JSFile, writeSync, 1), - CLASSMAPPER_FN(JSFile, isDir, 0), CLASSMAPPER_FN(JSFile, listFiles, 1), - CLASSMAPPER_FN(JSFile, rm, 0), - CLASSMAPPER_FN(JSFile, rmrf, 0), JS_FS_END }; diff --git a/src/Binding/JSFile.h b/src/Binding/JSFile.h index bfabe004..ca5d9598 100644 --- a/src/Binding/JSFile.h +++ b/src/Binding/JSFile.h @@ -75,10 +75,7 @@ class JSFile : public ClassMapper, public Nidium::Core::Messages NIDIUM_DECL_JSCALL(closeSync); NIDIUM_DECL_JSCALL(write); NIDIUM_DECL_JSCALL(writeSync); - NIDIUM_DECL_JSCALL(isDir); NIDIUM_DECL_JSCALL(listFiles); - NIDIUM_DECL_JSCALL(rm); - NIDIUM_DECL_JSCALL(rmrf); NIDIUM_DECL_JSCALL_STATIC(read); NIDIUM_DECL_JSCALL_STATIC(readSync); diff --git a/src/IO/File.cpp b/src/IO/File.cpp index 861673a2..5b3ada8e 100644 --- a/src/IO/File.cpp +++ b/src/IO/File.cpp @@ -91,49 +91,6 @@ void File::checkRead(bool async, void *arg) } } -int File::rm() -{ - int ret = unlink(m_Path); - - closeFd(); - - return ret; -} - -void File::rmrf() -{ - FTS *tree; - FTSENT *f; - - char *path[] = { m_Path, NULL }; - - tree = fts_open(path, FTS_COMFOLLOW | FTS_NOCHDIR, File_compare); - - if (!tree) { - ndm_log(NDM_LOG_ERROR, "File", "Failed to fts_open()"); - return; - } - while ((f = fts_read(tree))) { - switch (f->fts_info) { - case FTS_F: - case FTS_NS: - case FTS_SL: - case FTS_SLNONE: - unlink(f->fts_path); - break; - case FTS_DP: - rmdir(f->fts_path); - break; - default: - break; - } - } - - fts_close(tree); - - closeFd(); -} - File::~File() { Core::PthreadAutoLock tasksLock(&getManagedLock()); diff --git a/src/IO/File.h b/src/IO/File.h index 6bd37597..ac03857c 100644 --- a/src/IO/File.h +++ b/src/IO/File.h @@ -55,8 +55,6 @@ class File : public Nidium::Core::Managed, public Nidium::Core::Events void write(char *buf, size_t size, void *arg = NULL); void seek(size_t pos, void *arg = NULL); void listFiles(void *arg = NULL); - void rmrf(); - int rm(); /* Check whether a path points to an existing filename. diff --git a/src/IO/FileSystem.h b/src/IO/FileSystem.h new file mode 100644 index 00000000..0399e52d --- /dev/null +++ b/src/IO/FileSystem.h @@ -0,0 +1,55 @@ +/* + Copyright 2016 Nidium Inc. All rights reserved. + Use of this source code is governed by a MIT license + that can be found in the LICENSE file. +*/ +#ifndef io_file_h__ +#define io_file_h__ + +#include +#include +#include +#include +#include +#include +#include + +namespace Nidium { +namespace IO { +namespace FileSystem { + + static void mkdirp(const char *path) + { + + char tmp[PATH_MAX]; + char *p = NULL; + size_t len; + + snprintf(tmp, sizeof(tmp), "%s", path); + + len = strlen(tmp); + + if (tmp[len - 1] == '/') { + tmp[len - 1] = 0; + } + + for (p = tmp + 1; *p; p++) { + + if (*p == '/') { + *p = 0; + mkdir(tmp, S_IRWXU); + *p = '/'; + } + + } + + mkdir(tmp, S_IRWXU); + + } + + +} // namespace FileSystem +} // namespace IO +} // namespace Nidium + +#endif