build/stlport/src/details/fstream_unistd.cpp

Sat, 03 Jan 2015 20:18:00 +0100

author
Michael Schloh von Bennewitz <michael@schloh.com>
date
Sat, 03 Jan 2015 20:18:00 +0100
branch
TOR_BUG_3246
changeset 7
129ffea94266
permissions
-rw-r--r--

Conditionally enable double key logic according to:
private browsing mode or privacy.thirdparty.isolate preference and
implement in GetCookieStringCommon and FindCookie where it counts...
With some reservations of how to convince FindCookie users to test
condition and pass a nullptr when disabling double key logic.

michael@0 1 /*
michael@0 2 * Copyright (c) 1999
michael@0 3 * Silicon Graphics Computer Systems, Inc.
michael@0 4 *
michael@0 5 * Copyright (c) 1999
michael@0 6 * Boris Fomitchev
michael@0 7 *
michael@0 8 * This material is provided "as is", with absolutely no warranty expressed
michael@0 9 * or implied. Any use is at your own risk.
michael@0 10 *
michael@0 11 * Permission to use or copy this software for any purpose is hereby granted
michael@0 12 * without fee, provided the above notices are retained on all copies.
michael@0 13 * Permission to modify the code and to distribute modified code is granted,
michael@0 14 * provided the above notices are retained, and a notice that the code was
michael@0 15 * modified is included with the above copyright notice.
michael@0 16 *
michael@0 17 */
michael@0 18
michael@0 19 #if defined (__SUNPPRO_CC) && !defined (_STLP_NO_NEW_C_HEADERS)
michael@0 20 # include <time.h>
michael@0 21 // For sunpro, it chokes if time.h is included through stat.h
michael@0 22 #endif
michael@0 23
michael@0 24 #include <fstream>
michael@0 25
michael@0 26 #ifdef __CYGWIN__
michael@0 27 # define __int64 long long
michael@0 28 #endif
michael@0 29
michael@0 30 extern "C" {
michael@0 31 // open/close/read/write
michael@0 32 #include <sys/stat.h> // For stat
michael@0 33 #if !defined (_CRAY) && ! defined (__EMX__)
michael@0 34 # include <sys/mman.h> // For mmap
michael@0 35 #endif
michael@0 36
michael@0 37 // on HP-UX 11, this one contradicts with pthread.h on pthread_atfork, unless we unset this
michael@0 38 #if defined (__hpux) && defined (__GNUC__)
michael@0 39 # undef _INCLUDE_POSIX1C_SOURCE
michael@0 40 #endif
michael@0 41
michael@0 42 #include <unistd.h>
michael@0 43 #include <fcntl.h>
michael@0 44 }
michael@0 45
michael@0 46 #ifdef __APPLE__
michael@0 47 # include <sys/sysctl.h>
michael@0 48 #endif
michael@0 49
michael@0 50 const _STLP_fd INVALID_STLP_FD = -1;
michael@0 51
michael@0 52 #ifndef O_ACCMODE
michael@0 53 # define O_ACCMODE (O_RDONLY|O_WRONLY|O_RDWR)
michael@0 54 #endif
michael@0 55
michael@0 56 // Compare with streamoff definition in stl/char_traits.h!
michael@0 57 #if defined (_STLP_USE_DEFAULT_FILE_OFFSET) || \
michael@0 58 (!defined(_LARGEFILE_SOURCE) && !defined (_LARGEFILE64_SOURCE))
michael@0 59 # define FSTAT fstat
michael@0 60 # define STAT stat
michael@0 61 # define LSEEK lseek
michael@0 62 # define MMAP mmap
michael@0 63 # define OPEN open
michael@0 64 #else
michael@0 65 # define FSTAT fstat64
michael@0 66 # define STAT stat64
michael@0 67 # define LSEEK lseek64
michael@0 68 # define MMAP mmap64
michael@0 69 # define OPEN open64
michael@0 70 #endif
michael@0 71
michael@0 72 #ifndef MAP_FAILED /* MMAP failure return code */
michael@0 73 # define MAP_FAILED -1
michael@0 74 #endif
michael@0 75
michael@0 76 _STLP_BEGIN_NAMESPACE
michael@0 77
michael@0 78 static ios_base::openmode flag_to_openmode(int mode)
michael@0 79 {
michael@0 80 ios_base::openmode ret = ios_base::__default_mode;
michael@0 81
michael@0 82 switch ( mode & O_ACCMODE ) {
michael@0 83 case O_RDONLY:
michael@0 84 ret = ios_base::in;
michael@0 85 break;
michael@0 86 case O_WRONLY:
michael@0 87 ret = ios_base::out;
michael@0 88 break;
michael@0 89 case O_RDWR:
michael@0 90 ret = ios_base::in | ios_base::out;
michael@0 91 break;
michael@0 92 }
michael@0 93
michael@0 94 if ( mode & O_APPEND )
michael@0 95 ret |= ios_base::app;
michael@0 96
michael@0 97 return ret;
michael@0 98 }
michael@0 99
michael@0 100 _STLP_MOVE_TO_PRIV_NAMESPACE
michael@0 101
michael@0 102 // Helper functions for _Filebuf_base.
michael@0 103
michael@0 104 static bool __is_regular_file(_STLP_fd fd) {
michael@0 105 struct STAT buf;
michael@0 106 return FSTAT(fd, &buf) == 0 && S_ISREG(buf.st_mode);
michael@0 107 }
michael@0 108
michael@0 109 // Number of characters in the file.
michael@0 110 static streamoff __file_size(_STLP_fd fd) {
michael@0 111 streamoff ret = 0;
michael@0 112
michael@0 113 struct STAT buf;
michael@0 114 if (FSTAT(fd, &buf) == 0 && S_ISREG(buf.st_mode))
michael@0 115 ret = buf.st_size > 0 ? buf.st_size : 0;
michael@0 116
michael@0 117 return ret;
michael@0 118 }
michael@0 119
michael@0 120 _STLP_MOVE_TO_STD_NAMESPACE
michael@0 121
michael@0 122 size_t _Filebuf_base::_M_page_size = 4096;
michael@0 123
michael@0 124 _Filebuf_base::_Filebuf_base()
michael@0 125 : _M_file_id(INVALID_STLP_FD),
michael@0 126 _M_openmode(0),
michael@0 127 _M_is_open(false),
michael@0 128 _M_should_close(false)
michael@0 129 {}
michael@0 130
michael@0 131 void _Filebuf_base::_S_initialize()
michael@0 132 {
michael@0 133 #if defined (__APPLE__)
michael@0 134 int mib[2];
michael@0 135 size_t pagesize, len;
michael@0 136 mib[0] = CTL_HW;
michael@0 137 mib[1] = HW_PAGESIZE;
michael@0 138 len = sizeof(pagesize);
michael@0 139 sysctl(mib, 2, &pagesize, &len, NULL, 0);
michael@0 140 _M_page_size = pagesize;
michael@0 141 #elif defined (__DJGPP) && defined (_CRAY)
michael@0 142 _M_page_size = BUFSIZ;
michael@0 143 #else
michael@0 144 _M_page_size = sysconf(_SC_PAGESIZE);
michael@0 145 #endif
michael@0 146 }
michael@0 147
michael@0 148 // Return the size of the file. This is a wrapper for stat.
michael@0 149 // Returns zero if the size cannot be determined or is ill-defined.
michael@0 150 streamoff _Filebuf_base::_M_file_size()
michael@0 151 {
michael@0 152 return _STLP_PRIV __file_size(_M_file_id);
michael@0 153 }
michael@0 154
michael@0 155 bool _Filebuf_base::_M_open(const char* name, ios_base::openmode openmode,
michael@0 156 long permission)
michael@0 157 {
michael@0 158 _STLP_fd file_no;
michael@0 159
michael@0 160 if (_M_is_open)
michael@0 161 return false;
michael@0 162
michael@0 163 int flags = 0;
michael@0 164
michael@0 165 // Unix makes no distinction between text and binary files.
michael@0 166 switch ( openmode & (~ios_base::ate & ~ios_base::binary) ) {
michael@0 167 case ios_base::out:
michael@0 168 case ios_base::out | ios_base::trunc:
michael@0 169 flags = O_WRONLY | O_CREAT | O_TRUNC;
michael@0 170 break;
michael@0 171 case ios_base::app:
michael@0 172 case ios_base::out | ios_base::app:
michael@0 173 flags = O_WRONLY | O_CREAT | O_APPEND;
michael@0 174 break;
michael@0 175 case ios_base::in:
michael@0 176 flags = O_RDONLY;
michael@0 177 permission = 0; // Irrelevant unless we're writing.
michael@0 178 break;
michael@0 179 case ios_base::in | ios_base::out:
michael@0 180 flags = O_RDWR;
michael@0 181 break;
michael@0 182 case ios_base::in | ios_base::out | ios_base::trunc:
michael@0 183 flags = O_RDWR | O_CREAT | O_TRUNC;
michael@0 184 break;
michael@0 185 case ios_base::in | ios_base::app:
michael@0 186 case ios_base::in | ios_base::out | ios_base::app:
michael@0 187 flags = O_RDWR | O_CREAT | O_APPEND;
michael@0 188 break;
michael@0 189 default: // The above are the only combinations of
michael@0 190 return false; // flags allowed by the C++ standard.
michael@0 191 }
michael@0 192
michael@0 193 file_no = OPEN(name, flags, permission);
michael@0 194
michael@0 195 if (file_no < 0)
michael@0 196 return false;
michael@0 197
michael@0 198 _M_is_open = true;
michael@0 199
michael@0 200 if ((openmode & (ios_base::ate | ios_base::app)) && (LSEEK(file_no, 0, SEEK_END) == -1)) {
michael@0 201 _M_is_open = false;
michael@0 202 }
michael@0 203
michael@0 204 _M_file_id = file_no;
michael@0 205 _M_should_close = _M_is_open;
michael@0 206 _M_openmode = openmode;
michael@0 207
michael@0 208 if (_M_is_open)
michael@0 209 _M_regular_file = _STLP_PRIV __is_regular_file(_M_file_id);
michael@0 210
michael@0 211 return (_M_is_open != 0);
michael@0 212 }
michael@0 213
michael@0 214
michael@0 215 bool _Filebuf_base::_M_open(const char* name, ios_base::openmode openmode)
michael@0 216 {
michael@0 217 // This doesn't really grant everyone in the world read/write
michael@0 218 // access. On Unix, file-creation system calls always clear
michael@0 219 // bits that are set in the umask from the permissions flag.
michael@0 220 return this->_M_open(name, openmode, S_IRUSR | S_IWUSR | S_IRGRP |
michael@0 221 S_IWGRP | S_IROTH | S_IWOTH);
michael@0 222 }
michael@0 223
michael@0 224 // Associated the filebuf with a file descriptor pointing to an already-
michael@0 225 // open file. Mode is set to be consistent with the way that the file
michael@0 226 // was opened.
michael@0 227 bool _Filebuf_base::_M_open(int file_no, ios_base::openmode)
michael@0 228 {
michael@0 229 if (_M_is_open || file_no < 0)
michael@0 230 return false;
michael@0 231
michael@0 232 int mode = fcntl(file_no, F_GETFL);
michael@0 233
michael@0 234 if (mode == -1)
michael@0 235 return false;
michael@0 236
michael@0 237 _M_openmode = flag_to_openmode(mode);
michael@0 238 _M_file_id = file_no;
michael@0 239
michael@0 240 _M_is_open = true;
michael@0 241 _M_should_close = false;
michael@0 242 _M_regular_file = _STLP_PRIV __is_regular_file(_M_file_id);
michael@0 243 return true;
michael@0 244 }
michael@0 245
michael@0 246 bool _Filebuf_base::_M_close()
michael@0 247 {
michael@0 248 if (!_M_is_open)
michael@0 249 return false;
michael@0 250
michael@0 251 bool ok = _M_should_close ? (close(_M_file_id) == 0) : true;
michael@0 252
michael@0 253 _M_is_open = _M_should_close = false;
michael@0 254 _M_openmode = 0;
michael@0 255 return ok;
michael@0 256 }
michael@0 257
michael@0 258 // Read up to n characters into a buffer. Return value is number of
michael@0 259 // characters read.
michael@0 260 ptrdiff_t _Filebuf_base::_M_read(char* buf, ptrdiff_t n)
michael@0 261 {
michael@0 262 return read(_M_file_id, buf, n);
michael@0 263 }
michael@0 264
michael@0 265 // Write n characters from a buffer. Return value: true if we managed
michael@0 266 // to write the entire buffer, false if we didn't.
michael@0 267 bool _Filebuf_base::_M_write(char* buf, ptrdiff_t n)
michael@0 268 {
michael@0 269 for (;;) {
michael@0 270 ptrdiff_t written = write(_M_file_id, buf, n);
michael@0 271
michael@0 272 if (n == written) {
michael@0 273 return true;
michael@0 274 }
michael@0 275
michael@0 276 if (written > 0 && written < n) {
michael@0 277 n -= written;
michael@0 278 buf += written;
michael@0 279 } else {
michael@0 280 return false;
michael@0 281 }
michael@0 282 }
michael@0 283 }
michael@0 284
michael@0 285 // Wrapper for lseek or the like.
michael@0 286 streamoff _Filebuf_base::_M_seek(streamoff offset, ios_base::seekdir dir)
michael@0 287 {
michael@0 288 int whence;
michael@0 289
michael@0 290 switch ( dir ) {
michael@0 291 case ios_base::beg:
michael@0 292 if (offset < 0 /* || offset > _M_file_size() */ )
michael@0 293 return streamoff(-1);
michael@0 294 whence = SEEK_SET;
michael@0 295 break;
michael@0 296 case ios_base::cur:
michael@0 297 whence = SEEK_CUR;
michael@0 298 break;
michael@0 299 case ios_base::end:
michael@0 300 if (/* offset > 0 || */ -offset > _M_file_size() )
michael@0 301 return streamoff(-1);
michael@0 302 whence = SEEK_END;
michael@0 303 break;
michael@0 304 default:
michael@0 305 return streamoff(-1);
michael@0 306 }
michael@0 307
michael@0 308 return LSEEK(_M_file_id, offset, whence);
michael@0 309 }
michael@0 310
michael@0 311 // Attempts to memory-map len bytes of the current file, starting
michael@0 312 // at position offset. Precondition: offset is a multiple of the
michael@0 313 // page size. Postcondition: return value is a null pointer if the
michael@0 314 // memory mapping failed. Otherwise the return value is a pointer to
michael@0 315 // the memory-mapped file and the file position is set to offset.
michael@0 316 void* _Filebuf_base::_M_mmap(streamoff offset, streamoff len)
michael@0 317 {
michael@0 318 void* base;
michael@0 319 #if !defined (__DJGPP) && !defined (_CRAY)
michael@0 320 base = MMAP(0, len, PROT_READ, MAP_PRIVATE, _M_file_id, offset);
michael@0 321 if (base != (void*)MAP_FAILED) {
michael@0 322 if (LSEEK(_M_file_id, offset + len, SEEK_SET) < 0) {
michael@0 323 this->_M_unmap(base, len);
michael@0 324 base = 0;
michael@0 325 }
michael@0 326 } else
michael@0 327 base =0;
michael@0 328 #else
michael@0 329 _STLP_MARK_PARAMETER_AS_UNUSED(&offset)
michael@0 330 _STLP_MARK_PARAMETER_AS_UNUSED(&len)
michael@0 331 base = 0;
michael@0 332 #endif
michael@0 333 return base;
michael@0 334 }
michael@0 335
michael@0 336 void _Filebuf_base::_M_unmap(void* base, streamoff len)
michael@0 337 {
michael@0 338 // precondition : there is a valid mapping at the moment
michael@0 339 #if !defined (__DJGPP) && !defined (_CRAY)
michael@0 340 munmap((char*)base, len);
michael@0 341 #else
michael@0 342 _STLP_MARK_PARAMETER_AS_UNUSED(&len)
michael@0 343 _STLP_MARK_PARAMETER_AS_UNUSED(base)
michael@0 344 #endif
michael@0 345 }
michael@0 346
michael@0 347 _STLP_END_NAMESPACE

mercurial