Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(189)

Side by Side Diff: c/file.c

Issue 2842333002: Updated netty-tcnative to version 2.0.0.Final (Closed)
Patch Set: Created 3 years, 8 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
« no previous file with comments | « c/error.c ('k') | c/info.c » ('j') | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
OLDNEW
(Empty)
1 /* Licensed to the Apache Software Foundation (ASF) under one or more
2 * contributor license agreements. See the NOTICE file distributed with
3 * this work for additional information regarding copyright ownership.
4 * The ASF licenses this file to You under the Apache License, Version 2.0
5 * (the "License"); you may not use this file except in compliance with
6 * the License. You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16
17 /*
18 *
19 * @author Mladen Turk
20 * @version $Id: file.c 1442587 2013-02-05 13:49:48Z rjung $
21 */
22
23 #include "tcn.h"
24 #include "apr_file_io.h"
25
26
27 TCN_IMPLEMENT_CALL(jint, File, close)(TCN_STDARGS, jlong file)
28 {
29 apr_file_t *f = J2P(file, apr_file_t *);
30 UNREFERENCED_STDARGS;
31 return (jint)apr_file_close(f);
32 }
33
34 TCN_IMPLEMENT_CALL(jint, File, eof)(TCN_STDARGS, jlong file)
35 {
36 apr_file_t *f = J2P(file, apr_file_t *);
37 UNREFERENCED_STDARGS;
38 return (jint)apr_file_eof(f);
39 }
40
41 TCN_IMPLEMENT_CALL(jint, File, flush)(TCN_STDARGS, jlong file)
42 {
43 apr_file_t *f = J2P(file, apr_file_t *);
44 UNREFERENCED_STDARGS;
45 return (jint)apr_file_flush(f);
46 }
47
48 TCN_IMPLEMENT_CALL(jint, File, unlock)(TCN_STDARGS, jlong file)
49 {
50 apr_file_t *f = J2P(file, apr_file_t *);
51 UNREFERENCED_STDARGS;
52 return (jint)apr_file_unlock(f);
53 }
54
55 TCN_IMPLEMENT_CALL(jint, File, flagsGet)(TCN_STDARGS, jlong file)
56 {
57 apr_file_t *f = J2P(file, apr_file_t *);
58 UNREFERENCED_STDARGS;
59 return (jint)apr_file_flags_get(f);
60 }
61
62 TCN_IMPLEMENT_CALL(jint, File, lock)(TCN_STDARGS, jlong file, jint flags)
63 {
64 apr_file_t *f = J2P(file, apr_file_t *);
65 UNREFERENCED_STDARGS;
66 return (jint)apr_file_lock(f, (int)flags);
67 }
68
69 TCN_IMPLEMENT_CALL(jint, File, trunc)(TCN_STDARGS, jlong file, jlong off)
70 {
71 apr_file_t *f = J2P(file, apr_file_t *);
72 UNREFERENCED_STDARGS;
73 return (jint)apr_file_trunc(f, (apr_off_t)off);
74 }
75
76 TCN_IMPLEMENT_CALL(jlong, File, open)(TCN_STDARGS, jstring fname,
77 jint flag, jint perm,
78 jlong pool)
79 {
80 apr_pool_t *p = J2P(pool, apr_pool_t *);
81 apr_file_t *f = NULL;
82 TCN_ALLOC_CSTRING(fname);
83
84 UNREFERENCED(o);
85 TCN_THROW_IF_ERR(apr_file_open(&f, J2S(fname), (apr_int32_t)flag,
86 (apr_fileperms_t)perm, p), f);
87
88 cleanup:
89 TCN_FREE_CSTRING(fname);
90 return P2J(f);
91 }
92
93 TCN_IMPLEMENT_CALL(jlong, File, mktemp)(TCN_STDARGS, jstring templ,
94 jint flags,
95 jlong pool)
96 {
97 apr_pool_t *p = J2P(pool, apr_pool_t *);
98 apr_file_t *f = NULL;
99 char *ctempl = tcn_strdup(e, templ);
100
101 UNREFERENCED(o);
102 if (!ctempl) {
103 TCN_THROW_OS_ERROR(e);
104 return 0;
105 }
106 TCN_THROW_IF_ERR(apr_file_mktemp(&f, ctempl,
107 (apr_int32_t)flags, p), f);
108
109 cleanup:
110 free(ctempl);
111 return P2J(f);
112 }
113
114 TCN_IMPLEMENT_CALL(jint, File, remove)(TCN_STDARGS, jstring path, jlong pool)
115 {
116 apr_pool_t *p = J2P(pool, apr_pool_t *);
117 TCN_ALLOC_CSTRING(path);
118 apr_status_t rv;
119
120 UNREFERENCED(o);
121 rv = apr_file_remove(J2S(path), p);
122 TCN_FREE_CSTRING(path);
123 return (jint)rv;
124 }
125
126 TCN_IMPLEMENT_CALL(jint, File, rename)(TCN_STDARGS, jstring from,
127 jstring to, jlong pool)
128 {
129 apr_pool_t *p = J2P(pool, apr_pool_t *);
130 TCN_ALLOC_CSTRING(from);
131 TCN_ALLOC_CSTRING(to);
132 apr_status_t rv;
133
134 UNREFERENCED(o);
135 rv = apr_file_rename(J2S(from), J2S(to), p);
136 TCN_FREE_CSTRING(from);
137 TCN_FREE_CSTRING(to);
138 return (jint)rv;
139 }
140
141 TCN_IMPLEMENT_CALL(jint, File, copy)(TCN_STDARGS, jstring from,
142 jstring to, jint perms, jlong pool)
143 {
144 apr_pool_t *p = J2P(pool, apr_pool_t *);
145 TCN_ALLOC_CSTRING(from);
146 TCN_ALLOC_CSTRING(to);
147 apr_status_t rv;
148
149 UNREFERENCED(o);
150 rv = apr_file_copy(J2S(from), J2S(to), (apr_fileperms_t)perms, p);
151 TCN_FREE_CSTRING(from);
152 TCN_FREE_CSTRING(to);
153 return (jint)rv;
154 }
155
156 TCN_IMPLEMENT_CALL(jint, File, append)(TCN_STDARGS, jstring from,
157 jstring to, jint perms, jlong pool)
158 {
159 apr_pool_t *p = J2P(pool, apr_pool_t *);
160 TCN_ALLOC_CSTRING(from);
161 TCN_ALLOC_CSTRING(to);
162 apr_status_t rv;
163
164 UNREFERENCED(o);
165 rv = apr_file_append(J2S(from), J2S(to), (apr_fileperms_t)perms, p);
166 TCN_FREE_CSTRING(from);
167 TCN_FREE_CSTRING(to);
168 return (jint)rv;
169 }
170
171 TCN_IMPLEMENT_CALL(jstring, File, nameGet)(TCN_STDARGS, jlong file)
172 {
173 apr_file_t *f = J2P(file, apr_file_t *);
174 jstring name = NULL;
175 const char *fname;
176
177 UNREFERENCED(o);
178 if (apr_file_name_get(&fname, f) == APR_SUCCESS)
179 name = AJP_TO_JSTRING(fname);
180
181 return name;
182 }
183
184 TCN_IMPLEMENT_CALL(jint, File, permsSet)(TCN_STDARGS, jstring file, jint perms)
185 {
186 TCN_ALLOC_CSTRING(file);
187 apr_status_t rv;
188
189 UNREFERENCED(o);
190 rv = apr_file_perms_set(J2S(file), (apr_fileperms_t)perms);
191 TCN_FREE_CSTRING(file);
192 return (jint)rv;
193 }
194
195 TCN_IMPLEMENT_CALL(jint, File, attrsSet)(TCN_STDARGS, jstring file, jint attrs,
196 jint mask, jlong pool)
197 {
198 apr_pool_t *p = J2P(pool, apr_pool_t *);
199 TCN_ALLOC_CSTRING(file);
200 apr_status_t rv;
201
202 UNREFERENCED(o);
203 rv = apr_file_attrs_set(J2S(file), (apr_fileattrs_t)attrs,
204 (apr_fileattrs_t)mask, p);
205 TCN_FREE_CSTRING(file);
206 return (jint)rv;
207 }
208
209 TCN_IMPLEMENT_CALL(jint, File, mtimeSet)(TCN_STDARGS, jstring file, jlong mtime,
210 jlong pool)
211 {
212 apr_pool_t *p = J2P(pool, apr_pool_t *);
213 TCN_ALLOC_CSTRING(file);
214 apr_status_t rv;
215
216 UNREFERENCED(o);
217 rv = apr_file_mtime_set(J2S(file), J2T(mtime), p);
218 TCN_FREE_CSTRING(file);
219 return (jint)rv;
220 }
221
222 TCN_IMPLEMENT_CALL(jlong, File, seek)(TCN_STDARGS, jlong file,
223 jint where, jlong offset)
224 {
225 apr_file_t *f = J2P(file, apr_file_t *);
226 apr_off_t pos = (apr_off_t)offset;
227 apr_seek_where_t w;
228 UNREFERENCED(o);
229 switch (where) {
230 case 1:
231 w = APR_CUR;
232 break;
233 case 2:
234 w = APR_END;
235 break;
236 default:
237 w = APR_SET;
238 break;
239 }
240 TCN_THROW_IF_ERR(apr_file_seek(f, w, &pos), pos);
241
242 cleanup:
243 return (jlong)pos;
244 }
245
246 TCN_IMPLEMENT_CALL(jint, File, putc)(TCN_STDARGS, jbyte c, jlong file)
247 {
248 apr_file_t *f = J2P(file, apr_file_t *);
249 UNREFERENCED_STDARGS;
250 return (jint)apr_file_putc((char)c, f);
251 }
252
253 TCN_IMPLEMENT_CALL(jint, File, getc)(TCN_STDARGS, jlong file)
254 {
255 apr_file_t *f = J2P(file, apr_file_t *);
256 char ch;
257
258 UNREFERENCED_STDARGS;
259 TCN_THROW_IF_ERR(apr_file_getc(&ch, f), ch);
260
261 cleanup:
262 return (jint)ch;
263 }
264
265 TCN_IMPLEMENT_CALL(jint, File, ungetc)(TCN_STDARGS, jbyte c, jlong file)
266 {
267 apr_file_t *f = J2P(file, apr_file_t *);
268 UNREFERENCED_STDARGS;
269 return (jint)apr_file_ungetc((char)c, f);
270 }
271
272 TCN_IMPLEMENT_CALL(jint, File, puts)(TCN_STDARGS, jbyteArray str, jlong file)
273 {
274 apr_status_t rv = APR_EINVAL;
275 apr_file_t *f = J2P(file, apr_file_t *);
276 jbyte *bytes = (*e)->GetPrimitiveArrayCritical(e, str, NULL);
277
278 UNREFERENCED(o);
279 if (bytes) {
280 rv = apr_file_puts((const char *)bytes, f);
281 (*e)->ReleasePrimitiveArrayCritical(e, str, bytes, JNI_ABORT);
282 }
283 return (jint)rv;
284 }
285
286 TCN_IMPLEMENT_CALL(jint, File, write)(TCN_STDARGS, jlong file,
287 jbyteArray buf, jint offset, jint towrite)
288 {
289 apr_file_t *f = J2P(file, apr_file_t *);
290 apr_size_t nbytes = (apr_size_t)towrite;
291 jbyte *bytes = (*e)->GetPrimitiveArrayCritical(e, buf, NULL);
292 apr_status_t ss;
293
294 UNREFERENCED(o);
295 if (towrite < 0)
296 towrite = (*e)->GetArrayLength(e, buf);
297 ss = apr_file_write(f, bytes + offset, &nbytes);
298
299 (*e)->ReleasePrimitiveArrayCritical(e, buf, bytes, JNI_ABORT);
300 if (ss == APR_SUCCESS)
301 return (jint)nbytes;
302 else
303 return -(jint)ss;
304 }
305
306 TCN_IMPLEMENT_CALL(jint, File, writeb)(TCN_STDARGS, jlong file,
307 jobject buf, jint offset, jint towrite)
308 {
309 apr_file_t *f = J2P(file, apr_file_t *);
310 apr_size_t nbytes = (apr_size_t)towrite;
311 char *bytes = (char *)(*e)->GetDirectBufferAddress(e, buf);
312 apr_status_t ss = APR_EINVAL;
313
314 UNREFERENCED(o);
315 if (bytes)
316 ss = apr_file_write(f, bytes + offset, &nbytes);
317
318 if (ss == APR_SUCCESS)
319 return (jint)nbytes;
320 else
321 return -(jint)ss;
322 }
323
324 TCN_IMPLEMENT_CALL(jint, File, writeFull)(TCN_STDARGS, jlong file,
325 jbyteArray buf, jint offset, jint towr ite)
326 {
327 apr_file_t *f = J2P(file, apr_file_t *);
328 apr_size_t nbytes = (apr_size_t)towrite;
329 apr_size_t written = 0;
330 apr_status_t ss;
331 jbyte *bytes = (*e)->GetByteArrayElements(e, buf, NULL);
332
333 UNREFERENCED(o);
334 if (towrite < 0)
335 towrite = (*e)->GetArrayLength(e, buf);
336 ss = apr_file_write_full(f, bytes + offset, nbytes, &written);
337
338 (*e)->ReleaseByteArrayElements(e, buf, bytes, JNI_ABORT);
339 if (ss == APR_SUCCESS)
340 return (jint)written;
341 else
342 return -(jint)ss;
343 }
344
345 TCN_IMPLEMENT_CALL(jint, File, writeFullb)(TCN_STDARGS, jlong file,
346 jobject buf, jint offset, jint towrit e)
347 {
348 apr_file_t *f = J2P(file, apr_file_t *);
349 apr_size_t nbytes = (apr_size_t)towrite;
350 apr_size_t written = 0;
351 apr_status_t ss = APR_EINVAL;
352 char *bytes = (char *)(*e)->GetDirectBufferAddress(e, buf);
353
354 UNREFERENCED(o);
355 if (bytes)
356 ss = apr_file_write_full(f, bytes + offset, nbytes, &written);
357
358 if (ss == APR_SUCCESS)
359 return (jint)written;
360 else
361 return -(jint)ss;
362 }
363
364 TCN_IMPLEMENT_CALL(jint, File, writev)(TCN_STDARGS, jlong file,
365 jobjectArray bufs)
366 {
367 apr_file_t *f = J2P(file, apr_file_t *);
368 jsize nvec = (*e)->GetArrayLength(e, bufs);
369 jsize i;
370 struct iovec vec[APR_MAX_IOVEC_SIZE];
371 jobject ba[APR_MAX_IOVEC_SIZE];
372 apr_size_t written = 0;
373 apr_status_t ss;
374
375 UNREFERENCED(o);
376
377 if (nvec >= APR_MAX_IOVEC_SIZE) {
378 /* TODO: Throw something here */
379 return 0;
380 }
381 for (i = 0; i < nvec; i++) {
382 ba[i] = (*e)->GetObjectArrayElement(e, bufs, i);
383 vec[i].iov_len = (*e)->GetArrayLength(e, ba[i]);
384 vec[i].iov_base = (void *)((*e)->GetByteArrayElements(e, ba[i], NULL));
385 }
386
387 ss = apr_file_writev(f, vec, nvec, &written);
388
389 for (i = 0; i < nvec; i++) {
390 (*e)->ReleaseByteArrayElements(e, ba[i], (jbyte *)vec[i].iov_base, JNI_A BORT);
391 }
392 if (ss == APR_SUCCESS)
393 return (jint)written;
394 else
395 return -(jint)ss;
396 }
397
398 TCN_IMPLEMENT_CALL(jint, File, writevFull)(TCN_STDARGS, jlong file,
399 jobjectArray bufs)
400 {
401 apr_file_t *f = J2P(file, apr_file_t *);
402 jsize nvec = (*e)->GetArrayLength(e, bufs);
403 jsize i;
404 struct iovec vec[APR_MAX_IOVEC_SIZE];
405 jobject ba[APR_MAX_IOVEC_SIZE];
406 apr_size_t written = 0;
407 apr_status_t ss;
408
409 UNREFERENCED(o);
410
411 if (nvec >= APR_MAX_IOVEC_SIZE) {
412 /* TODO: Throw something here */
413 return 0;
414 }
415 for (i = 0; i < nvec; i++) {
416 ba[i] = (*e)->GetObjectArrayElement(e, bufs, i);
417 vec[i].iov_len = (*e)->GetArrayLength(e, ba[i]);
418 vec[i].iov_base = (void *)((*e)->GetByteArrayElements(e, ba[i], NULL));
419 }
420 #if (APR_VERSION_MAJOR >= 1) && (APR_VERSION_MINOR >= 1)
421 ss = apr_file_writev_full(f, vec, nvec, &written);
422 #else
423 ss = apr_file_writev(f, vec, nvec, &written);
424 #endif
425
426 for (i = 0; i < nvec; i++) {
427 (*e)->ReleaseByteArrayElements(e, ba[i], (jbyte *)vec[i].iov_base,
428 JNI_ABORT);
429 }
430 if (ss == APR_SUCCESS)
431 return (jint)written;
432 else
433 return -(jint)ss;
434 }
435
436 TCN_IMPLEMENT_CALL(jint, File, read)(TCN_STDARGS, jlong file,
437 jbyteArray buf, jint offset,
438 jint toread)
439 {
440 apr_file_t *f = J2P(file, apr_file_t *);
441 apr_size_t nbytes = (apr_size_t)toread;
442 jbyte *bytes = (*e)->GetByteArrayElements(e, buf, NULL);
443 apr_status_t ss;
444
445 UNREFERENCED(o);
446 ss = apr_file_read(f, bytes + offset, &nbytes);
447
448 (*e)->ReleaseByteArrayElements(e, buf, bytes,
449 ss == APR_SUCCESS ? 0 : JNI_ABORT);
450 if (ss == APR_SUCCESS)
451 return (jint)nbytes;
452 else
453 return -(jint)ss;
454 }
455
456 TCN_IMPLEMENT_CALL(jint, File, readb)(TCN_STDARGS, jlong file,
457 jobject buf, jint offset,
458 jint toread)
459 {
460 apr_file_t *f = J2P(file, apr_file_t *);
461 apr_size_t nbytes = (apr_size_t)toread;
462 char *bytes = (char *)(*e)->GetDirectBufferAddress(e, buf);
463 apr_status_t ss = APR_EINVAL;
464
465 UNREFERENCED(o);
466 if (bytes)
467 ss = apr_file_read(f, bytes + offset, &nbytes);
468
469 if (ss == APR_SUCCESS)
470 return (jint)nbytes;
471 else
472 return -(jint)ss;
473 }
474
475 TCN_IMPLEMENT_CALL(jint, File, readFull)(TCN_STDARGS, jlong file,
476 jbyteArray buf, jint offset,
477 jint toread)
478 {
479 apr_file_t *f = J2P(file, apr_file_t *);
480 apr_size_t nbytes = (apr_size_t)toread;
481 apr_size_t nread = 0;
482 apr_status_t ss;
483 jbyte *bytes = (*e)->GetByteArrayElements(e, buf, NULL);
484
485 UNREFERENCED(o);
486 ss = apr_file_read_full(f, bytes + offset, nbytes, &nread);
487
488 (*e)->ReleaseByteArrayElements(e, buf, bytes,
489 ss == APR_SUCCESS ? 0 : JNI_ABORT);
490 if (ss == APR_SUCCESS)
491 return (jint)nread;
492 else
493 return -(jint)ss;
494 }
495
496 TCN_IMPLEMENT_CALL(jint, File, readFullb)(TCN_STDARGS, jlong file,
497 jobject buf, jint offset,
498 jint toread)
499 {
500 apr_file_t *f = J2P(file, apr_file_t *);
501 apr_size_t nbytes = (apr_size_t)toread;
502 apr_size_t nread = 0;
503 char *bytes = (char *)(*e)->GetDirectBufferAddress(e, buf);
504 apr_status_t ss = APR_EINVAL;
505
506 UNREFERENCED(o);
507 if (bytes)
508 ss = apr_file_read_full(f, bytes + offset, nbytes, &nread);
509
510 if (ss == APR_SUCCESS)
511 return (jint)nread;
512 else
513 return -(jint)ss;
514 }
515
516 TCN_IMPLEMENT_CALL(jint, File, gets)(TCN_STDARGS, jbyteArray buf, jint offset,
517 jlong file)
518 {
519 apr_status_t rv;
520 apr_file_t *f = J2P(file, apr_file_t *);
521 jsize nbytes = (*e)->GetArrayLength(e, buf);
522 jbyte *bytes = (*e)->GetByteArrayElements(e, buf, NULL);
523
524 UNREFERENCED(o);
525 rv = apr_file_gets((char*)(bytes + offset),nbytes - offset, f);
526 (*e)->ReleaseByteArrayElements(e, buf, bytes,
527 rv == APR_SUCCESS ? 0 : JNI_ABORT);
528 return (jint)rv;
529 }
530
531 TCN_IMPLEMENT_CALL(jint, File, pipeCreate)(TCN_STDARGS, jlongArray io, jlong poo l)
532 {
533 apr_status_t rv;
534 apr_pool_t *p = J2P(pool, apr_pool_t *);
535 jsize npipes = (*e)->GetArrayLength(e, io);
536 jlong *pipes = (*e)->GetLongArrayElements(e, io, NULL);
537 apr_file_t *in;
538 apr_file_t *out;
539
540 UNREFERENCED(o);
541 if (npipes < 2) {
542 (*e)->ReleaseLongArrayElements(e, io, pipes, JNI_ABORT);
543 return APR_EINVAL;
544 }
545
546 rv = apr_file_pipe_create(&in, &out, p);
547 if (rv == APR_SUCCESS) {
548 pipes[0] = P2J(in);
549 pipes[1] = P2J(out);
550 (*e)->ReleaseLongArrayElements(e, io, pipes, 0);
551 }
552 else
553 (*e)->ReleaseLongArrayElements(e, io, pipes, JNI_ABORT);
554
555 return (jint)rv;
556 }
557
558 TCN_IMPLEMENT_CALL(jint, File, pipeTimeoutSet)(TCN_STDARGS, jlong pipe,
559 jlong timeout)
560 {
561 apr_file_t *f = J2P(pipe, apr_file_t *);
562 UNREFERENCED_STDARGS;
563 return (jint)apr_file_pipe_timeout_set(f, J2T(timeout));
564 }
565
566 TCN_IMPLEMENT_CALL(jlong, File, pipeTimeoutGet)(TCN_STDARGS, jlong pipe)
567 {
568 apr_file_t *f = J2P(pipe, apr_file_t *);
569 apr_interval_time_t timeout;
570
571 UNREFERENCED(o);
572 TCN_THROW_IF_ERR(apr_file_pipe_timeout_get(f, &timeout), timeout);
573
574 cleanup:
575 return (jlong)timeout;
576 }
577
578 TCN_IMPLEMENT_CALL(jlong, File, dup)(TCN_STDARGS, jlong newf, jlong file,
579 jlong pool)
580 {
581 apr_file_t *f = J2P(file, apr_file_t *);
582 apr_pool_t *p = J2P(pool, apr_pool_t *);
583 apr_file_t *d = J2P(newf, apr_file_t *);
584
585 UNREFERENCED(o);
586 TCN_THROW_IF_ERR(apr_file_dup(&d, f, p), d);
587
588 cleanup:
589 return P2J(d);
590 }
591
592 TCN_IMPLEMENT_CALL(jint, File, dup2)(TCN_STDARGS, jlong newf, jlong file,
593 jlong pool)
594 {
595 apr_file_t *f = J2P(file, apr_file_t *);
596 apr_pool_t *p = J2P(pool, apr_pool_t *);
597 apr_file_t *d = J2P(newf, apr_file_t *);
598
599 UNREFERENCED_STDARGS;
600 return (jint)apr_file_dup2(d, f, p);
601 }
OLDNEW
« no previous file with comments | « c/error.c ('k') | c/info.c » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698