1 /* 2 * Copyright 2009, The Android Open Source Project 3 * Copyright (C) 2007 Holger Hans Peter Freyther 4 * All rights reserved. 5 * 6 * Redistribution and use in source and binary forms, with or without 7 * modification, are permitted provided that the following conditions 8 * are met: 9 * 1. Redistributions of source code must retain the above copyright 10 * notice, this list of conditions and the following disclaimer. 11 * 2. Redistributions in binary form must reproduce the above copyright 12 * notice, this list of conditions and the following disclaimer in the 13 * documentation and/or other materials provided with the distribution. 14 * 15 * THIS SOFTWARE IS PROVIDED BY APPLE COMPUTER, INC. ``AS IS'' AND ANY 16 * EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 17 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR 18 * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL APPLE COMPUTER, INC. OR 19 * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, 20 * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, 21 * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR 22 * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY 23 * OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT 24 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE 25 * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 26 */ 27 28 #include "config.h" 29 #include "FileSystem.h" 30 31 #include "CString.h" 32 #include "StringBuilder.h" 33 #include "cutils/log.h" 34 #include <dirent.h> 35 #include <dlfcn.h> 36 #include <errno.h> 37 #include <fnmatch.h> 38 #include <sys/stat.h> 39 40 namespace WebCore { 41 42 // Global static used to store the base to the plugin path. 43 // This is set in WebSettings.cpp 44 String sPluginPath; 45 46 CString fileSystemRepresentation(const String& path) 47 { 48 return path.utf8(); 49 } 50 51 CString openTemporaryFile(const char* prefix, PlatformFileHandle& handle) 52 { 53 int number = rand() % 10000 + 1; 54 CString filename; 55 do { 56 StringBuilder builder; 57 builder.append(sPluginPath); 58 builder.append('/'); 59 builder.append(prefix); 60 builder.append(String::number(number)); 61 filename = builder.toString().utf8(); 62 const char* fstr = filename.data(); 63 handle = open(filename.data(), O_WRONLY | O_CREAT | O_EXCL, S_IRUSR | S_IWUSR); 64 number++; 65 66 if (handle != -1) 67 return filename; 68 } while (errno == EEXIST); 69 70 return CString(); 71 } 72 73 bool unloadModule(PlatformModule module) 74 { 75 return !dlclose(module); 76 } 77 78 void closeFile(PlatformFileHandle& handle) 79 { 80 if (isHandleValid(handle)) { 81 close(handle); 82 handle = invalidPlatformFileHandle; 83 } 84 } 85 86 int writeToFile(PlatformFileHandle handle, const char* data, int length) 87 { 88 int totalBytesWritten = 0; 89 while (totalBytesWritten < length) { 90 int bytesWritten = write(handle, data, (size_t)(length - totalBytesWritten)); 91 if (bytesWritten < 0 && errno != EINTR) 92 return -1; 93 if (bytesWritten > 0) 94 totalBytesWritten += bytesWritten; 95 } 96 97 return totalBytesWritten; 98 } 99 100 String homeDirectoryPath() 101 { 102 return sPluginPath; 103 } 104 105 Vector<String> listDirectory(const String& path, const String& filter) 106 { 107 Vector<String> entries; 108 CString cpath = path.utf8(); 109 CString cfilter = filter.utf8(); 110 DIR* dir = opendir(cpath.data()); 111 if (dir) { 112 struct dirent* dp; 113 while (dp = readdir(dir)) { 114 const char* name = dp->d_name; 115 if (!strcmp(name, ".") || !strcmp(name, "..")) 116 continue; 117 if (fnmatch(cfilter.data(), name, 0)) 118 continue; 119 char filePath[1024]; 120 if ((int) (sizeof(filePath) - 1) < snprintf(filePath, 121 sizeof(filePath), "%s/%s", cpath.data(), name)) { 122 continue; // buffer overflow 123 } 124 entries.append(filePath); 125 } 126 closedir(dir); 127 } 128 return entries; 129 } 130 131 } // namespace WebCore 132