Added old (obsolete ?) stuff from svn
[gpm-metux.git] / espresso9_jni.c
blobbd2abf296102900afb609e01bd2b2a5d76c09461
2 #include <stdio.h>
4 #include <mvfs/mvfs.h>
5 #include <mvfs/autoconnect_ops.h>
7 #include "metux_espresso9_NativeFS.h"
9 #define MAX_FILES 64
11 static MVFS_FILESYSTEM* fs = NULL;
12 static MVFS_FILE* files[MAX_FILES];
14 static void init_filesystems()
16 if (fs != NULL)
17 return;
19 memset(files, 0, sizeof(files));
20 fs = mvfs_autoconnectfs_create();
23 static int addfile(MVFS_FILE* f)
25 if (f==NULL)
26 return -1;
28 int x;
29 for (x=0; x<MAX_FILES; x++)
31 if (files[x] == NULL)
33 files[x] = f;
34 return x;
38 fprintf(stderr,"metux.espresso9.NativeFilesystem: addfile() no more slots. bailing out\n");
39 return -1;
42 static MVFS_FILE* getfile(int fd)
44 if (fd<MAX_FILES)
46 if (!files[fd])
48 fprintf(stderr,"metux.espresso9.NativeFS: getfile() fd not open %d\n", fd);
49 return NULL;
51 return files[fd];
53 fprintf(stderr,"metux.espresso9.NativeFS: getfile() fd %d out of range\n", fd);
54 return NULL;
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);
61 if (str == NULL)
62 return -1; /* OOM */
64 init_filesystems();
66 int m;
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;
72 default: m = mode;
75 if (mode & metux_espresso9_NativeFS_omode_Create)
76 m |= O_CREAT;
78 MVFS_FILE* f = mvfs_fs_openfile(fs, str, m);
79 if (f==NULL)
81 fprintf(stderr,"metux.espresso9.NativeFS::open() could not open file\n");
82 return -1;
85 (*env)->ReleaseStringUTFChars(env, name, str);
86 return addfile(f);
89 JNIEXPORT jbyteArray JNICALL Java_metux_espresso9_NativeFS_n_1read
90 (JNIEnv * env, jclass cls, jlong fd, jlong size)
92 init_filesystems();
94 // we should throw an exception ;-o
95 MVFS_FILE* f = getfile(fd);
96 if (f==NULL)
98 fprintf(stderr,"metux.espresso9.NativeFS::read(fd,pos,size): file not open: %ld\n", fd);
99 return NULL;
101 if (size<1)
103 fprintf(stderr,"metux.espresso9.NativeFS::read(fd,pos,size). size out of range %ld\n", size);
104 return NULL;
107 char* buffer = alloca(size);
108 long readsize = mvfs_file_read(f, buffer, size);
110 if (readsize==0)
111 return NULL;
113 if (readsize<0)
115 fprintf(stderr,"metux.espresso.NativeFS::read(fd,pos,size) error %s\n", strerror(f->errcode));
116 // should throw an exception
117 return NULL;
120 jbyteArray ret = (*env)->NewByteArray(env, readsize);
121 (*env)->SetByteArrayRegion(env, ret, 0, readsize, (jbyte *)buffer);
123 return ret;
126 JNIEXPORT jlong JNICALL Java_metux_espresso9_NativeFS_n_1write
127 (JNIEnv *env, jclass cls, jlong fd, jbyteArray data)
129 init_filesystems();
131 // we should throw an exception ;-o
132 MVFS_FILE* f = getfile(fd);
133 if (f==NULL)
135 fprintf(stderr,"metux.espresso9.NativeFS::read(fd,data[]): file not open: %ld\n", fd);
136 return -1;
139 if (data==NULL)
140 return 0;
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)
152 init_filesystems();
154 MVFS_FILE* f = getfile(fd);
155 if (f==NULL)
156 return -1;
158 mvfs_file_close(f);
159 files[fd] = NULL;
161 return 0;
164 static jobject alloc_stat(JNIEnv* env, MVFS_STAT* st)
166 jclass cls = (*env)->FindClass(env, "metux.espresso9.Stat");
167 if (cls==NULL)
169 fprintf(stderr, "alloc_stat() class lookup failed\n");
170 return NULL;
173 jobject stat = (*env)->AllocObject(env, cls);
174 if (stat == NULL)
176 fprintf(stderr,"alloc_stat() object creation failed\n");
177 return NULL;
180 return stat;
183 static void set_obj_string(JNIEnv* env, jclass cls, jobject obj, const char* name, const char* value)
185 if (value==NULL)
187 fprintf(stderr,"set_obj_string() NULL value\n");
188 return;
190 jfieldID field = (*env)->GetFieldID(env, cls, name, "Ljava/lang/String;");
191 if (field == NULL)
193 fprintf(stderr,"cannot get field ID for \"%s\"\n", name);
194 return;
196 jobject of = (*env)->NewStringUTF(env,value);
197 if (of == NULL)
199 fprintf(stderr,"cannot create java string for \"%s\" -> \"%s\"\n", name, value);
200 return;
202 (*env)->SetObjectField(env, obj, field, of);
205 static jobject stat_to_java(JNIEnv* env, MVFS_STAT* st, jobject stobj)
207 if (st == NULL)
208 return NULL;
210 jclass cls = (*env)->GetObjectClass(env, stobj);
211 if (cls == NULL)
213 fprintf(stderr,"stat_to_java() cannot get return object class\n");
214 return NULL;
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);
219 return stobj;
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);
226 if (f == NULL)
228 fprintf(stderr,"fstat() illegal fd passed: %d\n", fd);
229 return NULL;
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);
240 if (f == NULL)
242 fprintf(stderr,"scan() illegal fd passed: %d\n", fd);
243 return NULL;
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);
254 if (f == NULL)
256 fprintf(stderr,"metux.espresso9.NativeFS::lookup() null file object passed\n");
257 return -1;
260 const jbyte* str = (*env)->GetStringUTFChars(env, name, NULL);
261 if (str == NULL)
262 return -1; /* OOM */
264 MVFS_FILE* newfile = mvfs_file_lookup(f, str);
265 if (newfile==NULL)
267 fprintf(stderr,"metux.espresso9.NativeFS::lookup() could not open file\n");
268 return -1;
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);
279 if (f == NULL)
281 fprintf(stderr,"metux.espresso9.NativeFS::reset() null file object passed\n");
282 return;
284 mvfs_file_reset(f);