5 #include <mvfs/autoconnect_ops.h>
7 #include "metux_espresso9_NativeFS.h"
11 static MVFS_FILESYSTEM
* fs
= NULL
;
12 static MVFS_FILE
* files
[MAX_FILES
];
14 static void init_filesystems()
19 memset(files
, 0, sizeof(files
));
20 fs
= mvfs_autoconnectfs_create();
23 static int addfile(MVFS_FILE
* f
)
29 for (x
=0; x
<MAX_FILES
; x
++)
38 fprintf(stderr
,"metux.espresso9.NativeFilesystem: addfile() no more slots. bailing out\n");
42 static MVFS_FILE
* getfile(int fd
)
48 fprintf(stderr
,"metux.espresso9.NativeFS: getfile() fd not open %d\n", fd
);
53 fprintf(stderr
,"metux.espresso9.NativeFS: getfile() fd %d out of range\n", fd
);
57 JNIEXPORT jlong JNICALL Java_metux_espresso9_NativeFS_n_1open
58 (JNIEnv
* env
, jclass cls
, jstring name
, jint mode
)
60 const jbyte
* str
= (*env
)->GetStringUTFChars(env
, name
, NULL
);
67 switch (mode
& metux_espresso9_NativeFS_omode_RWMASK
)
69 case metux_espresso9_NativeFS_omode_ReadOnly
: m
= O_RDONLY
; break;
70 case metux_espresso9_NativeFS_omode_WriteOnly
: m
= O_WRONLY
; break;
71 case metux_espresso9_NativeFS_omode_ReadWrite
: m
= O_RDWR
; break;
75 if (mode
& metux_espresso9_NativeFS_omode_Create
)
78 MVFS_FILE
* f
= mvfs_fs_openfile(fs
, str
, m
);
81 fprintf(stderr
,"metux.espresso9.NativeFS::open() could not open file\n");
85 (*env
)->ReleaseStringUTFChars(env
, name
, str
);
89 JNIEXPORT jbyteArray JNICALL Java_metux_espresso9_NativeFS_n_1read
90 (JNIEnv
* env
, jclass cls
, jlong fd
, jlong size
)
94 // we should throw an exception ;-o
95 MVFS_FILE
* f
= getfile(fd
);
98 fprintf(stderr
,"metux.espresso9.NativeFS::read(fd,pos,size): file not open: %ld\n", fd
);
103 fprintf(stderr
,"metux.espresso9.NativeFS::read(fd,pos,size). size out of range %ld\n", size
);
107 char* buffer
= alloca(size
);
108 long readsize
= mvfs_file_read(f
, buffer
, size
);
115 fprintf(stderr
,"metux.espresso.NativeFS::read(fd,pos,size) error %s\n", strerror(f
->errcode
));
116 // should throw an exception
120 jbyteArray ret
= (*env
)->NewByteArray(env
, readsize
);
121 (*env
)->SetByteArrayRegion(env
, ret
, 0, readsize
, (jbyte
*)buffer
);
126 JNIEXPORT jlong JNICALL Java_metux_espresso9_NativeFS_n_1write
127 (JNIEnv
*env
, jclass cls
, jlong fd
, jbyteArray data
)
131 // we should throw an exception ;-o
132 MVFS_FILE
* f
= getfile(fd
);
135 fprintf(stderr
,"metux.espresso9.NativeFS::read(fd,data[]): file not open: %ld\n", fd
);
142 long size
= (*env
)->GetArrayLength(env
, data
);
144 jbyte
* buffer
= alloca(size
);
145 (*env
)->GetByteArrayRegion(env
, data
, 0, size
, (jbyte
*)buffer
);
146 return mvfs_file_write(f
, buffer
, size
);
149 JNIEXPORT jlong JNICALL Java_metux_espresso9_NativeFS_n_1close
150 (JNIEnv
*env
, jclass cls
, jlong fd
)
154 MVFS_FILE
* f
= getfile(fd
);
164 static jobject
alloc_stat(JNIEnv
* env
, MVFS_STAT
* st
)
166 jclass cls
= (*env
)->FindClass(env
, "metux.espresso9.Stat");
169 fprintf(stderr
, "alloc_stat() class lookup failed\n");
173 jobject stat
= (*env
)->AllocObject(env
, cls
);
176 fprintf(stderr
,"alloc_stat() object creation failed\n");
183 static void set_obj_string(JNIEnv
* env
, jclass cls
, jobject obj
, const char* name
, const char* value
)
187 fprintf(stderr
,"set_obj_string() NULL value\n");
190 jfieldID field
= (*env
)->GetFieldID(env
, cls
, name
, "Ljava/lang/String;");
193 fprintf(stderr
,"cannot get field ID for \"%s\"\n", name
);
196 jobject of
= (*env
)->NewStringUTF(env
,value
);
199 fprintf(stderr
,"cannot create java string for \"%s\" -> \"%s\"\n", name
, value
);
202 (*env
)->SetObjectField(env
, obj
, field
, of
);
205 static jobject
stat_to_java(JNIEnv
* env
, MVFS_STAT
* st
, jobject stobj
)
210 jclass cls
= (*env
)->GetObjectClass(env
, stobj
);
213 fprintf(stderr
,"stat_to_java() cannot get return object class\n");
216 set_obj_string(env
, cls
, stobj
, "name", st
->name
);
217 set_obj_string(env
, cls
, stobj
, "uid", st
->uid
);
218 set_obj_string(env
, cls
, stobj
, "gid", st
->gid
);
222 JNIEXPORT jobject JNICALL Java_metux_espresso9_NativeFS_n_1fstat
223 (JNIEnv
*env
, jclass myclass
, jlong fd
, jobject jst
)
225 MVFS_FILE
* f
= getfile(fd
);
228 fprintf(stderr
,"fstat() illegal fd passed: %d\n", fd
);
232 MVFS_STAT
* ust
= mvfs_file_stat(f
);
233 return stat_to_java(env
, ust
, jst
);
236 JNIEXPORT jobject JNICALL Java_metux_espresso9_NativeFS_n_1scan
237 (JNIEnv
*env
, jclass myclass
, jlong fd
, jobject jst
)
239 MVFS_FILE
* f
= getfile(fd
);
242 fprintf(stderr
,"scan() illegal fd passed: %d\n", fd
);
246 MVFS_STAT
* ust
= mvfs_file_scan(f
);
247 return stat_to_java(env
, ust
, jst
);
250 JNIEXPORT jlong JNICALL Java_metux_espresso9_NativeFS_n_1lookup
251 (JNIEnv
*env
, jclass myclass
, jlong fd
, jstring name
)
253 MVFS_FILE
* f
= getfile(fd
);
256 fprintf(stderr
,"metux.espresso9.NativeFS::lookup() null file object passed\n");
260 const jbyte
* str
= (*env
)->GetStringUTFChars(env
, name
, NULL
);
264 MVFS_FILE
* newfile
= mvfs_file_lookup(f
, str
);
267 fprintf(stderr
,"metux.espresso9.NativeFS::lookup() could not open file\n");
270 fprintf(stderr
," -> lookup() oldfd=%ld name=\"%s\" newfd=%ld\n", f
, str
, newfile
);
271 (*env
)->ReleaseStringUTFChars(env
, name
, str
);
272 return addfile(newfile
);
275 JNIEXPORT
void JNICALL Java_metux_espresso9_NativeFS_n_1reset
276 (JNIEnv
*env
, jclass myclass
, jlong fd
)
278 MVFS_FILE
* f
= getfile(fd
);
281 fprintf(stderr
,"metux.espresso9.NativeFS::reset() null file object passed\n");