diff options
author | sbc@chromium.org <sbc@chromium.org@0039d316-1c4b-4281-b951-d872f2087c98> | 2013-05-16 01:12:14 +0000 |
---|---|---|
committer | sbc@chromium.org <sbc@chromium.org@0039d316-1c4b-4281-b951-d872f2087c98> | 2013-05-16 01:12:14 +0000 |
commit | 1f393cc4fb1e85689a9a0e5587d25dceb52cec99 (patch) | |
tree | ea890450975eda492485d6c8e58fa18ed97fb590 /native_client_sdk | |
parent | c03b3ba7b060058400df041233fced8f0bd16ccb (diff) | |
download | chromium_src-1f393cc4fb1e85689a9a0e5587d25dceb52cec99.zip chromium_src-1f393cc4fb1e85689a9a0e5587d25dceb52cec99.tar.gz chromium_src-1f393cc4fb1e85689a9a0e5587d25dceb52cec99.tar.bz2 |
Add ftruncate to nacl_io
BUG=225573
R=binji@chromium.org
Review URL: https://codereview.chromium.org/13424002
git-svn-id: svn://svn.chromium.org/chrome/trunk/src@200406 0039d316-1c4b-4281-b951-d872f2087c98
Diffstat (limited to 'native_client_sdk')
21 files changed, 51 insertions, 19 deletions
diff --git a/native_client_sdk/src/libraries/nacl_io/kernel_handle.cc b/native_client_sdk/src/libraries/nacl_io/kernel_handle.cc index 3bcbc92..dcf42fb 100644 --- a/native_client_sdk/src/libraries/nacl_io/kernel_handle.cc +++ b/native_client_sdk/src/libraries/nacl_io/kernel_handle.cc @@ -46,7 +46,7 @@ off_t KernelHandle::Seek(off_t offset, int whence) { // Seeking past the end of the file will zero out the space between the old // end and the new end. if (offs_ > node_size) { - if (node_->Truncate(offs_) < 0) { + if (node_->FTruncate(offs_) < 0) { errno = EINVAL; return -1; } diff --git a/native_client_sdk/src/libraries/nacl_io/kernel_intercept.cc b/native_client_sdk/src/libraries/nacl_io/kernel_intercept.cc index d42f118..9615dde 100644 --- a/native_client_sdk/src/libraries/nacl_io/kernel_intercept.cc +++ b/native_client_sdk/src/libraries/nacl_io/kernel_intercept.cc @@ -116,6 +116,10 @@ int ki_getdents(int fd, void *buf, unsigned int count) { return s_kp->getdents(fd, buf, count); } +int ki_ftruncate(int fd, off_t length) { + return s_kp->ftruncate(fd, length); +} + int ki_fsync(int fd) { return s_kp->fsync(fd); } diff --git a/native_client_sdk/src/libraries/nacl_io/kernel_intercept.h b/native_client_sdk/src/libraries/nacl_io/kernel_intercept.h index 6de064d..b1bcf90d 100644 --- a/native_client_sdk/src/libraries/nacl_io/kernel_intercept.h +++ b/native_client_sdk/src/libraries/nacl_io/kernel_intercept.h @@ -42,6 +42,7 @@ ssize_t ki_read(int fd, void* buf, size_t nbyte); ssize_t ki_write(int fd, const void* buf, size_t nbyte); int ki_fstat(int fd, struct stat *buf); int ki_getdents(int fd, void* buf, unsigned int count); +int ki_ftruncate(int fd, off_t length); int ki_fsync(int fd); int ki_isatty(int fd); int ki_close(int fd); diff --git a/native_client_sdk/src/libraries/nacl_io/kernel_proxy.cc b/native_client_sdk/src/libraries/nacl_io/kernel_proxy.cc index 82731a2..1980d8c 100644 --- a/native_client_sdk/src/libraries/nacl_io/kernel_proxy.cc +++ b/native_client_sdk/src/libraries/nacl_io/kernel_proxy.cc @@ -362,6 +362,17 @@ int KernelProxy::getdents(int fd, void* buf, unsigned int count) { return cnt; } +int KernelProxy::ftruncate(int fd, off_t length) { + KernelHandle* handle = AcquireHandle(fd); + + // check if fd is valid and handle exists + if (NULL == handle) return -1; + int ret = handle->node_->FTruncate(length); + + ReleaseHandle(handle); + return ret; +} + int KernelProxy::fsync(int fd) { KernelHandle* handle = AcquireHandle(fd); diff --git a/native_client_sdk/src/libraries/nacl_io/kernel_proxy.h b/native_client_sdk/src/libraries/nacl_io/kernel_proxy.h index 5ac7b9347..6c7b6b3 100644 --- a/native_client_sdk/src/libraries/nacl_io/kernel_proxy.h +++ b/native_client_sdk/src/libraries/nacl_io/kernel_proxy.h @@ -70,6 +70,7 @@ class KernelProxy : protected KernelObject { virtual int fchmod(int fd, int prot); virtual int fstat(int fd, struct stat *buf); virtual int getdents(int fd, void *buf, unsigned int count); + virtual int ftruncate(int fd, off_t length); virtual int fsync(int fd); virtual int isatty(int fd); diff --git a/native_client_sdk/src/libraries/nacl_io/kernel_wrap.h b/native_client_sdk/src/libraries/nacl_io/kernel_wrap.h index fa67be3..87dca82 100644 --- a/native_client_sdk/src/libraries/nacl_io/kernel_wrap.h +++ b/native_client_sdk/src/libraries/nacl_io/kernel_wrap.h @@ -50,6 +50,7 @@ struct stat; int fstat(int fd, struct stat* buf) NOTHROW; #endif int fsync(int fd); +int ftruncate(int fd, off_t length) NOTHROW; char* NAME(getcwd)(char* buf, getcwd_size_t size) NOTHROW; char* getwd(char* buf) NOTHROW; int getdents(int fd, void* buf, unsigned int count) NOTHROW; diff --git a/native_client_sdk/src/libraries/nacl_io/kernel_wrap_glibc.cc b/native_client_sdk/src/libraries/nacl_io/kernel_wrap_glibc.cc index 3c81474..efa765b 100644 --- a/native_client_sdk/src/libraries/nacl_io/kernel_wrap_glibc.cc +++ b/native_client_sdk/src/libraries/nacl_io/kernel_wrap_glibc.cc @@ -165,6 +165,10 @@ int fsync(int fd) { return ki_fsync(fd); } +int ftruncate(int fd, off_t length) NOTHROW { + return ki_ftruncate(fd, length); +} + char* getcwd(char* buf, size_t size) NOTHROW { return ki_getcwd(buf, size); } diff --git a/native_client_sdk/src/libraries/nacl_io/kernel_wrap_newlib.cc b/native_client_sdk/src/libraries/nacl_io/kernel_wrap_newlib.cc index bf32144..722d54e 100644 --- a/native_client_sdk/src/libraries/nacl_io/kernel_wrap_newlib.cc +++ b/native_client_sdk/src/libraries/nacl_io/kernel_wrap_newlib.cc @@ -81,6 +81,10 @@ int WRAP(fstat)(int fd, struct stat *buf) { return (ki_fstat(fd, buf) < 0) ? errno : 0; } +int ftruncate(int fd, off_t length) { + return ki_ftruncate(fd, length); +} + int fsync(int fd) { return ki_fsync(fd); } diff --git a/native_client_sdk/src/libraries/nacl_io/mount_http.cc b/native_client_sdk/src/libraries/nacl_io/mount_http.cc index 9e862e3..9a6579e 100644 --- a/native_client_sdk/src/libraries/nacl_io/mount_http.cc +++ b/native_client_sdk/src/libraries/nacl_io/mount_http.cc @@ -160,7 +160,7 @@ class MountNodeHttp : public MountNode { virtual int GetDents(size_t offs, struct dirent* pdir, size_t count); virtual int GetStat(struct stat* stat); virtual int Read(size_t offs, void* buf, size_t count); - virtual int Truncate(size_t size); + virtual int FTruncate(off_t size); virtual int Write(size_t offs, const void* buf, size_t count); virtual size_t GetSize(); @@ -268,7 +268,7 @@ int MountNodeHttp::Read(size_t offs, void* buf, size_t count) { return DownloadPartial(offs, buf, count); } -int MountNodeHttp::Truncate(size_t size) { +int MountNodeHttp::FTruncate(off_t size) { errno = ENOSYS; return -1; } diff --git a/native_client_sdk/src/libraries/nacl_io/mount_node.cc b/native_client_sdk/src/libraries/nacl_io/mount_node.cc index 83e4e30..f4b692f 100644 --- a/native_client_sdk/src/libraries/nacl_io/mount_node.cc +++ b/native_client_sdk/src/libraries/nacl_io/mount_node.cc @@ -47,6 +47,11 @@ int MountNode::FSync() { return 0; } +int MountNode::FTruncate(off_t length) { + errno = EINVAL; + return -1; +} + int MountNode::GetDents(size_t offs, struct dirent* pdir, size_t count) { errno = ENOTDIR; return -1; @@ -68,11 +73,6 @@ int MountNode::Read(size_t offs, void* buf, size_t count) { return -1; } -int MountNode::Truncate(size_t size) { - errno = EINVAL; - return -1; -} - int MountNode::Write(size_t offs, const void* buf, size_t count) { errno = EINVAL; return -1; diff --git a/native_client_sdk/src/libraries/nacl_io/mount_node.h b/native_client_sdk/src/libraries/nacl_io/mount_node.h index ced95b2..7d87048 100644 --- a/native_client_sdk/src/libraries/nacl_io/mount_node.h +++ b/native_client_sdk/src/libraries/nacl_io/mount_node.h @@ -29,11 +29,11 @@ class MountNode : public RefObject { // directly so it must lock and unlock appropriately. These functions // must not be called by the mount. virtual int FSync(); + virtual int FTruncate(off_t length); virtual int GetDents(size_t offs, struct dirent* pdir, size_t count); virtual int GetStat(struct stat* stat); virtual int Ioctl(int request, char* arg); virtual int Read(size_t offs, void* buf, size_t count); - virtual int Truncate(size_t size); virtual int Write(size_t offs, const void* buf, size_t count); virtual void* MMap(void* addr, size_t length, int prot, int flags, size_t offset); diff --git a/native_client_sdk/src/libraries/nacl_io/mount_node_dir.cc b/native_client_sdk/src/libraries/nacl_io/mount_node_dir.cc index b97491f..541a70f 100644 --- a/native_client_sdk/src/libraries/nacl_io/mount_node_dir.cc +++ b/native_client_sdk/src/libraries/nacl_io/mount_node_dir.cc @@ -27,7 +27,7 @@ int MountNodeDir::Read(size_t offs, void *buf, size_t count) { return -1; } -int MountNodeDir::Truncate(size_t size) { +int MountNodeDir::FTruncate(off_t size) { errno = EISDIR; return -1; } diff --git a/native_client_sdk/src/libraries/nacl_io/mount_node_dir.h b/native_client_sdk/src/libraries/nacl_io/mount_node_dir.h index b747207..b6af58d 100644 --- a/native_client_sdk/src/libraries/nacl_io/mount_node_dir.h +++ b/native_client_sdk/src/libraries/nacl_io/mount_node_dir.h @@ -25,9 +25,9 @@ class MountNodeDir : public MountNode { public: typedef std::map<std::string, MountNode*> MountNodeMap_t; + virtual int FTruncate(off_t size); virtual int GetDents(size_t offs, struct dirent* pdir, size_t count); virtual int Read(size_t offs, void *buf, size_t count); - virtual int Truncate(size_t size); virtual int Write(size_t offs, void *buf, size_t count); // Adds a finds or adds a directory entry as an INO, updating the refcount diff --git a/native_client_sdk/src/libraries/nacl_io/mount_node_html5fs.cc b/native_client_sdk/src/libraries/nacl_io/mount_node_html5fs.cc index b540014..fb0d5ed 100644 --- a/native_client_sdk/src/libraries/nacl_io/mount_node_html5fs.cc +++ b/native_client_sdk/src/libraries/nacl_io/mount_node_html5fs.cc @@ -187,7 +187,7 @@ int MountNodeHtml5Fs::Read(size_t offs, void* buf, size_t count) { return result; } -int MountNodeHtml5Fs::Truncate(size_t size) { +int MountNodeHtml5Fs::FTruncate(off_t size) { int32_t result = mount_->ppapi()->GetFileIoInterface()->SetLength( fileio_resource_, size, PP_BlockUntilComplete()); if (result != PP_OK) { diff --git a/native_client_sdk/src/libraries/nacl_io/mount_node_html5fs.h b/native_client_sdk/src/libraries/nacl_io/mount_node_html5fs.h index 535ae3a..58b4d13 100644 --- a/native_client_sdk/src/libraries/nacl_io/mount_node_html5fs.h +++ b/native_client_sdk/src/libraries/nacl_io/mount_node_html5fs.h @@ -19,7 +19,7 @@ class MountNodeHtml5Fs : public MountNode { virtual int GetDents(size_t offs, struct dirent* pdir, size_t count); virtual int GetStat(struct stat* stat); virtual int Read(size_t offs, void* buf, size_t count); - virtual int Truncate(size_t size); + virtual int FTruncate(off_t size); virtual int Write(size_t offs, const void* buf, size_t count); virtual size_t GetSize(); diff --git a/native_client_sdk/src/libraries/nacl_io/mount_node_mem.cc b/native_client_sdk/src/libraries/nacl_io/mount_node_mem.cc index 248fed4..0fa82ba 100644 --- a/native_client_sdk/src/libraries/nacl_io/mount_node_mem.cc +++ b/native_client_sdk/src/libraries/nacl_io/mount_node_mem.cc @@ -41,7 +41,7 @@ int MountNodeMem::Write(size_t offs, const void *buf, size_t count) { if (count == 0) return 0; if (count + offs > GetSize()) { - Truncate(count + offs); + FTruncate(count + offs); count = GetSize() - offs; } @@ -49,7 +49,7 @@ int MountNodeMem::Write(size_t offs, const void *buf, size_t count) { return static_cast<int>(count); } -int MountNodeMem::Truncate(size_t size) { +int MountNodeMem::FTruncate(off_t size) { size_t need = (size + BLOCK_MASK) & ~BLOCK_MASK; // If the current capacity is correct, just adjust and return diff --git a/native_client_sdk/src/libraries/nacl_io/mount_node_mem.h b/native_client_sdk/src/libraries/nacl_io/mount_node_mem.h index f7e4fff..07acfe1 100644 --- a/native_client_sdk/src/libraries/nacl_io/mount_node_mem.h +++ b/native_client_sdk/src/libraries/nacl_io/mount_node_mem.h @@ -18,7 +18,7 @@ class MountNodeMem : public MountNode { // Normal read/write operations on a file virtual int Read(size_t offs, void* buf, size_t count); virtual int Write(size_t offs, const void* buf, size_t count); - virtual int Truncate(size_t size); + virtual int FTruncate(off_t size); private: char* data_; diff --git a/native_client_sdk/src/libraries/nacl_io/mount_passthrough.cc b/native_client_sdk/src/libraries/nacl_io/mount_passthrough.cc index 7cb75b1..82660d6 100644 --- a/native_client_sdk/src/libraries/nacl_io/mount_passthrough.cc +++ b/native_client_sdk/src/libraries/nacl_io/mount_passthrough.cc @@ -62,7 +62,7 @@ class MountNodePassthrough : public MountNode { return static_cast<int>(nwrote); } - virtual int Truncate(size_t size) { + virtual int FTruncate(off_t size) { // TODO(binji): what to do here? return -1; } diff --git a/native_client_sdk/src/libraries/nacl_io_test/kernel_proxy_mock.h b/native_client_sdk/src/libraries/nacl_io_test/kernel_proxy_mock.h index 45caa4c..d74e886 100644 --- a/native_client_sdk/src/libraries/nacl_io_test/kernel_proxy_mock.h +++ b/native_client_sdk/src/libraries/nacl_io_test/kernel_proxy_mock.h @@ -23,6 +23,7 @@ class KernelProxyMock : public KernelProxy { MOCK_METHOD1(close, int(int)); MOCK_METHOD1(dup, int(int)); MOCK_METHOD2(dup2, int(int, int)); + MOCK_METHOD2(ftruncate, int(int, off_t)); MOCK_METHOD2(fstat, int(int, struct stat*)); MOCK_METHOD1(fsync, int(int)); MOCK_METHOD2(getcwd, char*(char*, size_t)); diff --git a/native_client_sdk/src/libraries/nacl_io_test/kernel_wrap_test.cc b/native_client_sdk/src/libraries/nacl_io_test/kernel_wrap_test.cc index 0cf3bcb..f6f010c 100644 --- a/native_client_sdk/src/libraries/nacl_io_test/kernel_wrap_test.cc +++ b/native_client_sdk/src/libraries/nacl_io_test/kernel_wrap_test.cc @@ -136,6 +136,11 @@ TEST_F(KernelWrapTest, fstat) { EXPECT_THAT(&in_statbuf, IsEqualToStatbuf(&out_statbuf)); } +TEST_F(KernelWrapTest, ftruncate) { + EXPECT_CALL(mock, ftruncate(456, 0)).Times(1); + ftruncate(456, 0); +} + TEST_F(KernelWrapTest, fsync) { EXPECT_CALL(mock, fsync(345)).Times(1); fsync(345); diff --git a/native_client_sdk/src/libraries/nacl_io_test/mount_html5fs_test.cc b/native_client_sdk/src/libraries/nacl_io_test/mount_html5fs_test.cc index c84b761..ce3ab2d 100644 --- a/native_client_sdk/src/libraries/nacl_io_test/mount_html5fs_test.cc +++ b/native_client_sdk/src/libraries/nacl_io_test/mount_html5fs_test.cc @@ -395,12 +395,12 @@ TEST_F(MountHtml5FsNodeSyncTest, GetStat) { EXPECT_EQ(creation_time, statbuf.st_ctime); } -TEST_F(MountHtml5FsNodeSyncTest, Truncate) { +TEST_F(MountHtml5FsNodeSyncTest, FTruncate) { const int size = 123; EXPECT_CALL(*fileio_, SetLength(fileio_resource_, size, _)) .WillOnce(Return(int32_t(PP_OK))); - int result = node_->Truncate(size); + int result = node_->FTruncate(size); EXPECT_EQ(0, result); } |