cpio.c 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593
  1. /* cpio.c - Common java file IO native functions
  2. Copyright (C) 2005 Free Software Foundation, Inc.
  3. This file is part of GNU Classpath.
  4. GNU Classpath is free software; you can redistribute it and/or modify
  5. it under the terms of the GNU General Public License as published by
  6. the Free Software Foundation; either version 2, or (at your option)
  7. any later version.
  8. GNU Classpath is distributed in the hope that it will be useful, but
  9. WITHOUT ANY WARRANTY; without even the implied warranty of
  10. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  11. General Public License for more details.
  12. You should have received a copy of the GNU General Public License
  13. along with GNU Classpath; see the file COPYING. If not, write to the
  14. Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
  15. 02110-1301 USA.
  16. Linking this library statically or dynamically with other modules is
  17. making a combined work based on this library. Thus, the terms and
  18. conditions of the GNU General Public License cover the whole
  19. combination.
  20. As a special exception, the copyright holders of this library give you
  21. permission to link this library with independent modules to produce an
  22. executable, regardless of the license terms of these independent
  23. modules, and to copy and distribute the resulting executable under
  24. terms of your choice, provided that you also meet, for each linked
  25. independent module, the terms and conditions of the license of that
  26. module. An independent module is a module which is not derived from
  27. or based on this library. If you modify this library, you may extend
  28. this exception to your version of the library, but you are not
  29. obligated to do so. If you do not wish to do so, delete this
  30. exception statement from your version. */
  31. /* do not move; needed here because of some macro definitions */
  32. #include <config.h>
  33. #include <stdio.h>
  34. #include <stdlib.h>
  35. #include <errno.h>
  36. #include <string.h>
  37. #include <sys/types.h>
  38. #include <dirent.h>
  39. #include <jni.h>
  40. #if defined(HAVE_SYS_IOCTL_H)
  41. #define BSD_COMP /* Get FIONREAD on Solaris2 */
  42. #include <sys/ioctl.h>
  43. #endif
  44. #if defined(HAVE_SYS_FILIO_H) /* Get FIONREAD on Solaris 2.5 */
  45. #include <sys/filio.h>
  46. #endif
  47. #if defined(HAVE_SYS_STAT_H)
  48. #include <sys/stat.h>
  49. #endif
  50. #if defined(HAVE_FCNTL_H)
  51. #include <fcntl.h>
  52. #endif
  53. #if defined(HAVE_UNISTD_H)
  54. #include <unistd.h>
  55. #endif
  56. #if defined(HAVE_SYS_SELECT_H)
  57. #include <sys/select.h>
  58. #endif
  59. #if defined(HAVE_STATVFS)
  60. #include <sys/statvfs.h>
  61. #endif
  62. #include <utime.h>
  63. #include "cpnative.h"
  64. #include "cpio.h"
  65. /* Some POSIX systems don't have O_SYNC and O_DYSNC so we define them here. */
  66. #if !defined (O_SYNC) && defined (O_FSYNC)
  67. #define O_SYNC O_FSYNC
  68. #endif
  69. #if !defined (O_DSYNC) && defined (O_FSYNC)
  70. #define O_DSYNC O_FSYNC
  71. #endif
  72. /* If O_DSYNC is still not defined, use O_SYNC (needed for newlib). */
  73. #if !defined (O_DSYNC)
  74. #define O_DSYNC O_SYNC
  75. #endif
  76. JNIEXPORT int cpio_openFile (const char *filename, int *fd, int flags, int permissions)
  77. {
  78. int sflags = 0;
  79. int rwflags = flags & CPFILE_FLAG_READWRITE;
  80. int perms;
  81. if (flags & CPFILE_FLAG_CREATE)
  82. sflags |= O_CREAT;
  83. if (flags & CPFILE_FLAG_APPEND)
  84. sflags |= O_APPEND;
  85. if (flags & CPFILE_FLAG_TRUNCATE)
  86. sflags |= O_TRUNC;
  87. if (flags & CPFILE_FLAG_SYNC)
  88. sflags |= O_SYNC;
  89. if (flags & CPFILE_FLAG_DSYNC)
  90. sflags |= O_DSYNC;
  91. #if defined(O_BINARY)
  92. if (flags & CPFILE_FLAG_BINARY)
  93. sflags |= O_BINARY;
  94. #endif
  95. switch (rwflags)
  96. {
  97. case CPFILE_FLAG_READ:
  98. sflags |= O_RDONLY;
  99. break;
  100. case CPFILE_FLAG_WRITE:
  101. sflags |= O_WRONLY;
  102. break;
  103. case CPFILE_FLAG_READWRITE:
  104. sflags |= O_RDWR;
  105. break;
  106. }
  107. if (permissions == CPFILE_PERMISSION_NORMAL)
  108. perms = (S_IRUSR | S_IWUSR | S_IRGRP | S_IWGRP | S_IROTH | S_IWOTH);
  109. else
  110. perms = 0;
  111. *fd = open (filename, sflags, perms);
  112. if (*fd < 0)
  113. return errno;
  114. return CPNATIVE_OK;
  115. }
  116. JNIEXPORT int cpio_closeFile (int fd)
  117. {
  118. if (close (fd) < 0)
  119. return errno;
  120. return CPNATIVE_OK;
  121. }
  122. JNIEXPORT int cpio_availableBytes (int fd, jlong *bytes_available)
  123. {
  124. #if defined (FIONREAD)
  125. ssize_t n;
  126. if (ioctl (fd, FIONREAD, (char *)&n) != 0)
  127. return errno;
  128. *bytes_available = n;
  129. return CPNATIVE_OK;
  130. #elif defined(HAVE_FSTAT)
  131. struct stat statBuffer;
  132. off_t n;
  133. int result;
  134. *bytes_available = 0;
  135. if ((fstat (fd, &statBuffer) == 0) && S_ISREG (statBuffer.st_mode))
  136. {
  137. n = lseek (fd, 0, SEEK_CUR);
  138. if (n != -1)
  139. {
  140. *bytes_available = statBuffer.st_size - n;
  141. result = CPNATIVE_OK;
  142. }
  143. else
  144. {
  145. result = errno;
  146. }
  147. }
  148. else
  149. {
  150. result = errno;
  151. }
  152. return result;
  153. #elif defined(HAVE_SELECT)
  154. fd_set filedescriptset;
  155. struct timeval tv;
  156. int result;
  157. *bytes_available = 0;
  158. FD_ZERO (&filedescriptset);
  159. FD_SET (fd,&filedescriptset);
  160. memset (&tv, 0, sizeof(tv));
  161. switch (select (fd+1, &filedescriptset, NULL, NULL, &tv))
  162. {
  163. case -1:
  164. result=errno;
  165. break;
  166. case 0:
  167. *bytes_available = 0;
  168. result = CPNATIVE_OK;
  169. break;
  170. default:
  171. *bytes_available = 1;
  172. result = CPNATIVE_OK;
  173. break;
  174. }
  175. return result;
  176. #else
  177. *bytes_available = 0;
  178. return ENOTSUP;
  179. #endif
  180. }
  181. JNIEXPORT int cpio_getFileSize (int fd, jlong *filesize)
  182. {
  183. struct stat statBuffer;
  184. if (fstat(fd, &statBuffer) < 0)
  185. return errno;
  186. *filesize = statBuffer.st_size;
  187. return CPNATIVE_OK;
  188. }
  189. JNIEXPORT int cpio_getFilePosition (int fd, jlong *offset)
  190. {
  191. *offset = lseek (fd, 0, SEEK_CUR);
  192. if (*offset < 0)
  193. return errno;
  194. return CPNATIVE_OK;
  195. }
  196. JNIEXPORT int cpio_setFilePosition (int fd, jlong position)
  197. {
  198. if (lseek (fd, position, SEEK_SET) < 0)
  199. return errno;
  200. return CPNATIVE_OK;
  201. }
  202. JNIEXPORT int cpio_read (int fd, void *buffer, jint length, jint *bytes_read)
  203. {
  204. *bytes_read = read (fd, buffer, length);
  205. if (*bytes_read < 0)
  206. {
  207. return errno;
  208. }
  209. return CPNATIVE_OK;
  210. }
  211. JNIEXPORT int cpio_write (int fd, const void *buffer, jint length, jint *bytes_written)
  212. {
  213. *bytes_written = write (fd, buffer, length);
  214. if (*bytes_written < 0)
  215. return errno;
  216. return CPNATIVE_OK;
  217. }
  218. JNIEXPORT int cpio_fsync (int fd)
  219. {
  220. if (fsync (fd) < 0)
  221. return errno;
  222. return CPNATIVE_OK;
  223. }
  224. JNIEXPORT int cpio_truncate (int fd, jlong size)
  225. {
  226. if (ftruncate (fd, size) < 0)
  227. return errno;
  228. return CPNATIVE_OK;
  229. }
  230. JNIEXPORT int cpio_setFileSize (int native_fd, jlong new_size)
  231. {
  232. jlong file_size;
  233. jlong save_offset;
  234. int result;
  235. char data;
  236. jint bytes_written;
  237. result = cpio_getFileSize (native_fd, &file_size);
  238. if (result != CPNATIVE_OK)
  239. return result;
  240. /* Save off current position */
  241. result = cpio_getFilePosition (native_fd, &save_offset);
  242. if (result != CPNATIVE_OK)
  243. return result;
  244. if (file_size < new_size)
  245. {
  246. /* File is too short -- seek to one byte short of where we want,
  247. * then write a byte */
  248. /* move to position n-1 */
  249. result = cpio_setFilePosition (native_fd, new_size-1);
  250. if (result != CPNATIVE_OK)
  251. return result;
  252. /* write a byte
  253. Note: This will fail if we somehow get here in read only mode
  254. * That shouldn't happen */
  255. data = '\0';
  256. result = cpio_write (native_fd, &data, 1, &bytes_written);
  257. if (result != CPNATIVE_OK)
  258. return result;
  259. /* Reposition file pointer to where we started if not beyond new len. */
  260. if (save_offset < new_size)
  261. {
  262. result = cpio_setFilePosition (native_fd, save_offset);
  263. if (result != CPNATIVE_OK)
  264. return result;
  265. }
  266. }
  267. else if (new_size < file_size)
  268. {
  269. /* File is too long - use ftruncate if available */
  270. result = cpio_truncate (native_fd, new_size);
  271. if (result != CPNATIVE_OK)
  272. return result;
  273. /* Reposition file pointer when it now is beyond the end of file. */
  274. if (new_size < save_offset)
  275. {
  276. result = cpio_setFilePosition (native_fd, new_size);
  277. if (result != CPNATIVE_OK)
  278. return result;
  279. }
  280. }
  281. return CPNATIVE_OK;
  282. }
  283. int cpio_setFileReadonly (const char *filename)
  284. {
  285. struct stat statbuf;
  286. if (stat(filename, &statbuf) < 0)
  287. return errno;
  288. #ifdef S_IWRITE
  289. if (chmod(filename, statbuf.st_mode & ~(S_IWRITE | S_IWGRP | S_IWOTH)) < 0)
  290. return errno;
  291. #endif
  292. return 0;
  293. }
  294. int cpio_chmod (const char *filename, int permissions)
  295. {
  296. struct stat statbuf;
  297. int perms = 0;
  298. if (stat(filename, &statbuf) < 0)
  299. return errno;
  300. /* check for permission flags */
  301. if (permissions & CPFILE_FLAG_USR)
  302. {
  303. if (permissions & CPFILE_FLAG_READ)
  304. perms |= S_IRUSR;
  305. if (permissions & CPFILE_FLAG_WRITE)
  306. perms |= S_IWUSR;
  307. if (permissions & CPFILE_FLAG_EXEC)
  308. perms |= S_IXUSR;
  309. }
  310. else
  311. {
  312. if (permissions & CPFILE_FLAG_READ)
  313. perms |= (S_IRUSR | S_IRGRP | S_IROTH);
  314. if (permissions & CPFILE_FLAG_WRITE)
  315. perms |= (S_IWUSR | S_IWGRP | S_IWOTH);
  316. if (permissions & CPFILE_FLAG_EXEC)
  317. perms |= (S_IXUSR | S_IXGRP | S_IXOTH);
  318. }
  319. if (permissions & CPFILE_FLAG_OFF)
  320. perms = statbuf.st_mode & ~perms;
  321. else
  322. perms = statbuf.st_mode | perms;
  323. if (chmod(filename, perms) < 0)
  324. return errno;
  325. return 0;
  326. }
  327. JNIEXPORT long long
  328. cpio_df (__attribute__((unused)) const char *path,
  329. __attribute__((unused)) CPFILE_DF_TYPE type)
  330. {
  331. long long result = 0L;
  332. #if defined(HAVE_STATVFS)
  333. long long scale_factor = 0L;
  334. struct statvfs buf;
  335. if (statvfs (path, &buf) < 0)
  336. return 0L;
  337. /* f_blocks, f_bfree and f_bavail are defined in terms of f_frsize */
  338. scale_factor = (long long) (buf.f_frsize);
  339. switch (type)
  340. {
  341. case TOTAL:
  342. result = (long long) (buf.f_blocks * scale_factor);
  343. break;
  344. case FREE:
  345. result = (long long) (buf.f_bfree * scale_factor);
  346. break;
  347. case USABLE:
  348. result = (long long) (buf.f_bavail * scale_factor);
  349. break;
  350. default:
  351. result = 0L;
  352. break;
  353. }
  354. #endif
  355. return result;
  356. }
  357. int cpio_checkAccess (const char *filename, unsigned int flag)
  358. {
  359. struct stat statbuf;
  360. unsigned int perms = 0;
  361. if (stat(filename, &statbuf) < 0)
  362. return errno;
  363. switch (flag)
  364. {
  365. case CPFILE_FLAG_READ:
  366. perms = R_OK;
  367. break;
  368. case CPFILE_FLAG_WRITE:
  369. perms = W_OK;
  370. break;
  371. case CPFILE_FLAG_EXEC:
  372. default:
  373. perms = X_OK;
  374. break;
  375. }
  376. return (access (filename, perms));
  377. }
  378. int cpio_isFileExists (const char *filename)
  379. {
  380. struct stat statbuf;
  381. if (stat(filename, &statbuf) < 0)
  382. {
  383. return errno;
  384. }
  385. return 0;
  386. }
  387. int cpio_checkType (const char *filename, jint *entryType)
  388. {
  389. struct stat statbuf;
  390. if (stat(filename, &statbuf) < 0)
  391. return errno;
  392. if (S_ISDIR(statbuf.st_mode))
  393. *entryType = CPFILE_DIRECTORY;
  394. else
  395. *entryType = CPFILE_FILE;
  396. return 0;
  397. }
  398. int cpio_getModificationTime (const char *filename, jlong *mtime)
  399. {
  400. struct stat statbuf;
  401. if (stat(filename, &statbuf) < 0)
  402. return errno;
  403. *mtime = (jlong)statbuf.st_mtime * (jlong)1000;
  404. return 0;
  405. }
  406. int cpio_setModificationTime (const char *filename, jlong mtime)
  407. {
  408. struct stat statbuf;
  409. struct utimbuf buf;
  410. if (stat(filename, &statbuf) < 0)
  411. return errno;
  412. buf.actime = statbuf.st_atime;
  413. buf.modtime = mtime / 1000;
  414. if (utime(filename, &buf) < 0)
  415. return errno;
  416. return 0;
  417. }
  418. int cpio_removeFile (const char *filename)
  419. {
  420. if (unlink(filename) < 0 && rmdir(filename) < 0)
  421. return errno;
  422. return 0;
  423. }
  424. int cpio_mkdir (const char *path)
  425. {
  426. if (mkdir(path, S_IRWXU | S_IRWXG | S_IRWXO) < 0)
  427. return errno;
  428. return 0;
  429. }
  430. int cpio_rename (const char *old_name, const char *new_name)
  431. {
  432. if (rename(old_name, new_name) < 0)
  433. return errno;
  434. return 0;
  435. }
  436. int cpio_openDir (const char *dirname, void **handle)
  437. {
  438. *handle = (void *)opendir(dirname);
  439. if (*handle == NULL)
  440. return errno;
  441. return 0;
  442. }
  443. int cpio_closeDir (void *handle)
  444. {
  445. closedir((DIR *)handle);
  446. return 0;
  447. }
  448. int cpio_readDir (void *handle, char *filename)
  449. {
  450. struct dirent *dBuf;
  451. errno = 0;
  452. dBuf = readdir((DIR *)handle);
  453. if (dBuf == NULL)
  454. {
  455. /* Some OS's (OS X) return NULL on end-of-dir, but
  456. don't set errno to anything. */
  457. if (errno == 0)
  458. return ENOENT; /* Whatever. */
  459. return errno;
  460. }
  461. strncpy (filename, dBuf->d_name, FILENAME_MAX - 1);
  462. return 0;
  463. }
  464. int
  465. cpio_closeOnExec(int fd)
  466. {
  467. if (fcntl (fd, F_SETFD, FD_CLOEXEC) == -1)
  468. return errno;
  469. return 0;
  470. }