Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(94)

Unified Diff: ppapi/native_client/src/trusted/plugin/file_utils.cc

Issue 15697019: Parametrize names of llc and ld nexes by reading them from the resource info JSON file. (Closed) Base URL: svn://svn.chromium.org/chrome/trunk/src
Patch Set: Tweak comments some more, following Jan's review Created 7 years, 7 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View side-by-side diff with in-line comments
Download patch
Index: ppapi/native_client/src/trusted/plugin/file_utils.cc
diff --git a/ppapi/native_client/src/trusted/plugin/file_utils.cc b/ppapi/native_client/src/trusted/plugin/file_utils.cc
new file mode 100644
index 0000000000000000000000000000000000000000..50600a2a201e1e5c6f3a16c0d5a9c12b26afe743
--- /dev/null
+++ b/ppapi/native_client/src/trusted/plugin/file_utils.cc
@@ -0,0 +1,77 @@
+// Copyright (c) 2013 The Chromium Authors. All rights reserved.
+// Use of this source code is governed by a BSD-style license that can be
+// found in the LICENSE file.
+
+// Some common file utilities for plugin code.
+
+#include "native_client/src/trusted/plugin/file_utils.h"
+
+#include <fcntl.h>
+#include <stdio.h>
+#include <stdlib.h>
+
+#include <sys/stat.h>
+#include <sys/types.h>
+
+#include "native_client/src/include/nacl_scoped_ptr.h"
+#include "native_client/src/include/portability_io.h"
+#include "native_client/src/include/portability_string.h"
+
+
+namespace plugin {
+namespace file_utils {
+
+StatusCode SlurpFile(int32_t fd,
+ nacl::string& out_buf,
+ size_t max_size_to_read) {
+ struct stat stat_buf;
+ if (fstat(fd, &stat_buf) != 0) {
+ CLOSE(fd);
+ return PLUGIN_FILE_ERROR_STAT;
+ }
+
+ // Figure out how large a buffer we need to slurp the whole file (with a
+ // '\0' at the end).
+ size_t bytes_to_read = static_cast<size_t>(stat_buf.st_size);
+ if (bytes_to_read > max_size_to_read - 1) {
+ CLOSE(fd);
+ return PLUGIN_FILE_ERROR_FILE_TOO_LARGE;
+ }
+
+ FILE* input_file = fdopen(fd, "rb");
+ if (!input_file) {
+ CLOSE(fd);
+ return PLUGIN_FILE_ERROR_OPEN;
+ }
+ // From here on, closing input_file will automatically close fd.
+
+ nacl::scoped_array<char> buffer(new char[bytes_to_read + 1]);
+ if (buffer == NULL) {
+ fclose(input_file);
+ return PLUGIN_FILE_ERROR_MEM_ALLOC;
+ }
+
+ size_t total_bytes_read = 0;
+ while (bytes_to_read > 0) {
+ size_t bytes_this_read = fread(&buffer[total_bytes_read],
+ sizeof(char),
+ bytes_to_read,
+ input_file);
+ if (bytes_this_read < bytes_to_read && (feof(input_file) ||
+ ferror(input_file))) {
+ fclose(input_file);
+ return PLUGIN_FILE_ERROR_READ;
+ }
+ total_bytes_read += bytes_this_read;
+ bytes_to_read -= bytes_this_read;
+ }
+
+ fclose(input_file);
+ buffer[total_bytes_read] = '\0';
+ out_buf = buffer.get();
+ return PLUGIN_FILE_SUCCESS;
+}
+
+} // namespace file_utils
+} // namespace plugin
+
« no previous file with comments | « ppapi/native_client/src/trusted/plugin/file_utils.h ('k') | ppapi/native_client/src/trusted/plugin/plugin.cc » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698