-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathjtux_file.c
executable file
·827 lines (714 loc) · 20.3 KB
/
jtux_file.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
/*
Copyright 2003 by Marc J. Rochkind. All rights reserved.
May be copied only for purposes and under conditions described
on the Web page www.basepath.com/aup/copyright.htm.
The Example Files are provided "as is," without any warranty;
without even the implied warranty of merchantability or fitness
for a particular purpose. The author and his publisher are not
responsible for any damages, direct or incidental, resulting
from the use or non-use of these Example Files.
The Example Files may contain defects, and some contain deliberate
coding mistakes that were included for educational reasons.
You are responsible for determining if and how the Example Files
are to be used.
*/
#include "defs.h"
#include <sys/statvfs.h>
#include <poll.h>
#include <sys/uio.h>
#include <utime.h>
#include "JtuxFile.h" // generated by javah
#include "JtuxFile$fd_set.h" // generated by javah
#include "jtux_util.h"
#include "JNI_macros.h"
static bool statbuf_to_java(JNIEnv *env, jobject buf, struct stat *sbuf)
{
jclass cls = (*env)->FindClass(env, "jtux/UFile$s_stat");
if (!field_ctoj_int(env, cls, "st_dev", buf, sbuf->st_dev))
return false;
if (!field_ctoj_int(env, cls, "st_ino", buf, sbuf->st_ino))
return false;
if (!field_ctoj_int(env, cls, "st_mode", buf, sbuf->st_mode))
return false;
if (!field_ctoj_int(env, cls, "st_nlink", buf, sbuf->st_nlink))
return false;
if (!field_ctoj_long(env, cls, "st_uid", buf, sbuf->st_uid))
return false;
if (!field_ctoj_long(env, cls, "st_gid", buf, sbuf->st_gid))
return false;
if (!field_ctoj_int(env, cls, "st_rdev", buf, sbuf->st_rdev))
return false;
if (!field_ctoj_long(env, cls, "st_size", buf, sbuf->st_size))
return false;
if (!field_ctoj_long(env, cls, "st_atime", buf, sbuf->st_atime))
return false;
if (!field_ctoj_long(env, cls, "st_mtime", buf, sbuf->st_mtime))
return false;
if (!field_ctoj_long(env, cls, "st_ctime", buf, sbuf->st_ctime))
return false;
if (!field_ctoj_int(env, cls, "st_blksize", buf, sbuf->st_blksize))
return false;
if (!field_ctoj_long(env, cls, "st_blocks", buf, sbuf->st_blocks))
return false;
return true;
}
static bool statvfsbuf_to_java(JNIEnv *env, jobject buf, struct statvfs *sbuf)
{
jclass cls = (*env)->FindClass(env, "jtux/UFile$s_statvfs");
if (!field_ctoj_long(env, cls, "f_bsize", buf, sbuf->f_bsize))
return false;
if (!field_ctoj_long(env, cls, "f_frsize", buf, sbuf->f_frsize))
return false;
if (!field_ctoj_long(env, cls, "f_blocks", buf, sbuf->f_blocks))
return false;
if (!field_ctoj_long(env, cls, "f_bfree", buf, sbuf->f_bfree))
return false;
if (!field_ctoj_long(env, cls, "f_bavail", buf, sbuf->f_bavail))
return false;
if (!field_ctoj_long(env, cls, "f_files", buf, sbuf->f_files))
return false;
if (!field_ctoj_long(env, cls, "f_ffree", buf, sbuf->f_ffree))
return false;
if (!field_ctoj_long(env, cls, "f_favail", buf, sbuf->f_favail))
return false;
if (!field_ctoj_long(env, cls, "f_fsid", buf, sbuf->f_fsid))
return false;
if (!field_ctoj_long(env, cls, "f_flag", buf, sbuf->f_flag))
return false;
if (!field_ctoj_long(env, cls, "f_namemax", buf, sbuf->f_namemax))
return false;
return true;
}
JNIEXPORT jint JNICALL Java_jtux_UFile_00024fd_1set_GetSize_1fd_1set(JNIEnv *env, jclass obj)
{
return sizeof(fd_set);
}
static jbyte *get_fd_set(JNIEnv *env, jobject obj, jbyteArray *ba)
{
jclass cls = (*env)->FindClass(env, "jtux/UFile$fd_set");
jfieldID fid;
if (cls == NULL || obj == NULL)
return NULL;
if ((fid = (*env)->GetFieldID(env, cls, "set", "[B")) == NULL)
return NULL;
if ((*ba = (*env)->GetObjectField(env, obj, fid)) == NULL)
return NULL;
return (*env)->GetByteArrayElements(env, *ba, NULL);
}
static void release_fd_set(JNIEnv *env, jbyteArray b, jbyte *p)
{
// Always copy back, even though for FD_ISSET we don't have to
if (b != NULL && p != NULL)
(*env)->ReleaseByteArrayElements(env, b, p, 0);
}
JNIEXPORT void JNICALL Java_jtux_UFile_access(JNIEnv *env, jclass obj,
jstring path, jint what)
{
JSTR_GET_DECL(path_c, path)
JSTR_NULLTEST(path_c)
JTHROW_neg1(access(path_c, what))
JSTR_REL(path_c, path)
}
JNIEXPORT void JNICALL Java_jtux_UFile_chmod(JNIEnv *env, jclass obj,
jstring path, jint mode)
{
JSTR_GET_DECL(path_c, path)
JSTR_NULLTEST(path_c)
JTHROW_neg1(chmod(path_c, mode))
JSTR_REL(path_c, path)
}
JNIEXPORT void JNICALL Java_jtux_UFile_chown(JNIEnv *env, jclass obj,
jstring path, jlong uid, jlong gid)
{
JSTR_GET_DECL(path_c, path)
JSTR_NULLTEST(path_c)
JTHROW_neg1(chown(path_c, uid, gid))
JSTR_REL(path_c, path)
}
JNIEXPORT void JNICALL Java_jtux_UFile_close(JNIEnv *env, jclass obj,
jint fd)
{
JTHROW_neg1(close(fd))
}
JNIEXPORT jint JNICALL Java_jtux_UFile_dup(JNIEnv *env, jclass obj,
jint fd)
{
int fd2;
JTHROW_neg1(fd2 = dup(fd))
return fd2;
}
JNIEXPORT jint JNICALL Java_jtux_UFile_dup2(JNIEnv *env, jclass obj, jint fd, jint fd2)
{
JTHROW_neg1(dup2(fd, fd2))
return fd2;
}
JNIEXPORT void JNICALL Java_jtux_UFile_fchmod(JNIEnv *env, jclass obj,
jint fd, jint mode)
{
JTHROW_neg1(fchmod(fd, mode))
}
JNIEXPORT void JNICALL Java_jtux_UFile_fchown(JNIEnv *env, jclass obj,
jint fd, jlong uid, jlong gid)
{
JTHROW_neg1(fchown(fd, uid, gid))
}
JNIEXPORT jint JNICALL Java_jtux_UFile_fcntl(JNIEnv *env, jclass obj,
jint fd, jint op, jint arg)
{
int r;
JTHROW_neg1(r = fcntl(fd, op, arg))
return r;
}
JNIEXPORT void JNICALL Java_jtux_UFile_FD_1CLR(JNIEnv *env, jclass obj,
jint fd, jobject set)
{
jbyteArray ba;
jbyte *b = get_fd_set(env, set, &ba);
if (b != NULL) {
FD_CLR(fd, (fd_set *)b);
release_fd_set(env, ba, b);
}
}
JNIEXPORT jboolean JNICALL Java_jtux_UFile_FD_1ISSET(JNIEnv *env, jclass obj,
jint fd, jobject set)
{
int r;
jbyteArray ba;
jbyte *b = get_fd_set(env, set, &ba);
if (b != NULL) {
r = FD_ISSET(fd, (fd_set *)b);
release_fd_set(env, ba, b);
return r != 0;
}
return false;
}
JNIEXPORT void JNICALL Java_jtux_UFile_FD_1SET(JNIEnv *env, jclass obj,
jint fd, jobject set)
{
jbyteArray ba;
jbyte *b = get_fd_set(env, set, &ba);
if (b != NULL) {
FD_SET(fd, (fd_set *)b);
release_fd_set(env, ba, b);
}
}
JNIEXPORT void JNICALL Java_jtux_UFile_FD_1ZERO(JNIEnv *env, jclass obj,
jobject set)
{
jbyteArray ba;
jbyte *b = get_fd_set(env, set, &ba);
if (b != NULL) {
FD_ZERO((fd_set *)b);
release_fd_set(env, ba, b);
}
}
JNIEXPORT void JNICALL Java_jtux_UFile_fdatasync(JNIEnv *env, jclass obj,
jint fd)
{
JTHROW_neg1(fdatasync(fd))
}
JNIEXPORT void JNICALL Java_jtux_UFile_fstat(JNIEnv *env, jclass obj,
jint fd, jobject buf)
{
struct stat sbuf;
int r;
JTHROW_neg1(r = fstat(fd, &sbuf))
if (r == -1)
return;
if (!statbuf_to_java(env, buf, &sbuf))
return;
}
JNIEXPORT void JNICALL Java_jtux_UFile_fstatvfs(JNIEnv *env, jclass obj,
jint fd, jobject buf)
{
struct statvfs sbuf;
int r;
JTHROW_neg1(r = fstatvfs(fd, &sbuf))
if (r == -1)
return;
if (!statvfsbuf_to_java(env, buf, &sbuf))
return;
}
JNIEXPORT void JNICALL Java_jtux_UFile_fsync(JNIEnv *env, jclass obj,
jint fd)
{
JTHROW_neg1(fsync(fd))
}
JNIEXPORT void JNICALL Java_jtux_UFile_ftruncate(JNIEnv *env, jclass obj,
jint fd, jlong length)
{
JTHROW_neg1(ftruncate(fd, length))
}
JNIEXPORT void JNICALL Java_jtux_UFile_lchown(JNIEnv *env, jclass obj,
jstring path, jlong uid, jlong gid)
{
JSTR_GET_DECL(path_c, path)
JSTR_NULLTEST(path_c)
JTHROW_neg1(lchown(path_c, uid, gid))
JSTR_REL(path_c, path)
}
JNIEXPORT void JNICALL Java_jtux_UFile_link(JNIEnv *env, jclass obj,
jstring oldpath, jstring newpath)
{
JSTR_GET_DECL(oldpath_c, oldpath)
JSTR_GET_DECL(newpath_c, newpath)
JSTR_NULLTEST(oldpath_c)
JSTR_NULLTEST(newpath_c)
JTHROW_neg1(link(oldpath_c, newpath_c))
JSTR_REL(oldpath_c, oldpath)
JSTR_REL(newpath_c, newpath)
}
JNIEXPORT void JNICALL Java_jtux_UFile_lockf(JNIEnv *env, jclass obj,
jint fd, jint op, jlong len)
{
JTHROW_neg1(lockf(fd, op, len))
}
JNIEXPORT jlong JNICALL Java_jtux_UFile_lseek(JNIEnv *env, jclass obj,
jint fd, jlong pos, jint whence)
{
off_t off;
JTHROW_neg1(off = lseek(fd, pos, whence))
return off;
}
JNIEXPORT void JNICALL Java_jtux_UFile_lstat(JNIEnv *env, jclass obj,
jstring path, jobject buf)
{
JSTR_GET_DECL(path_c, path)
struct stat sbuf;
int r;
JSTR_NULLTEST(path_c)
JTHROW_neg1(r = lstat(path_c, &sbuf))
JSTR_REL(path_c, path)
if (r == -1)
return;
if (!statbuf_to_java(env, buf, &sbuf))
return;
}
JNIEXPORT void JNICALL Java_jtux_UFile_mkfifo(JNIEnv *env, jclass obj,
jstring path, jint perms)
{
JSTR_GET_DECL(path_c, path)
JSTR_NULLTEST(path_c)
JTHROW_neg1(mkfifo(path_c, perms))
JSTR_REL(path_c, path)
}
JNIEXPORT void JNICALL Java_jtux_UFile_mknod(JNIEnv *env, jclass obj,
jstring path, jint mode, jint dev)
{
JSTR_GET_DECL(path_c, path)
JSTR_NULLTEST(path_c)
JTHROW_neg1(mknod(path_c, mode, dev))
JSTR_REL(path_c, path)
}
JNIEXPORT jint JNICALL Java_jtux_UFile_mkstemp(JNIEnv *env, jclass obj,
jobject template)
{
jstring obj_string;
const char *t = string_buffer_get(env, template, &obj_string);
char *templ;
int fd = -1;
if (t != NULL) {
JTHROW_null(templ = malloc(strlen(t) + 1))
if (templ != NULL) {
strcpy(templ, t);
JTHROW_neg1(fd = mkstemp(templ))
(void)string_buffer_set(env, template, templ);
free(templ);
}
string_buffer_release(env, obj_string, t);
}
return fd;
}
JNIEXPORT jint JNICALL Java_jtux_UFile_open(JNIEnv *env, jclass obj,
jstring path, jint mode, jint perms)
{
int fd;
JSTR_GET_DECL(path_c, path)
JSTR_NULLTEST_V(path_c, -1)
JTHROW_neg1(fd = open(path_c, mode, perms))
JSTR_REL(path_c, path)
return fd;
}
JNIEXPORT void JNICALL Java_jtux_UFile_pipe(JNIEnv *env, jclass obj, jintArray pfd)
{
int fd[2];
jint *a;
JTHROW_neg1(pipe(fd))
a = (*env)->GetIntArrayElements(env, pfd, NULL);
if (a == NULL)
return;
a[0] = fd[0];
a[1] = fd[1];
(*env)->ReleaseIntArrayElements(env, pfd, a, 0);
}
JNIEXPORT jint JNICALL Java_jtux_UFile_poll(JNIEnv *env, jclass obj,
jobjectArray fdinfo, jint nfds, jint timeout)
{
struct pollfd *fi;
int i, r;
jclass cls = (*env)->FindClass(env, "jtux/UFile$s_pollfd");
JTHROW_null(fi = malloc(nfds * sizeof(struct pollfd)))
if (fi == NULL)
return -1;
for (i = 0; i < nfds; i++) {
jobject fi_obj = (*env)->GetObjectArrayElement(env, fdinfo, i);
if (fi_obj == NULL) {
free(fi);
return -1;
}
if (!field_jtoc_int(env, cls, "fd", fi_obj, &fi[i].fd))
return -1;
if (!field_jtoc_short(env, cls, "events", fi_obj, &fi[i].events))
return -1;
if (!field_jtoc_short(env, cls, "revents", fi_obj, &fi[i].revents))
return -1;
}
JTHROW_neg1(r = poll(fi, nfds, timeout))
if (r == -1) {
free(fi);
return -1;
}
for (i = 0; i < nfds; i++) {
jobject fi_obj = (*env)->GetObjectArrayElement(env, fdinfo, i);
if (fi_obj == NULL) {
free(fi);
return -1;
}
if (!field_ctoj_int(env, cls, "fd", fi_obj, fi[i].fd))
return -1;
if (!field_ctoj_short(env, cls, "events", fi_obj, fi[i].events))
return -1;
if (!field_ctoj_short(env, cls, "revents", fi_obj, fi[i].revents))
return -1;
}
free(fi);
return r;
}
JNIEXPORT jint JNICALL Java_jtux_UFile_pread(JNIEnv *env, jclass obj,
jint fd, jbyteArray buf, jint nbytes, jlong offset)
{
void *buf_c;
ssize_t r;
if ((buf_c = (*env)->GetByteArrayElements(env, buf, NULL)) == NULL)
return -1;
JTHROW_neg1(r = pread(fd, buf_c, nbytes, offset))
(*env)->ReleaseByteArrayElements(env, buf, buf_c, 0);
return r;
}
JNIEXPORT jint JNICALL Java_jtux_UFile_pselect(JNIEnv *env, jclass obj,
jint nfds, jobject readset, jobject writeset, jobject errorset, jobject timeout,
jobject sigmask)
{
#if _XOPEN_VERSION >= 600
// Following compiled but not tested
jbyteArray ba_read, ba_write, ba_error, ba_sigmask;
jbyte *b_read = get_fd_set(env, readset, &ba_read);
jbyte *b_write = get_fd_set(env, writeset, &ba_write);
jbyte *b_error = get_fd_set(env, errorset, &ba_error);
jbyte *b_sigmask = get_sigset(env, sigmask, &ba_sigmask);
jclass cls = (*env)->FindClass(env, "jtux/UProcess$s_timespec");
int r;
long n;
struct timespec tsbuf, *ts = &tsbuf;
if (timeout == NULL)
ts = NULL;
else {
if (!field_jtoc_long(env, cls, "tv_sec", timeout, &n))
return false;
ts->tv_sec = (time_t)n;
if (!field_jtoc_long(env, cls, "tv_nsec", timeout, &ts->tv_nsec))
return false;
printf("%ld %ld\n", (long)ts->tv_sec, (long)ts->tv_nsec);
}
JTHROW_neg1(r = pselect(nfds, (fd_set *)b_read, (fd_set *)b_write,
(fd_set *)b_error, ts, (sigset_t *)b_sigmask))
release_fd_set(env, ba_read, b_read);
release_fd_set(env, ba_write, b_write);
release_fd_set(env, ba_error, b_error);
release_sigset(env, ba_sigmask, b_sigmask);
return r;
#else
(void)setup_throw_errno(env, ENOSYS);
return -1;
#endif
}
JNIEXPORT jint JNICALL Java_jtux_UFile_pwrite(JNIEnv *env, jclass obj,
jint fd, jbyteArray buf, jint nbytes, jlong offset)
{
void *buf_c;
ssize_t r;
if ((buf_c = (*env)->GetByteArrayElements(env, buf, NULL)) == NULL)
return -1;
JTHROW_neg1(r = pwrite(fd, buf_c, nbytes, offset))
(*env)->ReleaseByteArrayElements(env, buf, buf_c, JNI_ABORT);
return r;
}
JNIEXPORT jint JNICALL Java_jtux_UFile_read(JNIEnv *env, jclass obj,
jint fd, jbyteArray buf, jint nbytes)
{
void *buf_c;
ssize_t r;
if ((buf_c = (*env)->GetByteArrayElements(env, buf, NULL)) == NULL)
return -1;
JTHROW_neg1(r = read(fd, buf_c, nbytes))
(*env)->ReleaseByteArrayElements(env, buf, buf_c, 0);
return r;
}
JNIEXPORT jint JNICALL Java_jtux_UFile_readlink(JNIEnv *env, jclass obj,
jstring path, jbyteArray buf, jint bufsize)
{
JSTR_GET_DECL(path_c, path)
void *buf_c;
ssize_t r;
JSTR_NULLTEST_V(path_c, -1)
if ((buf_c = (*env)->GetByteArrayElements(env, buf, NULL)) == NULL)
return -1;
JTHROW_neg1(r = readlink(path_c, buf_c, bufsize))
(*env)->ReleaseByteArrayElements(env, buf, buf_c, 0);
JSTR_REL(path_c, path)
return r;
}
JNIEXPORT jint JNICALL Java_jtux_UFile_readv(JNIEnv *env, jclass obj,
jint fd, jobjectArray iov, jint iovcnt)
{
struct iovec *v;
jbyteArray *v_bytearray;
ssize_t r;
if ((v = iovec_jtoc(env, iov, iovcnt, &v_bytearray)) == NULL)
return -1;
JTHROW_neg1(r = readv(fd, v, iovcnt))
iovec_jtoc_release(env, v, iovcnt, v_bytearray);
return r;
#if 0
struct iovec *v;
jbyteArray *v_bytearray;
int i;
ssize_t r;
jclass cls = (*env)->FindClass(env, "jtux/UFile$s_iovec");
JTHROW_null(v = malloc(iovcnt * sizeof(struct iovec)))
JTHROW_null(v_bytearray = malloc(iovcnt * sizeof(jbyteArray)))
if (v == NULL || v_bytearray == NULL)
return -1;
for (i = 0; i < iovcnt; i++) {
jobject v_obj = (*env)->GetObjectArrayElement(env, iov, i);
if (v_obj == NULL) {
free(v);
free(v_bytearray);
return -1;
}
if (!field_jtoc_bytearray(env, cls, "iov_base", v_obj, &v[i].iov_base,
&v_bytearray[i]))
return -1;
if (!field_jtoc_int(env, cls, "iov_len", v_obj, &v[i].iov_len))
return -1;
}
JTHROW_neg1(r = readv(fd, v, iovcnt))
for (i = 0; i < iovcnt; i++)
field_jtoc_bytearray_release(env, v_bytearray[i], v[i].iov_base);
free(v);
free(v_bytearray);
return r;
#endif
}
JNIEXPORT void JNICALL Java_jtux_UFile_rename(JNIEnv *env, jclass obj,
jstring oldpath, jstring newpath)
{
JSTR_GET_DECL(oldpath_c, oldpath)
JSTR_GET_DECL(newpath_c, newpath)
JSTR_NULLTEST(oldpath_c)
JSTR_NULLTEST(newpath_c)
JTHROW_neg1(rename(oldpath_c, newpath_c))
JSTR_REL(oldpath_c, oldpath)
JSTR_REL(newpath_c, newpath)
}
JNIEXPORT jboolean JNICALL Java_jtux_UFile_S_1ISBLK(JNIEnv *env, jclass obj,
jint m)
{
return S_ISBLK(m);
}
JNIEXPORT jboolean JNICALL Java_jtux_UFile_S_1ISCHR(JNIEnv *env, jclass obj,
jint m)
{
return S_ISCHR(m);
}
JNIEXPORT jboolean JNICALL Java_jtux_UFile_S_1ISDIR(JNIEnv *env, jclass obj,
jint m)
{
return S_ISDIR(m);
}
JNIEXPORT jboolean JNICALL Java_jtux_UFile_S_1ISFIFO(JNIEnv *env, jclass obj,
jint m)
{
return S_ISFIFO(m);
}
JNIEXPORT jboolean JNICALL Java_jtux_UFile_S_1ISLNK(JNIEnv *env, jclass obj,
jint m)
{
return S_ISLNK(m);
}
JNIEXPORT jboolean JNICALL Java_jtux_UFile_S_1ISREG(JNIEnv *env, jclass obj,
jint m)
{
return S_ISREG(m);
}
JNIEXPORT jboolean JNICALL Java_jtux_UFile_S_1ISSOCK(JNIEnv *env, jclass obj,
jint m)
{
return S_ISSOCK(m);
}
JNIEXPORT jint JNICALL Java_jtux_UFile_select(JNIEnv *env, jclass obj,
jint nfds, jobject readset, jobject writeset, jobject errorset, jobject timeout)
{
jbyteArray ba_read, ba_write, ba_error;
jbyte *b_read = get_fd_set(env, readset, &ba_read);
jbyte *b_write = get_fd_set(env, writeset, &ba_write);
jbyte *b_error = get_fd_set(env, errorset, &ba_error);
jclass cls = (*env)->FindClass(env, "jtux/UProcess$s_timeval");
int r;
long n;
struct timeval tvbuf, *tv = &tvbuf;
if (timeout == NULL)
tv = NULL;
else {
if (!field_jtoc_long(env, cls, "tv_sec", timeout, &n))
return false;
tv->tv_sec = (time_t)n;
if (!field_jtoc_long(env, cls, "tv_usec", timeout, &n))
return false;
tv->tv_usec = (suseconds_t)n;
printf("%ld %ld\n", (long)tv->tv_sec, (long)tv->tv_usec);
}
JTHROW_neg1(r = select(nfds, (fd_set *)b_read, (fd_set *)b_write, (fd_set *)b_error, tv))
release_fd_set(env, ba_read, b_read);
release_fd_set(env, ba_write, b_write);
release_fd_set(env, ba_error, b_error);
return r;
}
JNIEXPORT void JNICALL Java_jtux_UFile_stat(JNIEnv *env, jclass obj,
jstring path, jobject buf)
{
JSTR_GET_DECL(path_c, path)
struct stat sbuf;
int r;
JSTR_NULLTEST(path_c)
JTHROW_neg1(r = stat(path_c, &sbuf))
JSTR_REL(path_c, path)
if (r == -1)
return;
if (!statbuf_to_java(env, buf, &sbuf))
return;
}
JNIEXPORT void JNICALL Java_jtux_UFile_statvfs(JNIEnv *env, jclass obj,
jstring path, jobject buf)
{
JSTR_GET_DECL(path_c, path)
struct statvfs sbuf;
int r;
JSTR_NULLTEST(path_c)
JTHROW_neg1(r = statvfs(path_c, &sbuf))
JSTR_REL(path_c, path)
if (r == -1)
return;
if (!statvfsbuf_to_java(env, buf, &sbuf))
return;
}
JNIEXPORT void JNICALL Java_jtux_UFile_symlink(JNIEnv *env, jclass obj,
jstring oldpath, jstring newpath)
{
JSTR_GET_DECL(oldpath_c, oldpath)
JSTR_GET_DECL(newpath_c, newpath)
JSTR_NULLTEST(oldpath_c)
JSTR_NULLTEST(newpath_c)
JTHROW_neg1(symlink(oldpath_c, newpath_c))
JSTR_REL(oldpath_c, oldpath)
JSTR_REL(newpath_c, newpath)
}
JNIEXPORT void JNICALL Java_jtux_UFile_sync(JNIEnv *env, jclass obj)
{
sync();
}
JNIEXPORT void JNICALL Java_jtux_UFile_truncate(JNIEnv *env, jclass obj,
jstring path, jlong length)
{
JSTR_GET_DECL(path_c, path)
JSTR_NULLTEST(path_c)
JTHROW_neg1(truncate(path_c, length))
JSTR_REL(path_c, path)
}
JNIEXPORT void JNICALL Java_jtux_UFile_unlink(JNIEnv *env, jclass obj,
jstring path)
{
JSTR_GET_DECL(path_c, path)
JSTR_NULLTEST(path_c)
JTHROW_neg1(unlink(path_c))
JSTR_REL(path_c, path)
}
JNIEXPORT void JNICALL Java_jtux_UFile_utime(JNIEnv *env, jclass obj,
jstring path, jobject timbuf)
{
JSTR_GET_DECL(path_c, path)
jclass cls = (*env)->FindClass(env, "jtux/UFile$s_utimbuf");
struct utimbuf tbuf, *t_c = &tbuf;
if (timbuf == NULL)
t_c = NULL;
else {
if (!field_jtoc_long(env, cls, "actime", timbuf, &t_c->actime))
return;
if (!field_jtoc_long(env, cls, "modtime", timbuf, &t_c->modtime))
return;
}
JSTR_NULLTEST(path_c)
JTHROW_neg1(utime(path_c, t_c))
JSTR_REL(path_c, path)
}
JNIEXPORT jint JNICALL Java_jtux_UFile_write(JNIEnv *env, jclass obj,
jint fd, jbyteArray buf, jint nbytes)
{
void *buf_c;
ssize_t r;
if ((buf_c = (*env)->GetByteArrayElements(env, buf, NULL)) == NULL)
return -1;
JTHROW_neg1(r = write(fd, buf_c, nbytes))
(*env)->ReleaseByteArrayElements(env, buf, buf_c, JNI_ABORT);
return r;
}
JNIEXPORT jint JNICALL Java_jtux_UFile_writev(JNIEnv *env, jclass obj,
jint fd, jobjectArray iov, jint iovcnt)
{
struct iovec *v;
jbyteArray *v_bytearray;
ssize_t r;
if ((v = iovec_jtoc(env, iov, iovcnt, &v_bytearray)) == NULL)
return -1;
JTHROW_neg1(r = writev(fd, v, iovcnt))
iovec_jtoc_release_nocopy(env, v, iovcnt, v_bytearray);
return r;
#if 0
struct iovec *;
int i;
jclass cls = (*env)->FindClass(env, "jtux/UFile$s_iovec");
JTHROW_null(v = malloc(iovcnt * sizeof(struct iovec)))
JTHROW_null(v_bytearray = malloc(iovcnt * sizeof(jbyteArray)))
if (v == NULL || v_bytearray == NULL)
return -1;
for (i = 0; i < iovcnt; i++) {
jobject v_obj = (*env)->GetObjectArrayElement(env, iov, i);
if (v_obj == NULL) {
free(v);
free(v_bytearray);
return -1;
}
if (!field_jtoc_bytearray(env, cls, "iov_base", v_obj, &v[i].iov_base,
&v_bytearray[i]))
return -1;
if (!field_jtoc_int(env, cls, "iov_len", v_obj, &v[i].iov_len))
return -1;
}
JTHROW_neg1(r = writev(fd, v, iovcnt))
for (i = 0; i < iovcnt; i++)
field_jtoc_bytearray_release_nocopy(env, v_bytearray[i], v[i].iov_base);
free(v);
free(v_bytearray);
return r;
#endif
}