| OLD | NEW |
| (Empty) |
| 1 /* | |
| 2 * This file is part of Adblock Plus <http://adblockplus.org/>, | |
| 3 * Copyright (C) 2006-2013 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 "debug.h" | |
| 23 #include "ops.h" | |
| 24 | |
| 25 v8::Handle<v8::Value> fileExistsImpl(const v8::Arguments& args) | |
| 26 { | |
| 27 D(D_WARN, "fileExists()"); | |
| 28 v8::HandleScope handle_scope; | |
| 29 | |
| 30 if (args.Length() < 1) | |
| 31 { | |
| 32 return v8::ThrowException(v8::String::New("File name expected")); | |
| 33 } | |
| 34 v8::String::AsciiValue fileName(args[0]); | |
| 35 if (!*fileName) | |
| 36 { | |
| 37 return v8::ThrowException(v8::String::New("File name isn't a string")); | |
| 38 } | |
| 39 D(D_INFO, "fileExists(%s)", *fileName); | |
| 40 | |
| 41 struct stat buf; | |
| 42 int result = stat(*fileName, &buf); | |
| 43 | |
| 44 return v8::Boolean::New(result == 0 && S_ISREG(buf.st_mode)); | |
| 45 } | |
| 46 | |
| 47 v8::Handle<v8::Value> fileLastModifiedImpl(const v8::Arguments& args) | |
| 48 { | |
| 49 D(D_WARN, "fileLastModified()"); | |
| 50 v8::HandleScope handle_scope; | |
| 51 | |
| 52 if (args.Length() < 1) | |
| 53 { | |
| 54 return v8::ThrowException(v8::String::New("File name expected")); | |
| 55 } | |
| 56 v8::String::AsciiValue fileName(args[0]); | |
| 57 if (!*fileName) | |
| 58 { | |
| 59 return v8::ThrowException(v8::String::New("File name isn't a string")); | |
| 60 } | |
| 61 D(D_INFO, "fileLastModified(%s)", *fileName); | |
| 62 | |
| 63 struct stat buf; | |
| 64 int result = stat(*fileName, &buf); | |
| 65 | |
| 66 return v8::Number::New((double)buf.st_mtime * 1000); | |
| 67 } | |
| 68 | |
| 69 v8::Handle<v8::Value> fileRemoveImpl(const v8::Arguments& args) | |
| 70 { | |
| 71 D(D_WARN, "fileRemove()"); | |
| 72 v8::HandleScope handle_scope; | |
| 73 | |
| 74 if (args.Length() < 1) | |
| 75 { | |
| 76 return v8::ThrowException(v8::String::New("File name expected")); | |
| 77 } | |
| 78 v8::String::AsciiValue fileName(args[0]); | |
| 79 if (!*fileName) | |
| 80 { | |
| 81 return v8::ThrowException(v8::String::New("File name isn't a string")); | |
| 82 } | |
| 83 D(D_INFO, "fileRemove(%s)", *fileName); | |
| 84 | |
| 85 int result = unlink(*fileName); | |
| 86 | |
| 87 if (result == 0) | |
| 88 return v8::Undefined(); | |
| 89 else | |
| 90 return v8::ThrowException(v8::String::New("File couldn't be removed")); | |
| 91 } | |
| 92 | |
| 93 v8::Handle<v8::Value> fileRenameImpl(const v8::Arguments& args) | |
| 94 { | |
| 95 D(D_WARN, "fileRename()"); | |
| 96 v8::HandleScope handle_scope; | |
| 97 | |
| 98 if (args.Length() < 2) | |
| 99 { | |
| 100 return v8::ThrowException(v8::String::New("File names expected")); | |
| 101 } | |
| 102 v8::String::AsciiValue fileName(args[0]); | |
| 103 if (!*fileName) | |
| 104 { | |
| 105 return v8::ThrowException(v8::String::New("File name isn't a string")); | |
| 106 } | |
| 107 | |
| 108 v8::String::AsciiValue newPath(args[1]); | |
| 109 if (!*newPath) | |
| 110 { | |
| 111 return v8::ThrowException(v8::String::New("File name isn't a string")); | |
| 112 } | |
| 113 D(D_INFO, "fileRename(%s, %s)", *fileName, *newPath); | |
| 114 | |
| 115 int result = rename(*fileName, *newPath); | |
| 116 | |
| 117 if (result == 0) | |
| 118 return v8::Undefined(); | |
| 119 else | |
| 120 return v8::ThrowException(v8::String::New("File couldn't be renamed")); | |
| 121 } | |
| 122 | |
| 123 v8::Handle<v8::Value> fileReadImpl(const v8::Arguments& args) | |
| 124 { | |
| 125 D(D_WARN, "fileRead()"); | |
| 126 v8::HandleScope handle_scope; | |
| 127 | |
| 128 if (args.Length() < 1) | |
| 129 { | |
| 130 return v8::ThrowException(v8::String::New("File name expected")); | |
| 131 } | |
| 132 v8::String::AsciiValue fileName(args[0]); | |
| 133 if (!*fileName) | |
| 134 { | |
| 135 return v8::ThrowException(v8::String::New("File name isn't a string")); | |
| 136 } | |
| 137 D(D_INFO, "JS", "fileRead(%s)", *fileName); | |
| 138 | |
| 139 FILE* file = fopen(*fileName, "rb"); | |
| 140 if (!file) | |
| 141 { | |
| 142 v8::Handle<v8::String> fileNameString = v8::String::New(*fileName); | |
| 143 return v8::ThrowException(v8::String::Concat(v8::String::New("Failed opening
file: "), fileNameString)); | |
| 144 } | |
| 145 | |
| 146 fseek(file, 0, SEEK_END); | |
| 147 long size = ftell(file); | |
| 148 rewind(file); | |
| 149 | |
| 150 char* buffer = new char[size]; | |
| 151 if (!buffer) | |
| 152 { | |
| 153 fclose(file); | |
| 154 return v8::ThrowException(v8::String::New("Out of memory")); | |
| 155 } | |
| 156 size_t readSize = fread(buffer, 1, size, file); | |
| 157 fclose(file); | |
| 158 if (size != readSize) | |
| 159 { | |
| 160 delete buffer; | |
| 161 return v8::ThrowException(v8::String::New("File read error")); | |
| 162 } | |
| 163 | |
| 164 v8::Handle<v8::String> data = v8::String::New(buffer, size); | |
| 165 delete buffer; | |
| 166 return data; | |
| 167 } | |
| 168 | |
| 169 v8::Handle<v8::Value> fileWriteImpl(const v8::Arguments& args) | |
| 170 { | |
| 171 D(D_WARN, "fileWrite()"); | |
| 172 v8::HandleScope handle_scope; | |
| 173 | |
| 174 if (args.Length() < 1) | |
| 175 { | |
| 176 return v8::ThrowException(v8::String::New("File name expected")); | |
| 177 } | |
| 178 v8::String::AsciiValue fileName(args[0]); | |
| 179 if (!*fileName) | |
| 180 { | |
| 181 return v8::ThrowException(v8::String::New("File name isn't a string")); | |
| 182 } | |
| 183 D(D_INFO, "fileWrite(%s)", *fileName); | |
| 184 | |
| 185 if (args.Length() < 2) | |
| 186 { | |
| 187 return v8::ThrowException(v8::String::New("Data to write expected")); | |
| 188 } | |
| 189 v8::String::Utf8Value data(args[1]); | |
| 190 if (!*data) | |
| 191 { | |
| 192 return v8::ThrowException(v8::String::New("Data to write is not a string")); | |
| 193 } | |
| 194 | |
| 195 FILE* file = fopen(*fileName, "wb"); | |
| 196 if (!file) | |
| 197 { | |
| 198 v8::Handle<v8::String> fileNameString = v8::String::New(*fileName); | |
| 199 return v8::ThrowException(v8::String::Concat(v8::String::New("Failed opening
file: "), fileNameString)); | |
| 200 } | |
| 201 | |
| 202 size_t writeSize = fwrite(*data, 1, data.length(), file); | |
| 203 fclose(file); | |
| 204 | |
| 205 if (data.length() != writeSize) | |
| 206 { | |
| 207 fileRemoveImpl(args); | |
| 208 return v8::ThrowException(v8::String::New("File write error")); | |
| 209 } | |
| 210 | |
| 211 return v8::Undefined(); | |
| 212 } | |
| OLD | NEW |