diff options
author | binji@chromium.org <binji@chromium.org@0039d316-1c4b-4281-b951-d872f2087c98> | 2013-05-03 18:59:27 +0000 |
---|---|---|
committer | binji@chromium.org <binji@chromium.org@0039d316-1c4b-4281-b951-d872f2087c98> | 2013-05-03 18:59:27 +0000 |
commit | 6b80f2b78e073065fd096344826824a1fec41bef (patch) | |
tree | 22c37b98a55f9ed6dd43458b0500246708917045 /native_client_sdk | |
parent | 56b575f63b38f6b2594156566d31427ca94154c3 (diff) | |
download | chromium_src-6b80f2b78e073065fd096344826824a1fec41bef.zip chromium_src-6b80f2b78e073065fd096344826824a1fec41bef.tar.gz chromium_src-6b80f2b78e073065fd096344826824a1fec41bef.tar.bz2 |
[NaCl SDK] Fix stat bug, also add "stat" command to nacl_io example.
BUG=236606
R=noelallen@chromium.org
Review URL: https://codereview.chromium.org/14790014
git-svn-id: svn://svn.chromium.org/chrome/trunk/src@198154 0039d316-1c4b-4281-b951-d872f2087c98
Diffstat (limited to 'native_client_sdk')
6 files changed, 75 insertions, 11 deletions
diff --git a/native_client_sdk/src/examples/demo/nacl_io/example.js b/native_client_sdk/src/examples/demo/nacl_io/example.js index e764c21..5221e5d 100644 --- a/native_client_sdk/src/examples/demo/nacl_io/example.js +++ b/native_client_sdk/src/examples/demo/nacl_io/example.js @@ -30,6 +30,7 @@ function attachListeners() { document.getElementById('freadExecute').addEventListener('click', fread); document.getElementById('fwriteExecute').addEventListener('click', fwrite); document.getElementById('fseekExecute').addEventListener('click', fseek); + document.getElementById('statExecute').addEventListener('click', stat); } function onRadioClicked(e) { @@ -74,7 +75,7 @@ function fopen(e) { nacl_module.postMessage(makeCall('fopen', filename, access)); } -function fopen_result(filename, filehandle) { +function fopenResult(filename, filehandle) { filehandle_map[filehandle] = filename; addFilenameToSelectElements(filehandle, filename) @@ -86,7 +87,7 @@ function fclose(e) { nacl_module.postMessage(makeCall('fclose', filehandle)); } -function fclose_result(filehandle) { +function fcloseResult(filehandle) { var filename = filehandle_map[filehandle]; removeFilenameFromSelectElements(filehandle, filename); common.logMessage('File ' + filename + ' closed successfully.\n'); @@ -98,7 +99,7 @@ function fread(e) { nacl_module.postMessage(makeCall('fread', filehandle, numBytes)); } -function fread_result(filehandle, data) { +function freadResult(filehandle, data) { var filename = filehandle_map[filehandle]; common.logMessage('Read "' + data + '" from file ' + filename + '.\n'); } @@ -109,7 +110,7 @@ function fwrite(e) { nacl_module.postMessage(makeCall('fwrite', filehandle, data)); } -function fwrite_result(filehandle, bytes_written) { +function fwriteResult(filehandle, bytes_written) { var filename = filehandle_map[filehandle]; common.logMessage('Wrote ' + bytes_written + ' bytes to file ' + filename + '.\n'); @@ -122,12 +123,21 @@ function fseek(e) { nacl_module.postMessage(makeCall('fseek', filehandle, offset, whence)); } -function fseek_result(filehandle, filepos) { +function fseekResult(filehandle, filepos) { var filename = filehandle_map[filehandle]; common.logMessage('Seeked to location ' + filepos + ' in file ' + filename + '.\n'); } +function stat(e) { + var filename = document.getElementById('statFilename').value; + nacl_module.postMessage(makeCall('stat', filename)); +} + +function statResult(filename, size) { + common.logMessage('File ' + filename + ' has size ' + size + '.\n'); +} + /** * Return true when |s| starts with the string |prefix|. * @@ -157,15 +167,15 @@ function handleMessage(message_event) { } else { // Result from a function call. var params = msg.split('\1'); - var func_name = params[0]; - var func_result_name = func_name + '_result'; - var result_func = window[func_result_name]; + var funcName = params[0]; + var funcResultName = funcName + 'Result'; + var resultFunc = window[funcResultName]; - if (!result_func) { + if (!resultFunc) { common.logMessage('Error: Bad message received from NaCl module.\n'); return; } - result_func.apply(null, params.slice(1)); + resultFunc.apply(null, params.slice(1)); } } diff --git a/native_client_sdk/src/examples/demo/nacl_io/handlers.c b/native_client_sdk/src/examples/demo/nacl_io/handlers.c index 062a41f..5aed7a7 100644 --- a/native_client_sdk/src/examples/demo/nacl_io/handlers.c +++ b/native_client_sdk/src/examples/demo/nacl_io/handlers.c @@ -6,9 +6,11 @@ #include "handlers.h" #include <assert.h> +#include <errno.h> #include <stdio.h> #include <stdlib.h> #include <string.h> +#include <sys/stat.h> #include "hello_nacl_io.h" @@ -320,3 +322,43 @@ int HandleFclose(int num_params, char** params, char** output) { *output = PrintfToNewString("fclose\1%s", file_index_string); return 0; } + +/** + * Handle a call to stat() made by JavaScript. + * + * stat expects 1 parameter: + * 0: The name of the file + * on success, stat returns a result in |output| separated by \1: + * 0: "stat" + * 1: the file name + * 2: the size of the file + * on failure, stat returns an error string in |output|. + * + * @param[in] num_params The number of params in |params|. + * @param[in] params An array of strings, parameters to this function. + * @param[out] output A string to write informational function output to. + * @return An errorcode; 0 means success, anything else is a failure. */ +int HandleStat(int num_params, char** params, char** output) { + FILE* file; + int file_index; + const char* filename; + const char* mode; + + if (num_params != 1) { + *output = PrintfToNewString("Error: stat takes 1 parameter."); + return 1; + } + + filename = params[0]; + + struct stat buf; + memset(&buf, 0, sizeof(buf)); + int result = stat(filename, &buf); + if (result == -1) { + *output = PrintfToNewString("Error: stat returned error %d.", errno); + return 2; + } + + *output = PrintfToNewString("stat\1%s\1%d", filename, buf.st_size); + return 0; +} diff --git a/native_client_sdk/src/examples/demo/nacl_io/handlers.h b/native_client_sdk/src/examples/demo/nacl_io/handlers.h index 9510dc7..c059a43 100644 --- a/native_client_sdk/src/examples/demo/nacl_io/handlers.h +++ b/native_client_sdk/src/examples/demo/nacl_io/handlers.h @@ -15,5 +15,6 @@ int HandleFwrite(int num_params, char** params, char** output); int HandleFread(int num_params, char** params, char** output); int HandleFseek(int num_params, char** params, char** output); int HandleFclose(int num_params, char** params, char** output); +int HandleStat(int num_params, char** params, char** output); #endif /* HANDLERS_H_ */ diff --git a/native_client_sdk/src/examples/demo/nacl_io/hello_nacl_io.c b/native_client_sdk/src/examples/demo/nacl_io/hello_nacl_io.c index f187e3c..b56e1c6 100644 --- a/native_client_sdk/src/examples/demo/nacl_io/hello_nacl_io.c +++ b/native_client_sdk/src/examples/demo/nacl_io/hello_nacl_io.c @@ -47,6 +47,7 @@ static FuncNameMapping g_function_map[] = { { "fread", HandleFread }, { "fseek", HandleFseek }, { "fclose", HandleFclose }, + { "stat", HandleStat }, { NULL, NULL }, }; diff --git a/native_client_sdk/src/examples/demo/nacl_io/index.html b/native_client_sdk/src/examples/demo/nacl_io/index.html index aafa1cb..5871b46 100644 --- a/native_client_sdk/src/examples/demo/nacl_io/index.html +++ b/native_client_sdk/src/examples/demo/nacl_io/index.html @@ -44,6 +44,7 @@ found in the LICENSE file. <input type="radio" id="radiofread" name="group">fread <input type="radio" id="radiofwrite" name="group">fwrite <input type="radio" id="radiofseek" name="group">fseek + <input type="radio" id="radiostat" name="group">stat </span> </div> <div class="function" id="fopen"> @@ -97,6 +98,13 @@ found in the LICENSE file. <button id="fseekExecute">fseek</button> </span> </div> + <div class="function" id="stat" hidden> + <span> + Filename: + <input type="text" id="statFilename"> + <button id="statExecute">stat</button> + </span> + </div> <pre id="log" style="font-weight: bold"></pre> <!-- The NaCl plugin will be embedded inside the element with id "listener". See common.js.--> diff --git a/native_client_sdk/src/libraries/nacl_io/mount_html5fs.cc b/native_client_sdk/src/libraries/nacl_io/mount_html5fs.cc index 3fd66e0..9b995359 100644 --- a/native_client_sdk/src/libraries/nacl_io/mount_html5fs.cc +++ b/native_client_sdk/src/libraries/nacl_io/mount_html5fs.cc @@ -25,8 +25,10 @@ int64_t strtoull(const char* nptr, char** endptr, int base) { } // namespace MountNode *MountHtml5Fs::Open(const Path& path, int mode) { - if (BlockUntilFilesystemOpen() != PP_OK) + if (BlockUntilFilesystemOpen() != PP_OK) { + errno = ENODEV; return NULL; + } PP_Resource fileref = ppapi()->GetFileRefInterface()->Create( filesystem_resource_, path.Join().c_str()); |