Commit 98ffd9fd authored by sbc@chromium.org's avatar sbc@chromium.org

[NaCl SDK] nacl_io: Remove duplication between passthroughfs and devfs.

They both had implementations of RealNode.

Also update examples/tutorial/testing so that it shuts down
the NaCl module correctly.

TEST=trybots
R=binji@chromium.org

Review URL: https://codereview.chromium.org/414993002

git-svn-id: svn://svn.chromium.org/chrome/trunk/src@285382 0039d316-1c4b-4281-b951-d872f2087c98
parent 114571b3
...@@ -45,9 +45,14 @@ function handleMessage(event) { ...@@ -45,9 +45,14 @@ function handleMessage(event) {
var msg = event.data; var msg = event.data;
var firstColon = msg.indexOf(':'); var firstColon = msg.indexOf(':');
var cmd = msg.substr(0, firstColon); var cmd = msg.substr(0, firstColon);
if (cmd == 'testend') {
event.srcElement.postMessage({'testend' : ''});
return;
}
var cmdFunctionName = cmd + 'Command'; var cmdFunctionName = cmd + 'Command';
var cmdFunction = window[cmdFunctionName]; var cmdFunction = window[cmdFunctionName];
if (typeof(cmdFunction) !== 'function') { if (typeof(cmdFunction) !== 'function') {
console.log('Unknown command: ' + cmd); console.log('Unknown command: ' + cmd);
console.log(' message: ' + msg); console.log(' message: ' + msg);
......
...@@ -17,7 +17,7 @@ ...@@ -17,7 +17,7 @@
.failed { background-color: #f00; } .failed { background-color: #f00; }
</style> </style>
</head> </head>
<body {{attrs}}> <body data-attrs="PS_EXIT_MESSAGE=testend" {{attrs}}>
<h1>{{title}}</h1> <h1>{{title}}</h1>
<div id="listener"></div> <div id="listener"></div>
<h2>Status: <code id="statusField">NO-STATUS</code></h2> <h2>Status: <code id="statusField">NO-STATUS</code></h2>
......
...@@ -20,6 +20,7 @@ ...@@ -20,6 +20,7 @@
#include "nacl_io/kernel_wrap_real.h" #include "nacl_io/kernel_wrap_real.h"
#include "nacl_io/node.h" #include "nacl_io/node.h"
#include "nacl_io/osunistd.h" #include "nacl_io/osunistd.h"
#include "nacl_io/passthroughfs/real_node.h"
#include "nacl_io/pepper_interface.h" #include "nacl_io/pepper_interface.h"
#include "sdk_util/auto_lock.h" #include "sdk_util/auto_lock.h"
...@@ -33,24 +34,6 @@ namespace nacl_io { ...@@ -33,24 +34,6 @@ namespace nacl_io {
namespace { namespace {
class RealNode : public Node {
public:
RealNode(Filesystem* filesystem, int fd);
virtual Error Read(const HandleAttr& attr,
void* buf,
size_t count,
int* out_bytes);
virtual Error Write(const HandleAttr& attr,
const void* buf,
size_t count,
int* out_bytes);
virtual Error GetStat(struct stat* stat);
protected:
int fd_;
};
class NullNode : public CharNode { class NullNode : public CharNode {
public: public:
explicit NullNode(Filesystem* filesystem) : CharNode(filesystem) {} explicit NullNode(Filesystem* filesystem) : CharNode(filesystem) {}
...@@ -125,44 +108,6 @@ class FsNode : public Node { ...@@ -125,44 +108,6 @@ class FsNode : public Node {
Filesystem* other_fs_; Filesystem* other_fs_;
}; };
RealNode::RealNode(Filesystem* filesystem, int fd) : Node(filesystem), fd_(fd) {
SetType(S_IFCHR);
}
Error RealNode::Read(const HandleAttr& attr,
void* buf,
size_t count,
int* out_bytes) {
*out_bytes = 0;
size_t readcnt;
int err = _real_read(fd_, buf, count, &readcnt);
if (err)
return err;
*out_bytes = static_cast<int>(readcnt);
return 0;
}
Error RealNode::Write(const HandleAttr& attr,
const void* buf,
size_t count,
int* out_bytes) {
*out_bytes = 0;
size_t writecnt;
int err = _real_write(fd_, buf, count, &writecnt);
if (err)
return err;
*out_bytes = static_cast<int>(writecnt);
return 0;
}
Error RealNode::GetStat(struct stat* stat) {
return _real_fstat(fd_, stat);
}
Error NullNode::Read(const HandleAttr& attr, Error NullNode::Read(const HandleAttr& attr,
void* buf, void* buf,
size_t count, size_t count,
......
...@@ -46,6 +46,7 @@ EXTERN_C_BEGIN ...@@ -46,6 +46,7 @@ EXTERN_C_BEGIN
__libnacl_irt_##group.name = (typeof(REAL(name)))WRAP(name); __libnacl_irt_##group.name = (typeof(REAL(name)))WRAP(name);
extern void __libnacl_irt_dev_filename_init(void); extern void __libnacl_irt_dev_filename_init(void);
extern void __libnacl_irt_dev_fdio_init(void);
extern struct nacl_irt_basic __libnacl_irt_basic; extern struct nacl_irt_basic __libnacl_irt_basic;
extern struct nacl_irt_fdio __libnacl_irt_fdio; extern struct nacl_irt_fdio __libnacl_irt_fdio;
...@@ -261,13 +262,14 @@ static void assign_real_pointers() { ...@@ -261,13 +262,14 @@ static void assign_real_pointers() {
static bool assigned = false; static bool assigned = false;
if (!assigned) { if (!assigned) {
__libnacl_irt_dev_filename_init(); __libnacl_irt_dev_filename_init();
__libnacl_irt_dev_fdio_init();
EXPAND_SYMBOL_LIST_OPERATION(ASSIGN_REAL_PTR) EXPAND_SYMBOL_LIST_OPERATION(ASSIGN_REAL_PTR)
assigned = true; assigned = true;
} }
} }
#define CHECK_REAL(func) \ #define CHECK_REAL(func) \
if (!REAL(func)) \ if (!REAL(func)) \
assign_real_pointers(); assign_real_pointers();
// "real" functions, i.e. the unwrapped original functions. // "real" functions, i.e. the unwrapped original functions.
...@@ -289,6 +291,12 @@ int _real_fstat(int fd, struct stat* buf) { ...@@ -289,6 +291,12 @@ int _real_fstat(int fd, struct stat* buf) {
int _real_isatty(int fd, int* result) { int _real_isatty(int fd, int* result) {
CHECK_REAL(isatty); CHECK_REAL(isatty);
// The real isatty function can be NULL (for example if we are running
// withing chrome).
if (REAL(isatty) == NULL) {
*result = 0;
return ENOTTY;
}
return REAL(isatty)(fd, result); return REAL(isatty)(fd, result);
} }
......
...@@ -48,6 +48,7 @@ ...@@ -48,6 +48,7 @@
"nacl_io.cc", "nacl_io.cc",
"node.cc", "node.cc",
"passthroughfs/passthrough_fs.cc", "passthroughfs/passthrough_fs.cc",
"passthroughfs/real_node.cc",
"path.cc", "path.cc",
"pepper_interface.cc", "pepper_interface.cc",
"pepper_interface_delegate.cc", "pepper_interface_delegate.cc",
...@@ -213,6 +214,7 @@ ...@@ -213,6 +214,7 @@
"osunistd.h", "osunistd.h",
"osutime.h", "osutime.h",
"passthroughfs/passthrough_fs.h", "passthroughfs/passthrough_fs.h",
"passthroughfs/real_node.h",
"path.h", "path.h",
"pepper_interface_delegate.h", "pepper_interface_delegate.h",
"pepper_interface_dummy.h", "pepper_interface_dummy.h",
......
...@@ -8,120 +8,10 @@ ...@@ -8,120 +8,10 @@
#include "nacl_io/kernel_handle.h" #include "nacl_io/kernel_handle.h"
#include "nacl_io/kernel_wrap_real.h" #include "nacl_io/kernel_wrap_real.h"
#include "nacl_io/passthroughfs/real_node.h"
namespace nacl_io { namespace nacl_io {
class PassthroughFsNode : public Node {
public:
explicit PassthroughFsNode(Filesystem* filesystem, int real_fd)
: Node(filesystem), real_fd_(real_fd) {}
protected:
virtual Error Init(int flags) { return 0; }
virtual void Destroy() {
if (real_fd_)
_real_close(real_fd_);
real_fd_ = 0;
}
public:
// Normal read/write operations on a file
virtual Error Read(const HandleAttr& attr,
void* buf,
size_t count,
int* out_bytes) {
*out_bytes = 0;
int64_t new_offset;
int err = _real_lseek(real_fd_, attr.offs, 0, &new_offset);
if (err)
return err;
size_t nread;
err = _real_read(real_fd_, buf, count, &nread);
if (err)
return err;
*out_bytes = static_cast<int>(nread);
return 0;
}
virtual Error Write(const HandleAttr& attr,
const void* buf,
size_t count,
int* out_bytes) {
*out_bytes = 0;
int64_t new_offset;
int err = _real_lseek(real_fd_, attr.offs, 0, &new_offset);
if (err)
return err;
size_t nwrote;
err = _real_write(real_fd_, buf, count, &nwrote);
if (err)
return err;
*out_bytes = static_cast<int>(nwrote);
return 0;
}
virtual Error FTruncate(off_t size) {
// TODO(binji): what to do here?
return ENOSYS;
}
virtual Error GetDents(size_t offs,
struct dirent* pdir,
size_t count,
int* out_bytes) {
size_t nread;
int err = _real_getdents(real_fd_, pdir, count, &nread);
if (err)
return err;
return nread;
}
virtual Error GetStat(struct stat* stat) {
int err = _real_fstat(real_fd_, stat);
if (err)
return err;
return 0;
}
virtual Error Isatty() {
#ifdef __GLIBC__
// isatty is not yet hooked up to the IRT interface under glibc.
return ENOTTY;
#else
int result = 0;
int err = _real_isatty(real_fd_, &result);
if (err)
return err;
return 0;
#endif
}
Error MMap(void* addr,
size_t length,
int prot,
int flags,
size_t offset,
void** out_addr) {
*out_addr = addr;
int err = _real_mmap(out_addr, length, prot, flags, real_fd_, offset);
if (err)
return err;
return 0;
}
private:
friend class PassthroughFs;
int real_fd_;
};
PassthroughFs::PassthroughFs() { PassthroughFs::PassthroughFs() {
} }
...@@ -144,7 +34,7 @@ Error PassthroughFs::Open(const Path& path, int mode, ScopedNode* out_node) { ...@@ -144,7 +34,7 @@ Error PassthroughFs::Open(const Path& path, int mode, ScopedNode* out_node) {
if (error) if (error)
return error; return error;
out_node->reset(new PassthroughFsNode(this, real_fd)); out_node->reset(new RealNode(this, real_fd, true));
return 0; return 0;
} }
...@@ -155,7 +45,7 @@ Error PassthroughFs::OpenResource(const Path& path, ScopedNode* out_node) { ...@@ -155,7 +45,7 @@ Error PassthroughFs::OpenResource(const Path& path, ScopedNode* out_node) {
if (error) if (error)
return error; return error;
out_node->reset(new PassthroughFsNode(this, real_fd)); out_node->reset(new RealNode(this, real_fd));
return 0; return 0;
} }
......
...@@ -3,7 +3,6 @@ ...@@ -3,7 +3,6 @@
// found in the LICENSE file. // found in the LICENSE file.
#ifndef LIBRARIES_NACL_IO_PASSTHROUGHFS_PASSTHROUGH_FS_H_ #ifndef LIBRARIES_NACL_IO_PASSTHROUGHFS_PASSTHROUGH_FS_H_
#define LIBRARIES_NACL_IO_PASSTHROUGHFS_PASSTHROUGH_FS_H_ #define LIBRARIES_NACL_IO_PASSTHROUGHFS_PASSTHROUGH_FS_H_
#include "nacl_io/filesystem.h" #include "nacl_io/filesystem.h"
......
// Copyright 2014 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.
#include "nacl_io/passthroughfs/real_node.h"
#include <errno.h>
#include "nacl_io/kernel_handle.h"
#include "nacl_io/kernel_wrap_real.h"
namespace nacl_io {
RealNode::RealNode(Filesystem* filesystem, int real_fd, bool close_on_destroy)
: Node(filesystem),
real_fd_(real_fd),
close_on_destroy_(close_on_destroy)
{
}
void RealNode::Destroy() {
if (close_on_destroy_)
_real_close(real_fd_);
real_fd_ = -1;
}
// Normal read/write operations on a file
Error RealNode::Read(const HandleAttr& attr,
void* buf,
size_t count,
int* out_bytes) {
*out_bytes = 0;
int64_t new_offset;
int err = _real_lseek(real_fd_, attr.offs, 0, &new_offset);
if (err && err != ESPIPE)
return err;
size_t nread;
err = _real_read(real_fd_, buf, count, &nread);
if (err)
return err;
*out_bytes = static_cast<int>(nread);
return 0;
}
Error RealNode::Write(const HandleAttr& attr,
const void* buf,
size_t count,
int* out_bytes) {
//nacl_io_log("Real::Write\n");
int err;
*out_bytes = 0;
int64_t new_offset;
err = _real_lseek(real_fd_, attr.offs, 0, &new_offset);
if (err && err != ESPIPE)
return err;
size_t nwrote;
err = _real_write(real_fd_, buf, count, &nwrote);
if (err)
return err;
*out_bytes = static_cast<int>(nwrote);
return 0;
}
Error RealNode::FTruncate(off_t size) {
// TODO(binji): what to do here?
return ENOSYS;
}
Error RealNode::GetDents(size_t offs,
struct dirent* pdir,
size_t count,
int* out_bytes) {
size_t nread;
int err = _real_getdents(real_fd_, pdir, count, &nread);
if (err)
return err;
return nread;
}
Error RealNode::GetStat(struct stat* stat) {
int err = _real_fstat(real_fd_, stat);
if (err)
return err;
return 0;
}
Error RealNode::Isatty() {
#ifdef __GLIBC__
// isatty is not yet hooked up to the IRT interface under glibc.
return ENOTTY;
#else
int result = 0;
int err = _real_isatty(real_fd_, &result);
if (err)
return err;
return 0;
#endif
}
Error RealNode::MMap(void* addr,
size_t length,
int prot,
int flags,
size_t offset,
void** out_addr) {
*out_addr = addr;
int err = _real_mmap(out_addr, length, prot, flags, real_fd_, offset);
if (err)
return err;
return 0;
}
}
// Copyright 2014 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.
//
#ifndef LIBRARIES_NACL_IO_PASSTHROUGHFS_REAL_NODE_H_
#define LIBRARIES_NACL_IO_PASSTHROUGHFS_REAL_NODE_H_
#include "nacl_io/node.h"
namespace nacl_io {
class RealNode : public Node {
public:
RealNode(Filesystem* filesystem, int real_fd, bool close_on_destroy = false);
protected:
virtual Error Init(int flags) { return 0; }
virtual void Destroy();
public:
// Normal read/write operations on a file
virtual Error Read(const HandleAttr& attr,
void* buf,
size_t count,
int* out_bytes);
virtual Error Write(const HandleAttr& attr,
const void* buf,
size_t count,
int* out_bytes);
virtual Error FTruncate(off_t size);
virtual Error GetDents(size_t offs,
struct dirent* pdir,
size_t count,
int* out_bytes);
virtual Error GetStat(struct stat* stat);
virtual Error Isatty();
virtual Error MMap(void* addr,
size_t length,
int prot,
int flags,
size_t offset,
void** out_addr);
private:
int real_fd_;
bool close_on_destroy_;
};
} // namespace nacl_io
#endif // LIBRARIES_NACL_IO_PASSTHROUGHFS_REAL_NODE_H_
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment