Fixed null pointer exception
[cacao.git] / nat / UnixFileSystem.c
1 /* Structure information for class: java/io/UnixFileSystem */
2
3 /*
4  * Class:     java/io/UnixFileSystem
5  * Method:    canonicalize
6  * Signature: (Ljava/lang/String;)Ljava/lang/String;
7  */
8 JNIEXPORT struct java_lang_String* JNICALL Java_java_io_UnixFileSystem_canonicalize (JNIEnv *env ,  struct java_io_UnixFileSystem* this , struct java_lang_String* par1) 
9 {
10     return par1;
11 }
12
13 JNIEXPORT s4 JNICALL Java_java_io_UnixFileSystem_isAbsolute (JNIEnv *env ,  struct java_io_UnixFileSystem* this , struct java_io_File* file)
14 {
15   java_lang_String* s = file->path;
16   java_chararray *a = s->value;
17
18   /* absolute filenames start with '/' */
19   if (a->data[s->offset]=='/') return 1;
20   return 0;
21 }
22
23
24 /*
25  * Class:     java/io/UnixFileSystem
26  * Method:    getBooleanAttributes0
27  * Signature: (Ljava/io/File;)I
28  */
29 JNIEXPORT s4 JNICALL Java_java_io_UnixFileSystem_getBooleanAttributes0 (JNIEnv *env ,  struct java_io_UnixFileSystem* this , struct java_io_File* file)
30 {
31         struct stat buffer;
32         char *path;
33         int err;
34         s4 attrib = 0;
35         
36         path = javastring_tochar( (java_objectheader*) (file->path));
37         err  = stat (path, &buffer);
38
39         if (err==0) {
40
41             attrib = 0x01;  /* file exists */
42
43             if (S_ISREG(buffer.st_mode)) attrib |= 0x02;
44             if (S_ISDIR(buffer.st_mode)) attrib |= 0x04;
45         }
46
47         
48         /* printf("getBooleanAttributes called for file: %s (%d)\n",path,attrib); */
49
50         return attrib;
51 }
52
53 /*
54  * Class:     java/io/UnixFileSystem
55  * Method:    checkAccess
56  * Signature: (Ljava/io/File;Z)Z
57  */
58 JNIEXPORT s4 JNICALL Java_java_io_UnixFileSystem_checkAccess (JNIEnv *env ,  struct java_io_UnixFileSystem* this , struct java_io_File* file, s4 write)
59 {
60         struct stat buffer;
61         char *path;
62         int err;
63         
64         /* get file-attributes */
65         path = javastring_tochar( (java_objectheader*) (file->path));
66         err  = stat (path, &buffer);
67
68         if (err==0) {
69
70             /* check access rights */
71             if (((write)  && (buffer.st_mode & S_IWUSR)) ||
72                 ((!write) && (buffer.st_mode & S_IRUSR))    )
73               return 1;         
74         }
75
76         /* no access rights */
77         return 0;
78 }
79
80 /*
81  * Class:     java/io/UnixFileSystem
82  * Method:    getLastModifiedTime
83  * Signature: (Ljava/io/File;)J
84  */
85 JNIEXPORT s8 JNICALL Java_java_io_UnixFileSystem_getLastModifiedTime (JNIEnv *env ,  struct java_io_UnixFileSystem* this , struct java_io_File* file)
86 {
87         struct stat buffer;
88         int err;
89         
90         /* check argument */
91         if (!file) {
92             log_text("Warning: invalid call of native function getLastModifiedTime");
93             return 0;
94         }
95
96         /* get file-attributes */
97         err = stat (javastring_tochar( (java_objectheader*) (file->path)),  &buffer);
98         if (err!=0) return builtin_i2l(0);
99         return builtin_lmul (builtin_i2l(buffer.st_mtime), builtin_i2l(1000) );
100 }
101
102 /*
103  * Class:     java/io/UnixFileSystem
104  * Method:    getLength
105  * Signature: (Ljava/io/File;)J
106  */
107 JNIEXPORT s8 JNICALL Java_java_io_UnixFileSystem_getLength (JNIEnv *env ,  struct java_io_UnixFileSystem* this , struct java_io_File* file)
108 {
109         struct stat buffer;
110         int err;
111         
112         if (!file) {
113             log_text("Warning: invalid call of native function getLength");
114             return 0;
115         }
116
117         /* get file-attributes */
118         err = stat (javastring_tochar( (java_objectheader*) (file->path)),  &buffer);
119         if (err!=0) return builtin_i2l(0);
120         return builtin_i2l(buffer.st_size);
121 }
122
123 /*
124  * Class:     java/io/UnixFileSystem
125  * Method:    createFileExclusively
126  * Signature: (Ljava/lang/String;)Z
127  */
128 JNIEXPORT s4 JNICALL Java_java_io_UnixFileSystem_createFileExclusively (JNIEnv *env ,  struct java_io_UnixFileSystem* this , struct java_lang_String* name)
129 {
130         s4 fd;
131         char *fname = javastring_tochar ((java_objectheader*)name);
132
133         if (!fname) return 0;
134         
135         /* create new file */
136         fd = creat (fname, 0666);
137         if (fd<0) return 0;
138         
139         threadedFileDescriptor(fd);
140         close (fd);
141
142         return 1;
143 }
144
145 /*
146  * Class:     java/io/UnixFileSystem
147  * Method:    delete
148  * Signature: (Ljava/io/File;)Z
149  */
150 JNIEXPORT s4 JNICALL Java_java_io_UnixFileSystem_delete (JNIEnv *env ,  struct java_io_UnixFileSystem* this , struct java_io_File* file)
151 {
152         int err;
153         /* delete the file */
154         err = remove (javastring_tochar ( (java_objectheader*) (file->path) ) );
155         if (err==0) return 1;
156         
157         /* not successful */
158         return 0; 
159 }
160
161 /*
162  * Class:     java/io/UnixFileSystem
163  * Method:    deleteOnExit
164  * Signature: (Ljava/io/File;)Z
165  */
166 JNIEXPORT s4 JNICALL Java_java_io_UnixFileSystem_deleteOnExit (JNIEnv *env ,  struct java_io_UnixFileSystem* this , struct java_io_File* file)
167 {
168     log_text("Java_java_io_UnixFileSystem_deleteOnExit called");
169
170     return 1;
171 }
172
173 /*
174  * Class:     java/io/UnixFileSystem
175  * Method:    list
176  * Signature: (Ljava/io/File;)[Ljava/lang/String;
177  */
178 JNIEXPORT java_objectarray* JNICALL Java_java_io_UnixFileSystem_list (JNIEnv *env ,  struct java_io_UnixFileSystem* this , struct java_io_File* file) 
179 {
180         char *name;
181         DIR *d;
182         int i,len, namlen;
183         java_objectarray *a;
184         struct dirent *ent;
185         struct java_lang_String *n;
186         char entbuffer[257];
187         
188         name = javastring_tochar ( (java_objectheader*) (file->path) );
189         d = opendir(name);
190         if (!d) return NULL;
191         
192         len=0;
193         while (readdir(d) != NULL) len++;
194         rewinddir (d);
195         
196         a = builtin_anewarray (len, class_java_lang_String);
197         if (!a) {
198                 closedir(d);
199                 return NULL;
200                 }
201                 
202         for (i=0; i<len; i++) {
203                 if ( (ent = readdir(d)) != NULL) {
204                         namlen = strlen(ent->d_name);
205                         memcpy (entbuffer, ent->d_name, namlen);
206                         entbuffer[namlen] = '\0';
207                         
208                         n = (struct java_lang_String*) 
209                                 javastring_new_char (entbuffer);
210                         
211                         a -> data[i] = (java_objectheader*) n;
212                         }
213                 }
214
215
216         closedir(d);
217         return a;
218 }
219
220 /*
221  * Class:     java/io/UnixFileSystem
222  * Method:    createDirectory
223  * Signature: (Ljava/io/File;)Z
224  */
225 JNIEXPORT s4 JNICALL Java_java_io_UnixFileSystem_createDirectory (JNIEnv *env ,  struct java_io_UnixFileSystem* this , struct java_io_File* file)
226 {
227         char *name;
228         int err;
229
230         if (file) {
231
232             name = javastring_tochar ( (java_objectheader*) (file->path) );
233             err = mkdir (name, 0777);
234
235             if (err==0) return 1;
236         }
237
238         return 0;
239 }
240
241 /*
242  * Class:     java/io/UnixFileSystem
243  * Method:    rename
244  * Signature: (Ljava/io/File;Ljava/io/File;)Z
245  */
246 JNIEXPORT s4 JNICALL Java_java_io_UnixFileSystem_rename (JNIEnv *env ,  struct java_io_UnixFileSystem* this , struct java_io_File* file, struct java_io_File* new)
247 {
248
249 #define MAXJAVAPATHLEN 200
250
251         char newname[MAXJAVAPATHLEN];
252         char *n; 
253         int err;
254
255         if (file && new) {
256             n = javastring_tochar ( (java_objectheader*) (new->path) );
257             if (strlen(n)>=MAXJAVAPATHLEN) return 0;
258             strcpy (newname, n);
259             n = javastring_tochar ( (java_objectheader*) (file->path) );
260             err = rename (n, newname);
261             if (err==0) return 1;
262         }
263
264         return 0;
265 }
266
267 /*
268  * Class:     java/io/UnixFileSystem
269  * Method:    setLastModifiedTime
270  * Signature: (Ljava/io/File;J)Z
271  */
272 JNIEXPORT s4 JNICALL Java_java_io_UnixFileSystem_setLastModifiedTime (JNIEnv *env ,  struct java_io_UnixFileSystem* this , struct java_io_File* file, s8 time)
273 {
274         struct utimbuf ub;
275         log_text("Java_java_io_FileSystemImpl_setLastModifiedTime called");
276
277         ub.actime = time/1000;
278         ub.modtime = time/1000;
279         if (!utime(javastring_tochar( (java_objectheader*) (file->path)), &ub))
280             return 1;
281         else
282             return 0;
283 }
284
285 /*
286  * Class:     java/io/UnixFileSystem
287  * Method:    setReadOnly
288  * Signature: (Ljava/io/File;)Z
289  */
290 JNIEXPORT s4 JNICALL Java_java_io_UnixFileSystem_setReadOnly (JNIEnv *env ,  struct java_io_UnixFileSystem* this , struct java_io_File* file)
291 {
292         struct stat buffer;
293         char *path;
294         int err;
295         s4 attrib = 0;
296         
297         log_text("Java_java_io_FileSystemImpl_setReadOnly called");
298
299         path = javastring_tochar( (java_objectheader*) (file->path));
300         /* get file-attributes */
301         err  = stat (path, &buffer);
302
303         if (!err) {
304
305             buffer.st_mode &=  ~( 0x00002 |      /* write by others */
306                                   0x00200 );     /* write by owner  */
307
308             /* set file-attributes */
309             chmod(path,buffer.st_mode);
310
311             if (!err) return 1;
312         }
313
314         return 0;
315 }
316
317 /*
318  * Class:     java/io/UnixFileSystem
319  * Method:    initIDs
320  * Signature: ()V
321  */
322 JNIEXPORT void JNICALL Java_java_io_UnixFileSystem_initIDs (JNIEnv *env )
323 {
324     /* empty */
325 }
326
327