Rietveld Code Review Tool
Help | Bug tracker | Discussion group | Source code

Delta Between Two Patch Sets: jni/fileOps.cpp

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

Powered by Google App Engine
This is Rietveld