29 |
#include <sys/stat.h> |
#include <sys/stat.h> |
30 |
#include <sys/types.h> |
#include <sys/types.h> |
31 |
|
|
32 |
|
#include "encoding.h" |
33 |
#include "global.h" |
#include "global.h" |
34 |
#include "instrument-js.h" |
#include "instrument-js.h" |
35 |
#include "resource-manager.h" |
#include "resource-manager.h" |
91 |
break; |
break; |
92 |
case FILE_TYPE_JS: |
case FILE_TYPE_JS: |
93 |
{ |
{ |
94 |
FILE * input = xfopen(source_file, "r"); |
FILE * input = xfopen(source_file, "rb"); |
95 |
FILE * output = xfopen(destination_file, "w"); |
FILE * output = xfopen(destination_file, "wb"); |
96 |
|
|
97 |
Stream * input_stream = Stream_new(0); |
Stream * input_stream = Stream_new(0); |
98 |
Stream * output_stream = Stream_new(0); |
Stream * output_stream = Stream_new(0); |
99 |
|
|
100 |
Stream_write_file_contents(input_stream, input); |
Stream_write_file_contents(input_stream, input); |
101 |
|
|
102 |
jscoverage_instrument_js(id, jscoverage_encoding, input_stream, output_stream); |
size_t num_characters = input_stream->length; |
103 |
|
uint16_t * characters = NULL; |
104 |
|
int result = jscoverage_bytes_to_characters(jscoverage_encoding, input_stream->data, input_stream->length, &characters, &num_characters); |
105 |
|
if (result == JSCOVERAGE_ERROR_ENCODING_NOT_SUPPORTED) { |
106 |
|
fatal("encoding %s not supported", jscoverage_encoding); |
107 |
|
} |
108 |
|
else if (result == JSCOVERAGE_ERROR_INVALID_BYTE_SEQUENCE) { |
109 |
|
fatal("error decoding %s in file %s", jscoverage_encoding, id); |
110 |
|
} |
111 |
|
jscoverage_instrument_js(id, characters, num_characters, output_stream); |
112 |
|
free(characters); |
113 |
|
|
114 |
if (fwrite(output_stream->data, 1, output_stream->length, output) != output_stream->length) { |
if (fwrite(output_stream->data, 1, output_stream->length, output) != output_stream->length) { |
115 |
fatal("cannot write to file: %s", destination_file); |
fatal("cannot write to file: %s", destination_file); |
162 |
fatal("not a directory: %s", destination); |
fatal("not a directory: %s", destination); |
163 |
} |
} |
164 |
if (! directory_is_empty(destination)) { |
if (! directory_is_empty(destination)) { |
165 |
char * jscoverage_html = make_path(destination, "jscoverage.html"); |
char * expected_file = NULL; |
166 |
if (stat(jscoverage_html, &buf) == -1) { |
if (jscoverage_mozilla) { |
167 |
|
char * modules_directory = make_path(destination, "modules"); |
168 |
|
expected_file = make_path(modules_directory, "jscoverage.jsm"); |
169 |
|
free(modules_directory); |
170 |
|
} |
171 |
|
else { |
172 |
|
expected_file = make_path(destination, "jscoverage.html"); |
173 |
|
} |
174 |
|
if (stat(expected_file, &buf) == -1) { |
175 |
fatal("refusing to overwrite directory: %s", destination); |
fatal("refusing to overwrite directory: %s", destination); |
176 |
} |
} |
177 |
free(jscoverage_html); |
free(expected_file); |
178 |
} |
} |
179 |
} |
} |
180 |
else if (errno == ENOENT) { |
else if (errno == ENOENT) { |
184 |
fatal("cannot stat directory: %s", destination); |
fatal("cannot stat directory: %s", destination); |
185 |
} |
} |
186 |
|
|
|
/* copy the resources */ |
|
|
jscoverage_copy_resources(destination); |
|
|
|
|
187 |
/* finally: copy the directory */ |
/* finally: copy the directory */ |
188 |
struct DirListEntry * list = make_recursive_dir_list(source); |
struct DirListEntry * list = make_recursive_dir_list(source); |
189 |
for (struct DirListEntry * p = list; p != NULL; p = p->next) { |
for (struct DirListEntry * p = list; p != NULL; p = p->next) { |
221 |
free(s); |
free(s); |
222 |
free(d); |
free(d); |
223 |
} |
} |
224 |
|
|
225 |
|
/* copy the resources */ |
226 |
|
if (jscoverage_mozilla) { |
227 |
|
char * chrome_directory = make_path(destination, "chrome"); |
228 |
|
char * jscoverage_chrome_directory = make_path(chrome_directory, "jscoverage"); |
229 |
|
mkdir_if_necessary(jscoverage_chrome_directory); |
230 |
|
copy_resource("jscoverage.manifest", chrome_directory); |
231 |
|
copy_resource("jscoverage.html", jscoverage_chrome_directory); |
232 |
|
copy_resource("jscoverage.css", jscoverage_chrome_directory); |
233 |
|
copy_resource("jscoverage.js", jscoverage_chrome_directory); |
234 |
|
copy_resource("jscoverage-throbber.gif", jscoverage_chrome_directory); |
235 |
|
copy_resource("jscoverage-highlight.css", jscoverage_chrome_directory); |
236 |
|
copy_resource("jscoverage.xul", jscoverage_chrome_directory); |
237 |
|
copy_resource("jscoverage-overlay.js", jscoverage_chrome_directory); |
238 |
|
free(jscoverage_chrome_directory); |
239 |
|
free(chrome_directory); |
240 |
|
|
241 |
|
char * modules_directory = make_path(destination, "modules"); |
242 |
|
copy_resource("jscoverage.jsm", modules_directory); |
243 |
|
free(modules_directory); |
244 |
|
} |
245 |
|
else { |
246 |
|
jscoverage_copy_resources(destination); |
247 |
|
} |
248 |
|
|
249 |
free_dir_list(list); |
free_dir_list(list); |
250 |
} |
} |