Chromium Code Reviews| 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..b3e5a073cead916428bae87fc3f18984fa9e0a16 |
| --- /dev/null |
| +++ b/ppapi/native_client/src/trusted/plugin/file_utils.cc |
| @@ -0,0 +1,76 @@ |
| +// 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 { |
|
jvoung (off chromium)
2013/05/23 20:33:19
same, separate lines
eliben
2013/05/23 20:54:25
Done.
|
| + |
| +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 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 ERROR_FILE_TOO_LARGE; |
| + } |
| + |
| + FILE* input_file = fdopen(fd, "rb"); |
| + if (!input_file) { |
| + CLOSE(fd); |
| + return 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 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 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 SUCCESS; |
| +} |
| + |
| +} // namespace file_utils |
| +} // namespace plugin |
| + |