gfx/skia/trunk/src/utils/SkOSFile.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 2011 Google Inc.
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 #include "SkOSFile.h"
michael@0 8
michael@0 9 SkString SkOSPath::SkPathJoin(const char *rootPath, const char *relativePath) {
michael@0 10 SkString result(rootPath);
michael@0 11 if (!result.endsWith(SkPATH_SEPARATOR)) {
michael@0 12 result.appendUnichar(SkPATH_SEPARATOR);
michael@0 13 }
michael@0 14 result.append(relativePath);
michael@0 15 return result;
michael@0 16 }
michael@0 17
michael@0 18 SkString SkOSPath::SkBasename(const char* fullPath) {
michael@0 19 if (!fullPath) {
michael@0 20 return SkString();
michael@0 21 }
michael@0 22 const char* filename = strrchr(fullPath, SkPATH_SEPARATOR);
michael@0 23 if (NULL == filename) {
michael@0 24 filename = fullPath;
michael@0 25 } else {
michael@0 26 ++filename;
michael@0 27 }
michael@0 28 return SkString(filename);
michael@0 29 }
michael@0 30
michael@0 31 #ifdef SK_BUILD_FOR_WIN
michael@0 32
michael@0 33 static uint16_t* concat_to_16(const char src[], const char suffix[])
michael@0 34 {
michael@0 35 size_t i, len = strlen(src);
michael@0 36 size_t len2 = 3 + (suffix ? strlen(suffix) : 0);
michael@0 37 uint16_t* dst = (uint16_t*)sk_malloc_throw((len + len2) * sizeof(uint16_t));
michael@0 38
michael@0 39 for (i = 0; i < len; i++)
michael@0 40 dst[i] = src[i];
michael@0 41
michael@0 42 if (i > 0 && dst[i-1] != '/')
michael@0 43 dst[i++] = '/';
michael@0 44 dst[i++] = '*';
michael@0 45
michael@0 46 if (suffix)
michael@0 47 {
michael@0 48 while (*suffix)
michael@0 49 dst[i++] = *suffix++;
michael@0 50 }
michael@0 51 dst[i] = 0;
michael@0 52 SkASSERT(i + 1 <= len + len2);
michael@0 53
michael@0 54 return dst;
michael@0 55 }
michael@0 56
michael@0 57 SkUTF16_Str::SkUTF16_Str(const char src[])
michael@0 58 {
michael@0 59 size_t len = strlen(src);
michael@0 60
michael@0 61 fStr = (uint16_t*)sk_malloc_throw((len + 1) * sizeof(uint16_t));
michael@0 62 size_t i;
michael@0 63 for (i = 0; i < len; i++)
michael@0 64 fStr[i] = src[i];
michael@0 65 fStr[i] = 0;
michael@0 66 }
michael@0 67
michael@0 68 ////////////////////////////////////////////////////////////////////////////
michael@0 69
michael@0 70 SkOSFile::Iter::Iter() : fHandle(0), fPath16(NULL)
michael@0 71 {
michael@0 72 }
michael@0 73
michael@0 74 SkOSFile::Iter::Iter(const char path[], const char suffix[]) : fHandle(0), fPath16(NULL)
michael@0 75 {
michael@0 76 this->reset(path, suffix);
michael@0 77 }
michael@0 78
michael@0 79 SkOSFile::Iter::~Iter()
michael@0 80 {
michael@0 81 sk_free(fPath16);
michael@0 82 if (fHandle)
michael@0 83 ::FindClose(fHandle);
michael@0 84 }
michael@0 85
michael@0 86 void SkOSFile::Iter::reset(const char path[], const char suffix[])
michael@0 87 {
michael@0 88 if (fHandle)
michael@0 89 {
michael@0 90 ::FindClose(fHandle);
michael@0 91 fHandle = 0;
michael@0 92 }
michael@0 93 if (NULL == path)
michael@0 94 path = "";
michael@0 95
michael@0 96 sk_free(fPath16);
michael@0 97 fPath16 = concat_to_16(path, suffix);
michael@0 98 }
michael@0 99
michael@0 100 static bool is_magic_dir(const uint16_t dir[])
michael@0 101 {
michael@0 102 // return true for "." and ".."
michael@0 103 return dir[0] == '.' && (dir[1] == 0 || dir[1] == '.' && dir[2] == 0);
michael@0 104 }
michael@0 105
michael@0 106 static bool get_the_file(HANDLE handle, SkString* name, WIN32_FIND_DATAW* dataPtr, bool getDir)
michael@0 107 {
michael@0 108 WIN32_FIND_DATAW data;
michael@0 109
michael@0 110 if (NULL == dataPtr)
michael@0 111 {
michael@0 112 if (::FindNextFileW(handle, &data))
michael@0 113 dataPtr = &data;
michael@0 114 else
michael@0 115 return false;
michael@0 116 }
michael@0 117
michael@0 118 for (;;)
michael@0 119 {
michael@0 120 if (getDir)
michael@0 121 {
michael@0 122 if ((dataPtr->dwFileAttributes & FILE_ATTRIBUTE_DIRECTORY) && !is_magic_dir((uint16_t*)dataPtr->cFileName))
michael@0 123 break;
michael@0 124 }
michael@0 125 else
michael@0 126 {
michael@0 127 if (!(dataPtr->dwFileAttributes & FILE_ATTRIBUTE_DIRECTORY))
michael@0 128 break;
michael@0 129 }
michael@0 130 if (!::FindNextFileW(handle, dataPtr))
michael@0 131 return false;
michael@0 132 }
michael@0 133 // if we get here, we've found a file/dir
michael@0 134 if (name)
michael@0 135 name->setUTF16((uint16_t*)dataPtr->cFileName);
michael@0 136 return true;
michael@0 137 }
michael@0 138
michael@0 139 bool SkOSFile::Iter::next(SkString* name, bool getDir)
michael@0 140 {
michael@0 141 WIN32_FIND_DATAW data;
michael@0 142 WIN32_FIND_DATAW* dataPtr = NULL;
michael@0 143
michael@0 144 if (fHandle == 0) // our first time
michael@0 145 {
michael@0 146 if (fPath16 == NULL || *fPath16 == 0) // check for no path
michael@0 147 return false;
michael@0 148
michael@0 149 fHandle = ::FindFirstFileW((LPCWSTR)fPath16, &data);
michael@0 150 if (fHandle != 0 && fHandle != (HANDLE)~0)
michael@0 151 dataPtr = &data;
michael@0 152 }
michael@0 153 return fHandle != (HANDLE)~0 && get_the_file(fHandle, name, dataPtr, getDir);
michael@0 154 }
michael@0 155
michael@0 156 #elif defined(SK_BUILD_FOR_MAC) || defined(SK_BUILD_FOR_UNIX) || defined(SK_BUILD_FOR_ANDROID) || defined(SK_BUILD_FOR_IOS)
michael@0 157
michael@0 158 #if 0
michael@0 159 OSStatus FSPathMakeRef (
michael@0 160 const UInt8 * path,
michael@0 161 FSRef * ref,
michael@0 162 Boolean * isDirectory
michael@0 163 );
michael@0 164 #endif
michael@0 165
michael@0 166 SkOSFile::Iter::Iter() : fDIR(0)
michael@0 167 {
michael@0 168 }
michael@0 169
michael@0 170 SkOSFile::Iter::Iter(const char path[], const char suffix[]) : fDIR(0)
michael@0 171 {
michael@0 172 this->reset(path, suffix);
michael@0 173 }
michael@0 174
michael@0 175 SkOSFile::Iter::~Iter()
michael@0 176 {
michael@0 177 if (fDIR)
michael@0 178 ::closedir(fDIR);
michael@0 179 }
michael@0 180
michael@0 181 void SkOSFile::Iter::reset(const char path[], const char suffix[])
michael@0 182 {
michael@0 183 if (fDIR)
michael@0 184 {
michael@0 185 ::closedir(fDIR);
michael@0 186 fDIR = 0;
michael@0 187 }
michael@0 188
michael@0 189 fPath.set(path);
michael@0 190 if (path)
michael@0 191 {
michael@0 192 fDIR = ::opendir(path);
michael@0 193 fSuffix.set(suffix);
michael@0 194 }
michael@0 195 else
michael@0 196 fSuffix.reset();
michael@0 197 }
michael@0 198
michael@0 199 // returns true if suffix is empty, or if str ends with suffix
michael@0 200 static bool issuffixfor(const SkString& suffix, const char str[])
michael@0 201 {
michael@0 202 size_t suffixLen = suffix.size();
michael@0 203 size_t strLen = strlen(str);
michael@0 204
michael@0 205 return strLen >= suffixLen &&
michael@0 206 memcmp(suffix.c_str(), str + strLen - suffixLen, suffixLen) == 0;
michael@0 207 }
michael@0 208
michael@0 209 #include <sys/stat.h>
michael@0 210
michael@0 211 bool SkOSFile::Iter::next(SkString* name, bool getDir)
michael@0 212 {
michael@0 213 if (fDIR)
michael@0 214 {
michael@0 215 dirent* entry;
michael@0 216
michael@0 217 while ((entry = ::readdir(fDIR)) != NULL)
michael@0 218 {
michael@0 219 struct stat s;
michael@0 220 SkString str(fPath);
michael@0 221
michael@0 222 if (!str.endsWith("/") && !str.endsWith("\\"))
michael@0 223 str.append("/");
michael@0 224 str.append(entry->d_name);
michael@0 225
michael@0 226 if (0 == stat(str.c_str(), &s))
michael@0 227 {
michael@0 228 if (getDir)
michael@0 229 {
michael@0 230 if (s.st_mode & S_IFDIR)
michael@0 231 break;
michael@0 232 }
michael@0 233 else
michael@0 234 {
michael@0 235 if (!(s.st_mode & S_IFDIR) && issuffixfor(fSuffix, entry->d_name))
michael@0 236 break;
michael@0 237 }
michael@0 238 }
michael@0 239 }
michael@0 240 if (entry) // we broke out with a file
michael@0 241 {
michael@0 242 if (name)
michael@0 243 name->set(entry->d_name);
michael@0 244 return true;
michael@0 245 }
michael@0 246 }
michael@0 247 return false;
michael@0 248 }
michael@0 249 #endif // if one of:SK_BUILD_FOR_MAC, SK_BUILD_FOR_UNIX, SK_BUILD_FOR_ANDROID,SK_BUILD_FOR_IOS

mercurial