a2io.h 6.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199
  1. /* <!-- copyright */
  2. /*
  3. * aria2 - The high speed download utility
  4. *
  5. * Copyright (C) 2006 Tatsuhiro Tsujikawa
  6. *
  7. * This program is free software; you can redistribute it and/or modify
  8. * it under the terms of the GNU General Public License as published by
  9. * the Free Software Foundation; either version 2 of the License, or
  10. * (at your option) any later version.
  11. *
  12. * This program is distributed in the hope that it will be useful,
  13. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  14. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  15. * GNU General Public License for more details.
  16. *
  17. * You should have received a copy of the GNU General Public License
  18. * along with this program; if not, write to the Free Software
  19. * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
  20. *
  21. * In addition, as a special exception, the copyright holders give
  22. * permission to link the code of portions of this program with the
  23. * OpenSSL library under certain conditions as described in each
  24. * individual source file, and distribute linked combinations
  25. * including the two.
  26. * You must obey the GNU General Public License in all respects
  27. * for all of the code used other than OpenSSL. If you modify
  28. * file(s) with this exception, you may extend this exception to your
  29. * version of the file(s), but you are not obligated to do so. If you
  30. * do not wish to do so, delete this exception statement from your
  31. * version. If you delete this exception statement from all source
  32. * files in the program, then also delete it here.
  33. */
  34. /* copyright --> */
  35. #ifndef D_A2IO_H
  36. #define D_A2IO_H
  37. #include "common.h"
  38. #include <sys/types.h>
  39. #include <sys/stat.h>
  40. #include <unistd.h>
  41. #include <fcntl.h>
  42. #include <cerrno>
  43. #ifdef HAVE_POLL_H
  44. #include <poll.h>
  45. #endif // HAVE_POLL_H
  46. #ifdef HAVE_IO_H
  47. #include <io.h>
  48. #endif // HAVE_IO_H
  49. #ifdef HAVE_WINIOCTL_H
  50. #include <winioctl.h>
  51. #endif // HAVE_WINIOCTL_H
  52. #ifdef HAVE_SHARE_H
  53. #include <share.h>
  54. #endif // HAVE_SHARE_H
  55. // in some platforms following definitions are missing:
  56. #ifndef EINPROGRESS
  57. #define EINPROGRESS (WSAEINPROGRESS)
  58. #endif // EINPROGRESS
  59. #ifndef O_NONBLOCK
  60. #define O_NONBLOCK (O_NDELAY)
  61. #endif // O_NONBLOCK
  62. #ifndef O_BINARY
  63. #define O_BINARY (0)
  64. #endif // O_BINARY
  65. // st_mode flags
  66. #ifndef S_IRUSR
  67. #define S_IRUSR 0000400 /* read permission, owner */
  68. #endif /* S_IRUSR */
  69. #ifndef S_IWUSR
  70. #define S_IWUSR 0000200 /* write permission, owner */
  71. #endif /* S_IWUSR */
  72. #ifndef S_IXUSR
  73. #define S_IXUSR 0000100 /* execute/search permission, owner */
  74. #endif /* S_IXUSR */
  75. #ifndef S_IRWXU
  76. #define S_IRWXU (S_IRUSR | S_IWUSR | S_IXUSR)
  77. #endif /* S_IRWXU */
  78. #ifndef S_IRGRP
  79. #define S_IRGRP 0000040 /* read permission, group */
  80. #endif /* S_IRGRP */
  81. #ifndef S_IWGRP
  82. #define S_IWGRP 0000020 /* write permission, group */
  83. #endif /* S_IWGRP */
  84. #ifndef S_IXGRP
  85. #define S_IXGRP 0000010 /* execute/search permission, group */
  86. #endif /* S_IXGRP */
  87. #ifndef S_IRWXG
  88. #define S_IRWXG (S_IRGRP | S_IWGRP | S_IXGRP)
  89. #endif /* S_IRWXG */
  90. #ifndef S_IROTH
  91. #define S_IROTH 0000004 /* read permission, other */
  92. #endif /* S_IROTH */
  93. #ifndef S_IWOTH
  94. #define S_IWOTH 0000002 /* write permission, other */
  95. #endif /* S_IWOTH */
  96. #ifndef S_IXOTH
  97. #define S_IXOTH 0000001 /* execute/search permission, other */
  98. #endif /* S_IXOTH */
  99. #ifndef S_IRWXO
  100. #define S_IRWXO (S_IROTH | S_IWOTH | S_IXOTH)
  101. #endif /* S_IRWXO */
  102. // Use 'nul' instead of /dev/null in win32.
  103. #ifdef HAVE_WINSOCK2_H
  104. #define DEV_NULL "nul"
  105. #else
  106. #define DEV_NULL "/dev/null"
  107. #endif // HAVE_WINSOCK2_H
  108. // Use 'con' instead of '/dev/stdin' and '/dev/stdout' in win32.
  109. #ifdef HAVE_WINSOCK2_H
  110. #define DEV_STDIN "con"
  111. #define DEV_STDOUT "con"
  112. #else
  113. #define DEV_STDIN "/dev/stdin"
  114. #define DEV_STDOUT "/dev/stdout"
  115. #endif // HAVE_WINSOCK2_H
  116. #ifdef __MINGW32__
  117. #define a2lseek(fd, offset, origin) _lseeki64(fd, offset, origin)
  118. #define a2fseek(fd, offset, origin) _fseeki64(fd, offset, origin)
  119. #define a2fstat(fd, buf) _fstati64(fd, buf)
  120. #define a2ftell(fd) _ftelli64(fd)
  121. #define a2wstat(path, buf) _wstati64(path, buf)
  122. #ifdef stat
  123. #undef stat
  124. #endif // stat
  125. #define a2_struct_stat struct _stati64
  126. #define a2stat(path, buf) _wstati64(path, buf)
  127. #define a2tell(handle) _telli64(handle)
  128. #define a2mkdir(path, openMode) _wmkdir(path)
  129. #define a2utimbuf _utimbuf
  130. #define a2utime(path, times) _wutime(path, times)
  131. #define a2unlink(path) _wunlink(path)
  132. #define a2rmdir(path) _wrmdir(path)
  133. // For Windows, we share files for reading and writing.
  134. #define a2open(path, flags, mode) _wsopen(path, flags, _SH_DENYNO, mode)
  135. #define a2fopen(path, mode) _wfsopen(path, mode, _SH_DENYNO)
  136. // # define a2ftruncate(fd, length): We don't use ftruncate in Mingw build
  137. #define a2_off_t off_t
  138. #elif defined(__ANDROID__) || defined(ANDROID)
  139. #define a2lseek(fd, offset, origin) lseek64(fd, offset, origin)
  140. // # define a2fseek(fp, offset, origin): No fseek64 and not used in aria2
  141. #define a2fstat(fd, buf) fstat64(fd, buf)
  142. // # define a2ftell(fd): No ftell64 and not used in aria2
  143. #define a2_struct_stat struct stat
  144. #define a2stat(path, buf) stat64(path, buf)
  145. #define a2mkdir(path, openMode) mkdir(path, openMode)
  146. #define a2utimbuf utimbuf
  147. #define a2utime(path, times) ::utime(path, times)
  148. #define a2unlink(path) unlink(path)
  149. #define a2rmdir(path) rmdir(path)
  150. #define a2open(path, flags, mode) open(path, flags, mode)
  151. #define a2fopen(path, mode) fopen(path, mode)
  152. // Android NDK R8e does not provide ftruncate64 prototype, so let's
  153. // define it here.
  154. #ifdef __cplusplus
  155. extern "C" {
  156. #endif
  157. extern int ftruncate64(int fd, off64_t length);
  158. #ifdef __cplusplus
  159. }
  160. #endif
  161. #define a2ftruncate(fd, length) ftruncate64(fd, length)
  162. // Use off64_t directly since android does not offer transparent
  163. // switching between off_t and off64_t.
  164. #define a2_off_t off64_t
  165. #else // !__MINGW32__ && !(defined(__ANDROID__) || defined(ANDROID))
  166. #define a2lseek(fd, offset, origin) lseek(fd, offset, origin)
  167. #define a2fseek(fp, offset, origin) fseek(fp, offset, origin)
  168. #define a2fstat(fp, buf) fstat(fp, buf)
  169. #define a2ftell(fp) ftell(fp)
  170. #define a2_struct_stat struct stat
  171. #define a2stat(path, buf) stat(path, buf)
  172. #define a2mkdir(path, openMode) mkdir(path, openMode)
  173. #define a2utimbuf utimbuf
  174. #define a2utime(path, times) ::utime(path, times)
  175. #define a2unlink(path) unlink(path)
  176. #define a2rmdir(path) rmdir(path)
  177. #define a2open(path, flags, mode) open(path, flags, mode)
  178. #define a2fopen(path, mode) fopen(path, mode)
  179. #define a2ftruncate(fd, length) ftruncate(fd, length)
  180. #define a2_off_t off_t
  181. #endif
  182. #define OPEN_MODE S_IRUSR | S_IWUSR | S_IRGRP | S_IWGRP | S_IROTH | S_IWOTH
  183. #define DIR_OPEN_MODE S_IRWXU | S_IRWXG | S_IRWXO
  184. #ifdef __MINGW32__
  185. #define A2_BAD_FD INVALID_HANDLE_VALUE
  186. #else // !__MINGW32__
  187. #define A2_BAD_FD -1
  188. #endif // !__MINGW32__
  189. #endif // D_A2IO_H