< prev index next >

src/java.base/unix/native/libjava/UnixFileSystem_md.c

Print this page

128 
129 JNIEXPORT jint JNICALL
130 Java_java_io_UnixFileSystem_getBooleanAttributes0(JNIEnv *env, jobject this,
131                                                   jobject file)
132 {
133     jint rv = 0;
134 
135     WITH_FIELD_PLATFORM_STRING(env, file, ids.path, path) {
136         int mode;
137         if (statMode(path, &mode)) {
138             int fmt = mode & S_IFMT;
139             rv = (jint) (java_io_FileSystem_BA_EXISTS
140                   | ((fmt == S_IFREG) ? java_io_FileSystem_BA_REGULAR : 0)
141                   | ((fmt == S_IFDIR) ? java_io_FileSystem_BA_DIRECTORY : 0));
142         }
143     } END_PLATFORM_STRING(env, path);
144     return rv;
145 }
146 
147 JNIEXPORT jboolean JNICALL
148 Java_java_io_UnixFileSystem_checkAccess(JNIEnv *env, jobject this,
149                                         jobject file, jint a)
150 {
151     jboolean rv = JNI_FALSE;
152     int mode = 0;
153     switch (a) {
154     case java_io_FileSystem_ACCESS_READ:
155         mode = R_OK;
156         break;
157     case java_io_FileSystem_ACCESS_WRITE:
158         mode = W_OK;
159         break;
160     case java_io_FileSystem_ACCESS_EXECUTE:
161         mode = X_OK;
162         break;
163     default: assert(0);
164     }
165     WITH_FIELD_PLATFORM_STRING(env, file, ids.path, path) {
166         int res;
167         RESTARTABLE(access(path, mode), res);
168         if (res == 0) {
169             rv = JNI_TRUE;
170         }
171     } END_PLATFORM_STRING(env, path);
172     return rv;
173 }
174 
175 
176 JNIEXPORT jboolean JNICALL
177 Java_java_io_UnixFileSystem_setPermission(JNIEnv *env, jobject this,
178                                           jobject file,
179                                           jint access,
180                                           jboolean enable,
181                                           jboolean owneronly)
182 {
183     jboolean rv = JNI_FALSE;
184 
185     WITH_FIELD_PLATFORM_STRING(env, file, ids.path, path) {
186         int amode = 0;
187         int mode;
188         int res;
189         switch (access) {
190         case java_io_FileSystem_ACCESS_READ:
191             if (owneronly)
192                 amode = S_IRUSR;
193             else
194                 amode = S_IRUSR | S_IRGRP | S_IROTH;
195             break;
196         case java_io_FileSystem_ACCESS_WRITE:
197             if (owneronly)
198                 amode = S_IWUSR;
199             else
200                 amode = S_IWUSR | S_IWGRP | S_IWOTH;
201             break;

206                 amode = S_IXUSR | S_IXGRP | S_IXOTH;
207             break;
208         default:
209             assert(0);
210         }
211         if (statMode(path, &mode)) {
212             if (enable)
213                 mode |= amode;
214             else
215                 mode &= ~amode;
216             RESTARTABLE(chmod(path, mode), res);
217             if (res == 0) {
218                 rv = JNI_TRUE;
219             }
220         }
221     } END_PLATFORM_STRING(env, path);
222     return rv;
223 }
224 
225 JNIEXPORT jlong JNICALL
226 Java_java_io_UnixFileSystem_getLastModifiedTime(JNIEnv *env, jobject this,
227                                                 jobject file)
228 {
229     jlong rv = 0;
230 
231     WITH_FIELD_PLATFORM_STRING(env, file, ids.path, path) {
232         struct stat64 sb;
233         if (stat64(path, &sb) == 0) {
234 #if defined(_AIX)
235             rv =  (jlong)sb.st_mtime * 1000;
236             rv += (jlong)sb.st_mtime_n / 1000000;
237 #elif defined(MACOSX)
238             rv  = (jlong)sb.st_mtimespec.tv_sec * 1000;
239             rv += (jlong)sb.st_mtimespec.tv_nsec / 1000000;
240 #else
241             rv  = (jlong)sb.st_mtim.tv_sec * 1000;
242             rv += (jlong)sb.st_mtim.tv_nsec / 1000000;
243 #endif
244         }
245     } END_PLATFORM_STRING(env, path);
246     return rv;
247 }
248 
249 
250 JNIEXPORT jlong JNICALL
251 Java_java_io_UnixFileSystem_getLength(JNIEnv *env, jobject this,
252                                       jobject file)
253 {
254     jlong rv = 0;
255 
256     WITH_FIELD_PLATFORM_STRING(env, file, ids.path, path) {
257         struct stat64 sb;
258         if (stat64(path, &sb) == 0) {
259             rv = sb.st_size;
260         }
261     } END_PLATFORM_STRING(env, path);
262     return rv;
263 }
264 
265 
266 /* -- File operations -- */
267 
268 
269 JNIEXPORT jboolean JNICALL
270 Java_java_io_UnixFileSystem_createFileExclusively(JNIEnv *env, jclass cls,
271                                                   jstring pathname)
272 {
273     jboolean rv = JNI_FALSE;
274 
275     WITH_PLATFORM_STRING(env, pathname, path) {
276         FD fd;
277         /* The root directory always exists */
278         if (strcmp (path, "/")) {
279             fd = handleOpen(path, O_RDWR | O_CREAT | O_EXCL, 0666);
280             if (fd < 0) {
281                 if (errno != EEXIST)
282                     JNU_ThrowIOExceptionWithLastError(env, "Could not open file");
283             } else {
284                 if (close(fd) == -1)
285                     JNU_ThrowIOExceptionWithLastError(env, "Could not close file");
286                 rv = JNI_TRUE;
287             }
288         }
289     } END_PLATFORM_STRING(env, path);
290     return rv;
291 }
292 
293 
294 JNIEXPORT jboolean JNICALL
295 Java_java_io_UnixFileSystem_delete0(JNIEnv *env, jobject this,
296                                     jobject file)
297 {
298     jboolean rv = JNI_FALSE;
299 
300     WITH_FIELD_PLATFORM_STRING(env, file, ids.path, path) {
301         if (remove(path) == 0) {
302             rv = JNI_TRUE;
303         }
304     } END_PLATFORM_STRING(env, path);
305     return rv;
306 }
307 
308 
309 JNIEXPORT jobjectArray JNICALL
310 Java_java_io_UnixFileSystem_list(JNIEnv *env, jobject this,
311                                  jobject file)
312 {
313     DIR *dir = NULL;
314     struct dirent *ptr;
315     int len, maxlen;
316     jobjectArray rv, old;
317     jclass str_class;
318 
319     str_class = JNU_ClassString(env);
320     CHECK_NULL_RETURN(str_class, NULL);
321 
322     WITH_FIELD_PLATFORM_STRING(env, file, ids.path, path) {
323         dir = opendir(path);
324     } END_PLATFORM_STRING(env, path);
325     if (dir == NULL) return NULL;
326 
327     /* Allocate an initial String array */
328     len = 0;
329     maxlen = 16;
330     rv = (*env)->NewObjectArray(env, maxlen, str_class, NULL);
331     if (rv == NULL) goto error;

356     /* Copy the final results into an appropriately-sized array */
357     if (len < maxlen) {
358         old = rv;
359         rv = (*env)->NewObjectArray(env, len, str_class, NULL);
360         if (rv == NULL) {
361             return NULL;
362         }
363         if (JNU_CopyObjectArray(env, rv, old, len) < 0) {
364             return NULL;
365         }
366     }
367     return rv;
368 
369  error:
370     closedir(dir);
371     return NULL;
372 }
373 
374 
375 JNIEXPORT jboolean JNICALL
376 Java_java_io_UnixFileSystem_createDirectory(JNIEnv *env, jobject this,
377                                             jobject file)
378 {
379     jboolean rv = JNI_FALSE;
380 
381     WITH_FIELD_PLATFORM_STRING(env, file, ids.path, path) {
382         if (mkdir(path, 0777) == 0) {
383             rv = JNI_TRUE;
384         }
385     } END_PLATFORM_STRING(env, path);
386     return rv;
387 }
388 
389 
390 JNIEXPORT jboolean JNICALL
391 Java_java_io_UnixFileSystem_rename0(JNIEnv *env, jobject this,
392                                     jobject from, jobject to)
393 {
394     jboolean rv = JNI_FALSE;
395 
396     WITH_FIELD_PLATFORM_STRING(env, from, ids.path, fromPath) {
397         WITH_FIELD_PLATFORM_STRING(env, to, ids.path, toPath) {
398             if (rename(fromPath, toPath) == 0) {
399                 rv = JNI_TRUE;
400             }
401         } END_PLATFORM_STRING(env, toPath);
402     } END_PLATFORM_STRING(env, fromPath);
403     return rv;
404 }
405 
406 JNIEXPORT jboolean JNICALL
407 Java_java_io_UnixFileSystem_setLastModifiedTime(JNIEnv *env, jobject this,
408                                                 jobject file, jlong time)
409 {
410     jboolean rv = JNI_FALSE;
411 
412     WITH_FIELD_PLATFORM_STRING(env, file, ids.path, path) {
413         struct stat64 sb;
414 
415         if (stat64(path, &sb) == 0) {
416             struct timeval tv[2];
417 
418             /* Preserve access time */
419 #if defined(_AIX)
420             tv[0].tv_sec = sb.st_atime;
421             tv[0].tv_usec = sb.st_atime_n / 1000;
422 #elif defined(MACOSX)
423             tv[0].tv_sec = sb.st_atimespec.tv_sec;
424             tv[0].tv_usec = sb.st_atimespec.tv_nsec / 1000;
425 #else
426             tv[0].tv_sec = sb.st_atim.tv_sec;
427             tv[0].tv_usec = sb.st_atim.tv_nsec / 1000;
428 #endif
429             /* Change last-modified time */
430             tv[1].tv_sec = time / 1000;
431             tv[1].tv_usec = (time % 1000) * 1000;
432 
433             if (utimes(path, tv) == 0)
434                 rv = JNI_TRUE;
435         }
436     } END_PLATFORM_STRING(env, path);
437 
438     return rv;
439 }
440 
441 
442 JNIEXPORT jboolean JNICALL
443 Java_java_io_UnixFileSystem_setReadOnly(JNIEnv *env, jobject this,
444                                         jobject file)
445 {
446     jboolean rv = JNI_FALSE;
447 
448     WITH_FIELD_PLATFORM_STRING(env, file, ids.path, path) {
449         int mode;
450         int res;
451         if (statMode(path, &mode)) {
452             RESTARTABLE(chmod(path, mode & ~(S_IWUSR | S_IWGRP | S_IWOTH)), res);
453             if (res == 0) {
454                 rv = JNI_TRUE;
455             }
456         }
457     } END_PLATFORM_STRING(env, path);
458     return rv;
459 }
460 
461 JNIEXPORT jlong JNICALL
462 Java_java_io_UnixFileSystem_getSpace(JNIEnv *env, jobject this,
463                                      jobject file, jint t)
464 {
465     jlong rv = 0L;
466 
467     WITH_FIELD_PLATFORM_STRING(env, file, ids.path, path) {
468 #ifdef MACOSX
469         struct statfs fsstat;
470 #else
471         struct statvfs64 fsstat;
472         int res;
473 #endif
474         memset(&fsstat, 0, sizeof(fsstat));
475 #ifdef MACOSX
476         if (statfs(path, &fsstat) == 0) {
477             switch(t) {
478                 case java_io_FileSystem_SPACE_TOTAL:
479                     rv = jlong_mul(long_to_jlong(fsstat.f_bsize),
480                                    long_to_jlong(fsstat.f_blocks));
481                     break;
482                 case java_io_FileSystem_SPACE_FREE:
483                     rv = jlong_mul(long_to_jlong(fsstat.f_bsize),

128 
129 JNIEXPORT jint JNICALL
130 Java_java_io_UnixFileSystem_getBooleanAttributes0(JNIEnv *env, jobject this,
131                                                   jobject file)
132 {
133     jint rv = 0;
134 
135     WITH_FIELD_PLATFORM_STRING(env, file, ids.path, path) {
136         int mode;
137         if (statMode(path, &mode)) {
138             int fmt = mode & S_IFMT;
139             rv = (jint) (java_io_FileSystem_BA_EXISTS
140                   | ((fmt == S_IFREG) ? java_io_FileSystem_BA_REGULAR : 0)
141                   | ((fmt == S_IFDIR) ? java_io_FileSystem_BA_DIRECTORY : 0));
142         }
143     } END_PLATFORM_STRING(env, path);
144     return rv;
145 }
146 
147 JNIEXPORT jboolean JNICALL
148 Java_java_io_UnixFileSystem_checkAccess0(JNIEnv *env, jobject this,
149                                          jobject file, jint a)
150 {
151     jboolean rv = JNI_FALSE;
152     int mode = 0;
153     switch (a) {
154     case java_io_FileSystem_ACCESS_READ:
155         mode = R_OK;
156         break;
157     case java_io_FileSystem_ACCESS_WRITE:
158         mode = W_OK;
159         break;
160     case java_io_FileSystem_ACCESS_EXECUTE:
161         mode = X_OK;
162         break;
163     default: assert(0);
164     }
165     WITH_FIELD_PLATFORM_STRING(env, file, ids.path, path) {
166         int res;
167         RESTARTABLE(access(path, mode), res);
168         if (res == 0) {
169             rv = JNI_TRUE;
170         }
171     } END_PLATFORM_STRING(env, path);
172     return rv;
173 }
174 
175 
176 JNIEXPORT jboolean JNICALL
177 Java_java_io_UnixFileSystem_setPermission0(JNIEnv *env, jobject this,
178                                            jobject file,
179                                            jint access,
180                                            jboolean enable,
181                                            jboolean owneronly)
182 {
183     jboolean rv = JNI_FALSE;
184 
185     WITH_FIELD_PLATFORM_STRING(env, file, ids.path, path) {
186         int amode = 0;
187         int mode;
188         int res;
189         switch (access) {
190         case java_io_FileSystem_ACCESS_READ:
191             if (owneronly)
192                 amode = S_IRUSR;
193             else
194                 amode = S_IRUSR | S_IRGRP | S_IROTH;
195             break;
196         case java_io_FileSystem_ACCESS_WRITE:
197             if (owneronly)
198                 amode = S_IWUSR;
199             else
200                 amode = S_IWUSR | S_IWGRP | S_IWOTH;
201             break;

206                 amode = S_IXUSR | S_IXGRP | S_IXOTH;
207             break;
208         default:
209             assert(0);
210         }
211         if (statMode(path, &mode)) {
212             if (enable)
213                 mode |= amode;
214             else
215                 mode &= ~amode;
216             RESTARTABLE(chmod(path, mode), res);
217             if (res == 0) {
218                 rv = JNI_TRUE;
219             }
220         }
221     } END_PLATFORM_STRING(env, path);
222     return rv;
223 }
224 
225 JNIEXPORT jlong JNICALL
226 Java_java_io_UnixFileSystem_getLastModifiedTime0(JNIEnv *env, jobject this,
227                                                  jobject file)
228 {
229     jlong rv = 0;
230 
231     WITH_FIELD_PLATFORM_STRING(env, file, ids.path, path) {
232         struct stat64 sb;
233         if (stat64(path, &sb) == 0) {
234 #if defined(_AIX)
235             rv =  (jlong)sb.st_mtime * 1000;
236             rv += (jlong)sb.st_mtime_n / 1000000;
237 #elif defined(MACOSX)
238             rv  = (jlong)sb.st_mtimespec.tv_sec * 1000;
239             rv += (jlong)sb.st_mtimespec.tv_nsec / 1000000;
240 #else
241             rv  = (jlong)sb.st_mtim.tv_sec * 1000;
242             rv += (jlong)sb.st_mtim.tv_nsec / 1000000;
243 #endif
244         }
245     } END_PLATFORM_STRING(env, path);
246     return rv;
247 }
248 
249 
250 JNIEXPORT jlong JNICALL
251 Java_java_io_UnixFileSystem_getLength0(JNIEnv *env, jobject this,
252                                        jobject file)
253 {
254     jlong rv = 0;
255 
256     WITH_FIELD_PLATFORM_STRING(env, file, ids.path, path) {
257         struct stat64 sb;
258         if (stat64(path, &sb) == 0) {
259             rv = sb.st_size;
260         }
261     } END_PLATFORM_STRING(env, path);
262     return rv;
263 }
264 
265 
266 /* -- File operations -- */
267 
268 
269 JNIEXPORT jboolean JNICALL
270 Java_java_io_UnixFileSystem_createFileExclusively0(JNIEnv *env, jclass cls,
271                                                    jstring pathname)
272 {
273     jboolean rv = JNI_FALSE;
274 
275     WITH_PLATFORM_STRING(env, pathname, path) {
276         FD fd;
277         /* The root directory always exists */
278         if (strcmp (path, "/")) {
279             fd = handleOpen(path, O_RDWR | O_CREAT | O_EXCL, 0666);
280             if (fd < 0) {
281                 if (errno != EEXIST)
282                     JNU_ThrowIOExceptionWithLastError(env, "Could not open file");
283             } else {
284                 if (close(fd) == -1)
285                     JNU_ThrowIOExceptionWithLastError(env, "Could not close file");
286                 rv = JNI_TRUE;
287             }
288         }
289     } END_PLATFORM_STRING(env, path);
290     return rv;
291 }
292 
293 
294 JNIEXPORT jboolean JNICALL
295 Java_java_io_UnixFileSystem_delete0(JNIEnv *env, jobject this,
296                                     jobject file)
297 {
298     jboolean rv = JNI_FALSE;
299 
300     WITH_FIELD_PLATFORM_STRING(env, file, ids.path, path) {
301         if (remove(path) == 0) {
302             rv = JNI_TRUE;
303         }
304     } END_PLATFORM_STRING(env, path);
305     return rv;
306 }
307 
308 
309 JNIEXPORT jobjectArray JNICALL
310 Java_java_io_UnixFileSystem_list0(JNIEnv *env, jobject this, jobject file)

311 {
312     DIR *dir = NULL;
313     struct dirent *ptr;
314     int len, maxlen;
315     jobjectArray rv, old;
316     jclass str_class;
317 
318     str_class = JNU_ClassString(env);
319     CHECK_NULL_RETURN(str_class, NULL);
320 
321     WITH_FIELD_PLATFORM_STRING(env, file, ids.path, path) {
322         dir = opendir(path);
323     } END_PLATFORM_STRING(env, path);
324     if (dir == NULL) return NULL;
325 
326     /* Allocate an initial String array */
327     len = 0;
328     maxlen = 16;
329     rv = (*env)->NewObjectArray(env, maxlen, str_class, NULL);
330     if (rv == NULL) goto error;

355     /* Copy the final results into an appropriately-sized array */
356     if (len < maxlen) {
357         old = rv;
358         rv = (*env)->NewObjectArray(env, len, str_class, NULL);
359         if (rv == NULL) {
360             return NULL;
361         }
362         if (JNU_CopyObjectArray(env, rv, old, len) < 0) {
363             return NULL;
364         }
365     }
366     return rv;
367 
368  error:
369     closedir(dir);
370     return NULL;
371 }
372 
373 
374 JNIEXPORT jboolean JNICALL
375 Java_java_io_UnixFileSystem_createDirectory0(JNIEnv *env, jobject this,
376                                              jobject file)
377 {
378     jboolean rv = JNI_FALSE;
379 
380     WITH_FIELD_PLATFORM_STRING(env, file, ids.path, path) {
381         if (mkdir(path, 0777) == 0) {
382             rv = JNI_TRUE;
383         }
384     } END_PLATFORM_STRING(env, path);
385     return rv;
386 }
387 
388 
389 JNIEXPORT jboolean JNICALL
390 Java_java_io_UnixFileSystem_rename0(JNIEnv *env, jobject this,
391                                     jobject from, jobject to)
392 {
393     jboolean rv = JNI_FALSE;
394 
395     WITH_FIELD_PLATFORM_STRING(env, from, ids.path, fromPath) {
396         WITH_FIELD_PLATFORM_STRING(env, to, ids.path, toPath) {
397             if (rename(fromPath, toPath) == 0) {
398                 rv = JNI_TRUE;
399             }
400         } END_PLATFORM_STRING(env, toPath);
401     } END_PLATFORM_STRING(env, fromPath);
402     return rv;
403 }
404 
405 JNIEXPORT jboolean JNICALL
406 Java_java_io_UnixFileSystem_setLastModifiedTime0(JNIEnv *env, jobject this,
407                                                  jobject file, jlong time)
408 {
409     jboolean rv = JNI_FALSE;
410 
411     WITH_FIELD_PLATFORM_STRING(env, file, ids.path, path) {
412         struct stat64 sb;
413 
414         if (stat64(path, &sb) == 0) {
415             struct timeval tv[2];
416 
417             /* Preserve access time */
418 #if defined(_AIX)
419             tv[0].tv_sec = sb.st_atime;
420             tv[0].tv_usec = sb.st_atime_n / 1000;
421 #elif defined(MACOSX)
422             tv[0].tv_sec = sb.st_atimespec.tv_sec;
423             tv[0].tv_usec = sb.st_atimespec.tv_nsec / 1000;
424 #else
425             tv[0].tv_sec = sb.st_atim.tv_sec;
426             tv[0].tv_usec = sb.st_atim.tv_nsec / 1000;
427 #endif
428             /* Change last-modified time */
429             tv[1].tv_sec = time / 1000;
430             tv[1].tv_usec = (time % 1000) * 1000;
431 
432             if (utimes(path, tv) == 0)
433                 rv = JNI_TRUE;
434         }
435     } END_PLATFORM_STRING(env, path);
436 
437     return rv;
438 }
439 
440 
441 JNIEXPORT jboolean JNICALL
442 Java_java_io_UnixFileSystem_setReadOnly0(JNIEnv *env, jobject this,
443                                          jobject file)
444 {
445     jboolean rv = JNI_FALSE;
446 
447     WITH_FIELD_PLATFORM_STRING(env, file, ids.path, path) {
448         int mode;
449         int res;
450         if (statMode(path, &mode)) {
451             RESTARTABLE(chmod(path, mode & ~(S_IWUSR | S_IWGRP | S_IWOTH)), res);
452             if (res == 0) {
453                 rv = JNI_TRUE;
454             }
455         }
456     } END_PLATFORM_STRING(env, path);
457     return rv;
458 }
459 
460 JNIEXPORT jlong JNICALL
461 Java_java_io_UnixFileSystem_getSpace0(JNIEnv *env, jobject this,
462                                       jobject file, jint t)
463 {
464     jlong rv = 0L;
465 
466     WITH_FIELD_PLATFORM_STRING(env, file, ids.path, path) {
467 #ifdef MACOSX
468         struct statfs fsstat;
469 #else
470         struct statvfs64 fsstat;
471         int res;
472 #endif
473         memset(&fsstat, 0, sizeof(fsstat));
474 #ifdef MACOSX
475         if (statfs(path, &fsstat) == 0) {
476             switch(t) {
477                 case java_io_FileSystem_SPACE_TOTAL:
478                     rv = jlong_mul(long_to_jlong(fsstat.f_bsize),
479                                    long_to_jlong(fsstat.f_blocks));
480                     break;
481                 case java_io_FileSystem_SPACE_FREE:
482                     rv = jlong_mul(long_to_jlong(fsstat.f_bsize),
< prev index next >