gfx/skia/trunk/src/ports/SkOSFile_stdio.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 2006 The Android Open Source Project
michael@0 3 *
michael@0 4 * Use of this source code is governed by a BSD-style license that can be
michael@0 5 * found in the LICENSE file.
michael@0 6 */
michael@0 7
michael@0 8 #include "SkOSFile.h"
michael@0 9
michael@0 10 #include <errno.h>
michael@0 11 #include <stdio.h>
michael@0 12 #include <sys/stat.h>
michael@0 13 #include <sys/types.h>
michael@0 14
michael@0 15 #ifdef _WIN32
michael@0 16 #include <direct.h>
michael@0 17 #include <io.h>
michael@0 18 #else
michael@0 19 #include <unistd.h>
michael@0 20 #endif
michael@0 21
michael@0 22 SkFILE* sk_fopen(const char path[], SkFILE_Flags flags) {
michael@0 23 char perm[4];
michael@0 24 char* p = perm;
michael@0 25
michael@0 26 if (flags & kRead_SkFILE_Flag) {
michael@0 27 *p++ = 'r';
michael@0 28 }
michael@0 29 if (flags & kWrite_SkFILE_Flag) {
michael@0 30 *p++ = 'w';
michael@0 31 }
michael@0 32 *p++ = 'b';
michael@0 33 *p = 0;
michael@0 34
michael@0 35 //TODO: on Windows fopen is just ASCII or the current code page,
michael@0 36 //convert to utf16 and use _wfopen
michael@0 37 return (SkFILE*)::fopen(path, perm);
michael@0 38 }
michael@0 39
michael@0 40 char* sk_fgets(char* str, int size, SkFILE* f) {
michael@0 41 return ::fgets(str, size, (FILE *)f);
michael@0 42 }
michael@0 43
michael@0 44 int sk_feof(SkFILE *f) {
michael@0 45 // no :: namespace qualifier because it breaks android
michael@0 46 return feof((FILE *)f);
michael@0 47 }
michael@0 48
michael@0 49 size_t sk_fgetsize(SkFILE* f) {
michael@0 50 SkASSERT(f);
michael@0 51
michael@0 52 long curr = ::ftell((FILE*)f); // remember where we are
michael@0 53 if (curr < 0) {
michael@0 54 return 0;
michael@0 55 }
michael@0 56
michael@0 57 ::fseek((FILE*)f, 0, SEEK_END); // go to the end
michael@0 58 long size = ::ftell((FILE*)f); // record the size
michael@0 59 if (size < 0) {
michael@0 60 size = 0;
michael@0 61 }
michael@0 62
michael@0 63 ::fseek((FILE*)f, curr, SEEK_SET); // go back to our prev location
michael@0 64 return size;
michael@0 65 }
michael@0 66
michael@0 67 bool sk_frewind(SkFILE* f) {
michael@0 68 SkASSERT(f);
michael@0 69 ::rewind((FILE*)f);
michael@0 70 return true;
michael@0 71 }
michael@0 72
michael@0 73 size_t sk_fread(void* buffer, size_t byteCount, SkFILE* f) {
michael@0 74 SkASSERT(f);
michael@0 75 if (buffer == NULL) {
michael@0 76 size_t curr = ::ftell((FILE*)f);
michael@0 77 if ((long)curr == -1) {
michael@0 78 SkDEBUGF(("sk_fread: ftell(%p) returned -1 feof:%d ferror:%d\n", f, feof((FILE*)f), ferror((FILE*)f)));
michael@0 79 return 0;
michael@0 80 }
michael@0 81 int err = ::fseek((FILE*)f, (long)byteCount, SEEK_CUR);
michael@0 82 if (err != 0) {
michael@0 83 SkDEBUGF(("sk_fread: fseek(%d) tell:%d failed with feof:%d ferror:%d returned:%d\n",
michael@0 84 byteCount, curr, feof((FILE*)f), ferror((FILE*)f), err));
michael@0 85 return 0;
michael@0 86 }
michael@0 87 return byteCount;
michael@0 88 }
michael@0 89 else
michael@0 90 return ::fread(buffer, 1, byteCount, (FILE*)f);
michael@0 91 }
michael@0 92
michael@0 93 size_t sk_fwrite(const void* buffer, size_t byteCount, SkFILE* f) {
michael@0 94 SkASSERT(f);
michael@0 95 return ::fwrite(buffer, 1, byteCount, (FILE*)f);
michael@0 96 }
michael@0 97
michael@0 98 void sk_fflush(SkFILE* f) {
michael@0 99 SkASSERT(f);
michael@0 100 ::fflush((FILE*)f);
michael@0 101 }
michael@0 102
michael@0 103 bool sk_fseek(SkFILE* f, size_t byteCount) {
michael@0 104 int err = ::fseek((FILE*)f, (long)byteCount, SEEK_SET);
michael@0 105 return err == 0;
michael@0 106 }
michael@0 107
michael@0 108 bool sk_fmove(SkFILE* f, long byteCount) {
michael@0 109 int err = ::fseek((FILE*)f, byteCount, SEEK_CUR);
michael@0 110 return err == 0;
michael@0 111 }
michael@0 112
michael@0 113 size_t sk_ftell(SkFILE* f) {
michael@0 114 long curr = ::ftell((FILE*)f);
michael@0 115 if (curr < 0) {
michael@0 116 return 0;
michael@0 117 }
michael@0 118 return curr;
michael@0 119 }
michael@0 120
michael@0 121 void sk_fclose(SkFILE* f) {
michael@0 122 SkASSERT(f);
michael@0 123 ::fclose((FILE*)f);
michael@0 124 }
michael@0 125
michael@0 126 bool sk_exists(const char *path) {
michael@0 127 #ifdef _WIN32
michael@0 128 return (0 == _access(path, 0));
michael@0 129 #else
michael@0 130 return (0 == access(path, 0));
michael@0 131 #endif
michael@0 132 }
michael@0 133
michael@0 134 bool sk_isdir(const char *path) {
michael@0 135 struct stat status;
michael@0 136 if (0 != stat(path, &status)) {
michael@0 137 return false;
michael@0 138 }
michael@0 139 return SkToBool(status.st_mode & S_IFDIR);
michael@0 140 }
michael@0 141
michael@0 142 bool sk_mkdir(const char* path) {
michael@0 143 if (sk_isdir(path)) {
michael@0 144 return true;
michael@0 145 }
michael@0 146 if (sk_exists(path)) {
michael@0 147 fprintf(stderr,
michael@0 148 "sk_mkdir: path '%s' already exists but is not a directory\n",
michael@0 149 path);
michael@0 150 return false;
michael@0 151 }
michael@0 152
michael@0 153 int retval;
michael@0 154 #ifdef _WIN32
michael@0 155 retval = _mkdir(path);
michael@0 156 #else
michael@0 157 retval = mkdir(path, 0777);
michael@0 158 #endif
michael@0 159 if (0 == retval) {
michael@0 160 return true;
michael@0 161 } else {
michael@0 162 fprintf(stderr, "sk_mkdir: error %d creating dir '%s'\n", errno, path);
michael@0 163 return false;
michael@0 164 }
michael@0 165 }

mercurial