OLD | NEW |
(Empty) | |
| 1 /* |
| 2 * This file is part of the Adblock Plus, |
| 3 * Copyright (C) 2006-2012 Eyeo GmbH |
| 4 * |
| 5 * Adblock Plus is free software: you can redistribute it and/or modify |
| 6 * it under the terms of the GNU General Public License version 3 as |
| 7 * published by the Free Software Foundation. |
| 8 * |
| 9 * Adblock Plus is distributed in the hope that it will be useful, |
| 10 * but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 12 * GNU General Public License for more details. |
| 13 * |
| 14 * You should have received a copy of the GNU General Public License |
| 15 * along with Adblock Plus. If not, see <http://www.gnu.org/licenses/>. |
| 16 */ |
| 17 |
| 18 #include <stdio.h> |
| 19 #include <string.h> |
| 20 #include <unistd.h> |
| 21 #include <sys/stat.h> |
| 22 #include <android/log.h> |
| 23 #include "ops.h" |
| 24 |
| 25 v8::Handle<v8::Value> fileExistsImpl(const v8::Arguments& args) |
| 26 { |
| 27 v8::HandleScope handle_scope; |
| 28 |
| 29 if (args.Length() < 1) |
| 30 { |
| 31 return v8::ThrowException(v8::String::New("File name expected")); |
| 32 } |
| 33 v8::String::AsciiValue fileName(args[0]); |
| 34 if (!*fileName) |
| 35 { |
| 36 return v8::ThrowException(v8::String::New("File name isn't a string")); |
| 37 } |
| 38 __android_log_print(ANDROID_LOG_INFO, "JS", "fileExists(%s)", *fileName); |
| 39 |
| 40 struct stat buf; |
| 41 int result = stat(*fileName, &buf); |
| 42 |
| 43 return v8::Boolean::New(result == 0 && S_ISREG(buf.st_mode)); |
| 44 } |
| 45 |
| 46 v8::Handle<v8::Value> fileLastModifiedImpl(const v8::Arguments& args) |
| 47 { |
| 48 v8::HandleScope handle_scope; |
| 49 |
| 50 if (args.Length() < 1) |
| 51 { |
| 52 return v8::ThrowException(v8::String::New("File name expected")); |
| 53 } |
| 54 v8::String::AsciiValue fileName(args[0]); |
| 55 if (!*fileName) |
| 56 { |
| 57 return v8::ThrowException(v8::String::New("File name isn't a string")); |
| 58 } |
| 59 __android_log_print(ANDROID_LOG_INFO, "JS", "fileLastModified(%s)", *fileName)
; |
| 60 |
| 61 struct stat buf; |
| 62 int result = stat(*fileName, &buf); |
| 63 |
| 64 return v8::Number::New((double)buf.st_mtime * 1000); |
| 65 } |
| 66 |
| 67 v8::Handle<v8::Value> fileRemoveImpl(const v8::Arguments& args) |
| 68 { |
| 69 v8::HandleScope handle_scope; |
| 70 |
| 71 if (args.Length() < 1) |
| 72 { |
| 73 return v8::ThrowException(v8::String::New("File name expected")); |
| 74 } |
| 75 v8::String::AsciiValue fileName(args[0]); |
| 76 if (!*fileName) |
| 77 { |
| 78 return v8::ThrowException(v8::String::New("File name isn't a string")); |
| 79 } |
| 80 __android_log_print(ANDROID_LOG_INFO, "JS", "fileRemove(%s)", *fileName); |
| 81 |
| 82 int result = unlink(*fileName); |
| 83 |
| 84 if (result == 0) |
| 85 return v8::Undefined(); |
| 86 else |
| 87 return v8::ThrowException(v8::String::New("File couldn't be removed")); |
| 88 } |
| 89 |
| 90 v8::Handle<v8::Value> fileRenameImpl(const v8::Arguments& args) |
| 91 { |
| 92 v8::HandleScope handle_scope; |
| 93 |
| 94 if (args.Length() < 2) |
| 95 { |
| 96 return v8::ThrowException(v8::String::New("File names expected")); |
| 97 } |
| 98 v8::String::AsciiValue fileName(args[0]); |
| 99 if (!*fileName) |
| 100 { |
| 101 return v8::ThrowException(v8::String::New("File name isn't a string")); |
| 102 } |
| 103 |
| 104 v8::String::AsciiValue newPath(args[1]); |
| 105 if (!*newPath) |
| 106 { |
| 107 return v8::ThrowException(v8::String::New("File name isn't a string")); |
| 108 } |
| 109 __android_log_print(ANDROID_LOG_INFO, "JS", "fileRename(%s, %s)", *fileName, *
newPath); |
| 110 |
| 111 int result = rename(*fileName, *newPath); |
| 112 |
| 113 if (result == 0) |
| 114 return v8::Undefined(); |
| 115 else |
| 116 return v8::ThrowException(v8::String::New("File couldn't be renamed")); |
| 117 } |
| 118 |
| 119 v8::Handle<v8::Value> fileReadImpl(const v8::Arguments& args) |
| 120 { |
| 121 v8::HandleScope handle_scope; |
| 122 |
| 123 if (args.Length() < 1) |
| 124 { |
| 125 return v8::ThrowException(v8::String::New("File name expected")); |
| 126 } |
| 127 v8::String::AsciiValue fileName(args[0]); |
| 128 if (!*fileName) |
| 129 { |
| 130 return v8::ThrowException(v8::String::New("File name isn't a string")); |
| 131 } |
| 132 __android_log_print(ANDROID_LOG_INFO, "JS", "fileRead(%s)", *fileName); |
| 133 |
| 134 FILE* file = fopen(*fileName, "rb"); |
| 135 if (!file) |
| 136 { |
| 137 v8::Handle<v8::String> fileNameString = v8::String::New(*fileName); |
| 138 return v8::ThrowException(v8::String::Concat(v8::String::New("Failed opening
file: "), fileNameString)); |
| 139 } |
| 140 |
| 141 fseek(file, 0, SEEK_END); |
| 142 long size = ftell(file); |
| 143 rewind(file); |
| 144 |
| 145 char* buffer = new char[size]; |
| 146 if (!buffer) |
| 147 { |
| 148 fclose(file); |
| 149 return v8::ThrowException(v8::String::New("Out of memory")); |
| 150 } |
| 151 size_t readSize = fread(buffer, 1, size, file); |
| 152 fclose(file); |
| 153 if (size != readSize) |
| 154 { |
| 155 delete buffer; |
| 156 return v8::ThrowException(v8::String::New("File read error")); |
| 157 } |
| 158 |
| 159 v8::Handle<v8::String> data = v8::String::New(buffer, size); |
| 160 delete buffer; |
| 161 return data; |
| 162 } |
| 163 |
| 164 v8::Handle<v8::Value> fileWriteImpl(const v8::Arguments& args) |
| 165 { |
| 166 v8::HandleScope handle_scope; |
| 167 |
| 168 if (args.Length() < 1) |
| 169 { |
| 170 return v8::ThrowException(v8::String::New("File name expected")); |
| 171 } |
| 172 v8::String::AsciiValue fileName(args[0]); |
| 173 if (!*fileName) |
| 174 { |
| 175 return v8::ThrowException(v8::String::New("File name isn't a string")); |
| 176 } |
| 177 __android_log_print(ANDROID_LOG_INFO, "JS", "fileWrite(%s)", *fileName); |
| 178 |
| 179 if (args.Length() < 2) |
| 180 { |
| 181 return v8::ThrowException(v8::String::New("Data to write expected")); |
| 182 } |
| 183 v8::String::Utf8Value data(args[1]); |
| 184 if (!*data) |
| 185 { |
| 186 return v8::ThrowException(v8::String::New("Data to write is not a string")); |
| 187 } |
| 188 |
| 189 FILE* file = fopen(*fileName, "wb"); |
| 190 if (!file) |
| 191 { |
| 192 v8::Handle<v8::String> fileNameString = v8::String::New(*fileName); |
| 193 return v8::ThrowException(v8::String::Concat(v8::String::New("Failed opening
file: "), fileNameString)); |
| 194 } |
| 195 |
| 196 size_t writeSize = fwrite(*data, 1, data.length(), file); |
| 197 fclose(file); |
| 198 |
| 199 if (data.length() != writeSize) |
| 200 { |
| 201 fileRemoveImpl(args); |
| 202 return v8::ThrowException(v8::String::New("File write error")); |
| 203 } |
| 204 |
| 205 return v8::Undefined(); |
| 206 } |
OLD | NEW |