Permalink
Browse files

vfs

  • Loading branch information...
1 parent 4025d7c commit cb721ac5035e8cf5149cf513fcd871e61e83ff11 @cloudwu committed May 10, 2011
Showing with 850 additions and 9 deletions.
  1. +4 −2 build_tools/geninit.c
  2. +5 −1 mk/build.mk
  3. +2 −2 mk/build_init.mk
  4. +1 −1 mk/build_o.mk
  5. +3 −1 mk/build_test.mk
  6. +1 −1 mk/defines.mk
  7. +4 −1 src/core/atom.h
  8. +1 −0 src/core/log.c
  9. +224 −0 src/core/nativefs.c
  10. +14 −0 src/core/nativefs.h
  11. +173 −0 src/core/path.c
  12. +13 −0 src/core/path.h
  13. +102 −0 src/core/utf8.c
  14. +11 −0 src/core/utf8.h
  15. +211 −0 src/core/vfs.c
  16. +27 −0 src/core/vfs.h
  17. +2 −0 src/test/test.h
  18. +27 −0 src/test/utf.c
  19. +25 −0 src/test/vfs.c
View
@@ -74,7 +74,7 @@ import(const char *name, struct importAll *all, struct importModule **head)
if (!((c >= 'a' && c <='z') || (c >='0' && c<='9')))
return;
}
- if (name[i] == '\0')
+ if (i==0 || name[i] == '\0')
return;
memcpy(module, name, i);
module[i] = '\0';
@@ -140,6 +140,9 @@ header(struct importModule *head, FILE *f)
fprintf(f, "int _%sInit(void);" CR, head->name);
head = head->next;
}
+ if (exportInit) {
+ fprintf(f, "int %sInit(void);" CR, exportName);
+ }
}
static void
@@ -190,7 +193,6 @@ output(const char * filename, struct importModule *head)
int
main(int argc, char *argv[])
{
- FILE *f;
char buf[1024];
int i;
struct importAll all;
View
@@ -7,7 +7,11 @@ LIBSRCS := \
src/core/atom.c \
src/core/map.c \
src/core/set.c \
- src/core/log.c
+ src/core/log.c \
+ src/core/utf8.c \
+ src/core/nativefs.c \
+ src/core/path.c \
+ src/core/vfs.c
include mk/build_o.mk
include mk/build_init.mk
View
@@ -4,14 +4,14 @@ GENINIT := $(BUILD_TOOLS)/geninit$(EXE)
$(GENINIT) : | $(BUILD_TOOLS)
$(GENINIT) : build_tools/geninit.c
- $(CC) -o $@ $<
+ $(CC) $(CFLAGS) -o $@ $<
LIB_INIT_O := $(LIB_O:.o=.init.o)
LIB_INIT_C := $(LIB_O:.o=.init.c)
LIB_NAMES := $(sort $(basename $(notdir $(LIB_O))))
$(LIB_INIT_O) : $(BUILD_TOP)/o/%.o : $(BUILD_TOP)/o/%.c
- $(CC) -c -o $@ $<
+ $(CC) $(CFLAGS) -c -o $@ $<
$(LIB_INIT_C) : $(BUILD_TOP)/o/%.init.c : $(BUILD_TOP)/o/%.o $(GENINIT)
objdump -t $< | $(GENINIT) $@ $(LIB_NAMES)
View
@@ -7,7 +7,7 @@ define BUILD_temp
$$(TAR).o : | $(BUILD_TOP)/o
-include $$(TAR).d
$$(TAR).o : $(1)
- $(CC) -c -o $$@ $(CFLAGS) -MMD $$<
+ $(CC) $(CFLAGS) -c -o $$@ -MMD $$<
endef
$(foreach s,$(LIBSRCS),$(eval $(call BUILD_temp,$(s))))
View
@@ -5,6 +5,8 @@ test :
TESTSRCS := \
src/test/set.c \
src/test/map.c \
+ src/test/vfs.c \
+ src/test/utf.c \
src/test/log.c
INC := src/core
@@ -17,7 +19,7 @@ define TEST_temp
$$(TAR_O).o : | $(BUILD_TOP)/test
-include $$(TAR_O).d
$$(TAR_O).o : $(1)
- $(CC) -c -o $$@ $(CFLAGS) $(INC) -MMD $(1)
+ $(CC) $(CFLAGS) -c -o $$@ $(INC) -MMD $(1)
TAR_EXE := $(TEST_DIR)/$(notdir $(basename $(1)))$(EXE)
test : $$(TAR_EXE)
$$(TAR_EXE) : | $(TEST_DIR)
View
@@ -8,7 +8,7 @@ AR := ar rc
RM := rm
RMDIR := rm -r -f
MKDIR := mkdir -p
-DEBUG ?= -g -O0
+DEBUG := -g -O0
TOUCH := touch
EXE ?=.exe
View
@@ -3,7 +3,10 @@
#include <stddef.h>
-struct atom_t;
+struct atom_t {
+ char s[16];
+};
+
typedef struct atom_t * atom;
atom atomBuild(const void *data, size_t sz);
View
@@ -44,6 +44,7 @@ static void
logerr(const char *string)
{
fputs(string, stderr);
+ fputs("\n", stderr);
}
int
View
@@ -0,0 +1,224 @@
+#include "nativefs.h"
+#include "memory.h"
+#include <string.h>
+#include <stdio.h>
+
+struct nativefs {
+ size_t sz;
+ char root[1];
+};
+
+
+#ifdef __WIN32__
+
+#include <windows.h>
+#include "utf8.h"
+
+#define PATH_MAX 4096
+
+static char *
+realpath(const char * file_name, char * resolved_name)
+{
+ w_char name[PATH_MAX];
+ size_t s = utf8ToWideString(file_name, name,PATH_MAX);
+ size_t i;
+ for (i=0;i<s;i++) {
+ if (name[i] == '/') {
+ name[i] = '\\';
+ }
+ }
+
+ w_char tmp[PATH_MAX];
+
+ DWORD r = GetFullPathNameW(name,PATH_MAX,tmp,NULL);
+ if (r==0 || r==PATH_MAX) {
+ return NULL;
+ }
+ tmp[r]='\0';
+
+ s = utf8FromWideString(tmp, resolved_name, PATH_MAX);
+
+ for (i=0;i<s;i++) {
+ if (resolved_name[i]=='\\') {
+ resolved_name[i]='/';
+ }
+ }
+ if (s>0 && resolved_name[s-1]=='/') {
+ resolved_name[s-1] = '\0';
+ }
+
+ return resolved_name;
+}
+
+static atom
+nextfile(WIN32_FIND_DATAW *data, HANDLE handle)
+{
+ const w_char *name = data->cFileName;
+ while (name[0]=='.' && (name[1]=='\0' || (name[1]=='.' && name[2]=='\0'))) {
+ if (FindNextFileW(handle,data)==0) {
+ return NULL;
+ }
+ name = data->cFileName;
+ }
+ char str[PATH_MAX];
+ size_t sz = utf8FromWideString(name, str, PATH_MAX);
+ return atomBuild(str,sz);
+}
+
+static size_t
+_name(const char *root,const char *name, w_char *buf)
+{
+ size_t s = utf8ToWideString(root, buf, PATH_MAX);
+ size_t s2 = utf8ToWideString(name, buf+s, PATH_MAX-s);
+ return s+s2;
+}
+
+int
+nativefsList(void *_fs, const char *name, atom *buffer, int sz)
+{
+ struct nativefs *fs = _fs;
+ WIN32_FIND_DATAW data;
+ w_char buf[PATH_MAX];
+ size_t s = _name(fs->root,name, buf);
+ if (s + 2 >= PATH_MAX) {
+ return -1;
+ }
+ buf[s] = '\\';
+ buf[s+1] = '*';
+ buf[s+2] = '\0';
+
+ HANDLE handle = FindFirstFileW(buf,&data);
+ if (handle == INVALID_HANDLE_VALUE) {
+ return -1;
+ }
+
+ int n = 0;
+
+ for (;;) {
+ atom name = nextfile(&data, handle);
+ if (n<sz) {
+ buffer[n] = name;
+ }
+ ++n;
+ if (name == NULL)
+ break;
+ if (FindNextFileW(handle,&data)==0) {
+ break;
+ }
+ }
+ FindClose(handle);
+
+ return n;
+}
+
+size_t
+nativefsSize(void *_fs , const char *name)
+{
+ struct nativefs *fs = _fs;
+ w_char buf[PATH_MAX];
+ size_t s = _name(fs->root,name, buf);
+ if (s + 1 >= PATH_MAX) {
+ return 0;
+ }
+ HANDLE fd=CreateFileW(
+ buf,
+ GENERIC_READ,
+ FILE_SHARE_READ,
+ 0,
+ OPEN_EXISTING,
+ FILE_ATTRIBUTE_NORMAL,
+ 0);
+ if ( fd == INVALID_HANDLE_VALUE)
+ return 0;
+ LARGE_INTEGER size;
+ size.u.LowPart = GetFileSize(fd,&size.u.HighPart);
+ if (size.u.LowPart == INVALID_FILE_SIZE) {
+ if (GetLastError() != NO_ERROR) {
+ CloseHandle(fd);
+ return 0;
+ }
+ }
+
+
+ return (size_t)size.QuadPart;
+}
+
+int
+nativefsRead(void *_fs , const char *name, size_t pos, void *buffer, int sz)
+{
+ struct nativefs *fs = _fs;
+ w_char buf[PATH_MAX];
+ size_t s = _name(fs->root,name, buf);
+ if (s + 1 >= PATH_MAX) {
+ return -1;
+ }
+ HANDLE fd=CreateFileW(
+ buf,
+ GENERIC_READ,
+ FILE_SHARE_READ,
+ 0,
+ OPEN_EXISTING,
+ FILE_ATTRIBUTE_NORMAL,
+ 0);
+ if ( fd == INVALID_HANDLE_VALUE)
+ return 0;
+ LARGE_INTEGER move;
+ move.QuadPart = pos;
+ if (SetFilePointerEx(fd, move, NULL, FILE_BEGIN)) {
+ DWORD ret = 0;
+ ReadFile(fd, buffer, sz, &ret, 0) ;
+ CloseHandle(fd);
+ return (int)ret;
+ }
+ CloseHandle(fd);
+ return -1;
+}
+
+int
+nativefsCreate(void *_fs,const char *name, char mode)
+{
+ struct nativefs *fs = _fs;
+ w_char buf[PATH_MAX];
+ size_t s = _name(fs->root,name, buf);
+ if (s + 1 >= PATH_MAX) {
+ return 0;
+ }
+ switch (mode) {
+ case 'q': {
+ HANDLE fd=CreateFileW(
+ buf,
+ GENERIC_READ,
+ FILE_SHARE_READ,
+ 0,
+ OPEN_EXISTING,
+ FILE_ATTRIBUTE_NORMAL,
+ 0);
+ if ( fd == INVALID_HANDLE_VALUE)
+ return 0;
+ CloseHandle(fd);
+ }
+ return 1;
+ }
+ return 0;
+}
+
+#else
+
+#endif
+
+void*
+nativefsCreateFS(void *arg)
+{
+ if (arg == NULL) {
+ arg = ".";
+ }
+ char tmp[PATH_MAX];
+ realpath(arg,tmp);
+ size_t s = strlen(tmp);
+ struct nativefs *fs = memoryPermanent(sizeof(struct nativefs) + s);
+ fs->sz = s;
+ strcpy(fs->root,tmp);
+
+ return fs;
+}
+
View
@@ -0,0 +1,14 @@
+#ifndef windsoul_native_fs_h
+#define windsoul_native_fs_h
+
+#include "atom.h"
+#include <stddef.h>
+
+size_t nativefsSize(void *fs , const char *name);
+int nativefsRead(void *fs , const char *name, size_t pos, void *buffer, int sz);
+int nativefsList(void *fs, const char *name, atom *buffer, int sz);
+int nativefsCreate(void *fs,const char *name, char mode);
+
+void* nativefsCreateFS(void *arg);
+
+#endif
Oops, something went wrong.

0 comments on commit cb721ac

Please sign in to comment.