Skip to content
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

WIP: Feature/fs #77

Open
wants to merge 8 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
/NidiumTools
konstruct*.log
configure*c
bin
Expand Down
4 changes: 2 additions & 2 deletions src/Binding/JSCanvas.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}

Expand Down Expand Up @@ -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<Frontend::Context>(cx), true);

Expand Down
126 changes: 126 additions & 0 deletions src/Binding/JSFS.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -12,8 +12,11 @@
#include <sys/types.h>
#include <sys/stat.h>

#include "IO/FileSystem.h"

using Nidium::Core::SharedMessages;
using Nidium::Core::Task;
using Nidium::IO::FileSystem::mkdirp;

namespace Nidium {
namespace Binding {
Expand Down Expand Up @@ -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);
Expand All @@ -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
};
Expand Down
4 changes: 4 additions & 0 deletions src/Binding/JSFS.h
Original file line number Diff line number Diff line change
Expand Up @@ -81,7 +81,11 @@ class JSFS : public ClassMapper<JSFS>, 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
Expand Down
31 changes: 0 additions & 31 deletions src/Binding/JSFile.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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();
Expand Down Expand Up @@ -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
};

Expand Down
3 changes: 0 additions & 3 deletions src/Binding/JSFile.h
Original file line number Diff line number Diff line change
Expand Up @@ -75,10 +75,7 @@ class JSFile : public ClassMapper<JSFile>, 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);
Expand Down
43 changes: 0 additions & 43 deletions src/IO/File.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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());
Expand Down
2 changes: 0 additions & 2 deletions src/IO/File.h
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down
55 changes: 55 additions & 0 deletions src/IO/FileSystem.h
Original file line number Diff line number Diff line change
@@ -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 <limits.h>
#include <stdio.h>
#include <stdint.h>
#include <string.h>
#include <dirent.h>
#include <sys/stat.h>
#include <sys/types.h>

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