2 * Copyright (c) 2012 The Native Client Authors. All rights reserved.
3 * Use of this source code is governed by a BSD-style license that can be
4 * found in the LICENSE file.
8 * NaCl Generic I/O interface implementation: in-memory snapshot of a file.
11 #include "native_client/src/include/portability.h"
16 #include "native_client/src/shared/gio/gio.h"
18 struct GioVtbl const kGioMemoryFileSnapshotVtbl = {
19 GioMemoryFileSnapshotDtor,
28 int GioMemoryFileSnapshotCtor(struct GioMemoryFileSnapshot *self,
29 const char *filename) {
34 ((struct Gio *) self)->vtbl = (struct GioVtbl *) NULL;
35 if (0 == (iop = fopen(filename, "rb"))) {
38 if (fstat(fileno(iop), &stbuf) == -1) {
43 if (0 == (buffer = malloc(stbuf.st_size))) {
46 if (fread(buffer, 1, stbuf.st_size, iop) != (size_t) stbuf.st_size) {
51 if (GioMemoryFileCtor(&self->base, buffer, stbuf.st_size) == 0) {
56 ((struct Gio *) self)->vtbl = &kGioMemoryFileSnapshotVtbl;
61 void GioMemoryFileSnapshotDtor(struct Gio *vself) {
62 struct GioMemoryFileSnapshot *self = (struct GioMemoryFileSnapshot *)
64 free(self->base.buffer);
65 GioMemoryFileDtor(vself);