aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
-rw-r--r--src/library.js52
-rw-r--r--tests/stat/output.txt28
-rw-r--r--tests/stat/src.c32
3 files changed, 75 insertions, 37 deletions
diff --git a/src/library.js b/src/library.js
index edcfb425..748fda43 100644
--- a/src/library.js
+++ b/src/library.js
@@ -241,7 +241,6 @@ LibraryManager.library = {
// input: Takes no parameters, returns a byte value or null if no data is
// currently available.
// output: Takes a byte value; doesn't return anything.
- // TODO: Decide how to handle flushing in a consistent manner.
createDevice: function(parent, name, input, output) {
if (!(input || output)) {
throw new Error('A device must have at least one callback defined.');
@@ -278,7 +277,6 @@ LibraryManager.library = {
return success;
}
},
- // TODO: Replace dirname/basename usage with a core describePath.
// ==========================================================================
// dirent.h
@@ -596,7 +594,6 @@ LibraryManager.library = {
// http://pubs.opengroup.org/onlinepubs/7908799/xsh/lstat.html
return _stat(path, buf, true);
},
- // TODO: Test once open() is implemented using FS.
fstat__deps: ['$FS', '__setErrNo', '$ERRNO_CODES', 'stat'],
fstat: function(fildes, buf) {
// int fstat(int fildes, struct stat *buf);
@@ -605,15 +602,14 @@ LibraryManager.library = {
___setErrNo(ERRNO_CODES.EBADF);
return -1;
} else {
- var pathArray = intArrayFromString(FS.streams[filedes].path);
+ var pathArray = intArrayFromString(FS.streams[fildes].path);
var pathPtr = allocate(pathArray, 'i8', ALLOC_NORMAL);
var result = _stat(pathPtr, buf);
_free(pathPtr);
return result;
}
},
- mknod__deps: ['$FS', '__setErrNo', '$ERRNO_CODES',
- 'strlen', 'strcpy', 'dirname', 'basename'],
+ mknod__deps: ['$FS', '__setErrNo', '$ERRNO_CODES'],
mknod: function(path, mode, dev) {
// int mknod(const char *path, mode_t mode, dev_t dev);
// http://pubs.opengroup.org/onlinepubs/7908799/xsh/mknod.html
@@ -623,12 +619,10 @@ LibraryManager.library = {
return -1;
} else {
var properties = {contents: [], isFolder: Boolean(mode & 0x4000)}; // S_IFDIR.
- var buffer = _malloc(_strlen(path) + 1);
- var parent = Pointer_stringify(_dirname(_strcpy(buffer, path)));
- var name = Pointer_stringify(_basename(_strcpy(buffer, path)));
- _free(buffer);
+ path = FS.analyzePath(Pointer_stringify(path));
try {
- FS.createObject(parent, name, properties, mode & 0x100, mode & 0x80); // S_IRUSR, S_IWUSR.
+ FS.createObject(path.parentObject, path.name, properties,
+ mode & 0x100, mode & 0x80); // S_IRUSR, S_IWUSR.
return 0;
} catch (e) {
return -1;
@@ -663,7 +657,6 @@ LibraryManager.library = {
obj.timestamp = new Date();
return 0;
},
- // TODO: Test once open() is implemented using FS.
fchmod__deps: ['$FS', '__setErrNo', '$ERRNO_CODES', 'chmod'],
fchmod: function(fildes, mode) {
// int fchmod(int fildes, mode_t mode);
@@ -672,7 +665,7 @@ LibraryManager.library = {
___setErrNo(ERRNO_CODES.EBADF);
return -1;
} else {
- var pathArray = intArrayFromString(FS.streams[filedes].path);
+ var pathArray = intArrayFromString(FS.streams[fildes].path);
var pathPtr = allocate(pathArray, 'i8', ALLOC_NORMAL);
var result = _chmod(pathPtr, mode);
_free(pathPtr);
@@ -728,8 +721,7 @@ LibraryManager.library = {
// ==========================================================================
__flock_struct_layout: Runtime.generateStructInfo(null, '%struct.flock'),
- open__deps: ['$FS', '__setErrNo', '$ERRNO_CODES',
- 'strlen', 'strcpy', 'dirname', 'basename'],
+ open__deps: ['$FS', '__setErrNo', '$ERRNO_CODES'],
open: function(path, oflag, mode) {
// int open(const char *path, int oflag, ...);
// http://pubs.opengroup.org/onlinepubs/009695399/functions/open.html
@@ -746,27 +738,12 @@ LibraryManager.library = {
var isAppend = Boolean(oflag & 0x400); // O_APPEND.
// Verify path.
- var pathStr = Pointer_stringify(path);
- if (!pathStr) {
- ___setErrNo(ERRNO_CODES.ENOENT);
- return -1;
- }
- var absolutePath = FS.absolutePath(pathStr);
- if (absolutePath === null) {
- ___setErrNo(ERRNO_CODES.ENOENT);
- return -1;
- }
- var buffer = _malloc(_strlen(path) + 1);
- var parentPath = Pointer_stringify(_dirname(_strcpy(buffer, path)));
- var name = Pointer_stringify(_basename(_strcpy(buffer, path)));
- _free(buffer);
- var parent = FS.findObject(parentPath);
- if (parent === null) return -1;
- if (!parent.isFolder || !parent.read) {
- ___setErrNo(ERRNO_CODES.EACCES);
+ path = FS.analyzePath(Pointer_stringify(path));
+ if (!path.parentExists) {
+ ___setErrNo(path.error);
return -1;
}
- var target = parent.contents[name] || null;
+ var target = path.object || null;
// Verify the file exists, create if needed and allowed.
if (target) {
@@ -795,18 +772,19 @@ LibraryManager.library = {
___setErrNo(ERRNO_CODES.ENOENT);
return -1;
}
- if (!parent.write) {
+ if (!path.parentObject.write) {
___setErrNo(ERRNO_CODES.EACCES);
return -1;
}
- target = FS.createDataFile(parent, name, [], mode & 0x100, mode & 0x80); // S_IRUSR, S_IWUSR.
+ target = FS.createDataFile(path.parentObject, path.name, [],
+ mode & 0x100, mode & 0x80); // S_IRUSR, S_IWUSR.
}
// Actually create an open stream.
var id = FS.streams.length;
FS.streams[id] = {
isFolder: false,
- path: absolutePath,
+ path: path.path,
object: target,
position: 0,
isRead: isRead,
diff --git a/tests/stat/output.txt b/tests/stat/output.txt
index 5253a834..1e6ae74e 100644
--- a/tests/stat/output.txt
+++ b/tests/stat/output.txt
@@ -108,12 +108,40 @@ S_ISREG: 0
S_ISLNK: 1
S_ISSOCK: 0
+--fstat FILE--
+ret: 0
+errno: 0
+st_dev: 1
+st_ino: 3
+st_mode: 0100777
+st_nlink: 1
+st_rdev: 0
+st_size: 6
+st_atime: 1200000000
+st_mtime: 1200000000
+st_ctime: 1200000000
+st_blksize: 4096
+st_blocks: 1
+S_ISBLK: 0
+S_ISCHR: 0
+S_ISDIR: 0
+S_ISFIFO: 0
+S_ISREG: 1
+S_ISLNK: 0
+S_ISSOCK: 0
+
--chmod FILE--
ret: 0
errno: 0
st_mode: 0100222
st_mtime changed: 1
+--fchmod FILE--
+ret: 0
+errno: 0
+st_mode: 0100777
+st_mtime changed: 1
+
--chmod FOLDER--
ret: 0
errno: 0
diff --git a/tests/stat/src.c b/tests/stat/src.c
index 4dc78805..3b3b8421 100644
--- a/tests/stat/src.c
+++ b/tests/stat/src.c
@@ -1,6 +1,7 @@
#include <stdio.h>
#include <string.h>
#include <errno.h>
+#include <fcntl.h>
#include <sys/stat.h>
int main() {
@@ -121,6 +122,29 @@ int main() {
printf("S_ISSOCK: %d\n", S_ISSOCK(s.st_mode));
memset(&s, 0, sizeof s);
+ printf("\n--fstat FILE--\n");
+ printf("ret: %d\n", fstat(open("/test/file", O_RDONLY, 0777), &s));
+ printf("errno: %d\n", errno);
+ printf("st_dev: %llu\n", s.st_dev);
+ printf("st_ino: %lu\n", s.st_ino);
+ printf("st_mode: 0%o\n", s.st_mode);
+ printf("st_nlink: %d\n", s.st_nlink);
+ printf("st_rdev: %llu\n", s.st_rdev);
+ printf("st_size: %ld\n", s.st_size);
+ printf("st_atime: %ld\n", s.st_atime);
+ printf("st_mtime: %ld\n", s.st_mtime);
+ printf("st_ctime: %ld\n", s.st_ctime);
+ printf("st_blksize: %ld\n", s.st_blksize);
+ printf("st_blocks: %ld\n", s.st_blocks);
+ printf("S_ISBLK: %d\n", S_ISBLK(s.st_mode));
+ printf("S_ISCHR: %d\n", S_ISCHR(s.st_mode));
+ printf("S_ISDIR: %d\n", S_ISDIR(s.st_mode));
+ printf("S_ISFIFO: %d\n", S_ISFIFO(s.st_mode));
+ printf("S_ISREG: %d\n", S_ISREG(s.st_mode));
+ printf("S_ISLNK: %d\n", S_ISLNK(s.st_mode));
+ printf("S_ISSOCK: %d\n", S_ISSOCK(s.st_mode));
+ memset(&s, 0, sizeof s);
+
printf("\n--chmod FILE--\n");
printf("ret: %d\n", chmod("/test/file", 0200));
printf("errno: %d\n", errno);
@@ -129,6 +153,14 @@ int main() {
printf("st_mtime changed: %d\n", s.st_mtime != 1200000000l);
memset(&s, 0, sizeof s);
+ printf("\n--fchmod FILE--\n");
+ printf("ret: %d\n", fchmod(open("/test/file", O_WRONLY, 0777), 0777));
+ printf("errno: %d\n", errno);
+ stat("/test/file", &s);
+ printf("st_mode: 0%o\n", s.st_mode);
+ printf("st_mtime changed: %d\n", s.st_mtime != 1200000000l);
+ memset(&s, 0, sizeof s);
+
printf("\n--chmod FOLDER--\n");
printf("ret: %d\n", chmod("/test", 0400));
printf("errno: %d\n", errno);