diff --git a/cpp/src/arrow/filesystem/gcsfs.cc b/cpp/src/arrow/filesystem/gcsfs.cc index 4273b8a3245..5d7d7c750b0 100644 --- a/cpp/src/arrow/filesystem/gcsfs.cc +++ b/cpp/src/arrow/filesystem/gcsfs.cc @@ -250,31 +250,51 @@ class GcsRandomAccessFile : public arrow::io::RandomAccessFile { //@{ // @name FileInterface Status Close() override { - ARROW_RETURN_NOT_OK(InitializeStream()); - return stream_->Close(); + closed_ = true; + if (stream_) { + return stream_->Close(); + } + return Status::OK(); } Status Abort() override { - ARROW_RETURN_NOT_OK(InitializeStream()); - return stream_->Abort(); + closed_ = true; + if (stream_) { + return stream_->Abort(); + } + return Status::OK(); } Result Tell() const override { - ARROW_RETURN_NOT_OK(InitializeStream()); - return stream_->TellOr(metadata_.size()); - } - bool closed() const override { - auto status = InitializeStream(); - if (!status.ok()) return true; - return stream_->closed(); + if (closed()) return Status::Invalid("Cannot use Tell() on a closed stream"); + if (!stream_) { + return offset_; + } + return stream_->TellOr(file_size()); } + bool closed() const override { return closed_ || (stream_ && stream_->closed()); } //@} //@{ // @name Readable Result Read(int64_t nbytes, void* out) override { + if (closed()) return Status::Invalid("Cannot read from a closed stream"); + if (nbytes < 0) { + return Status::Invalid("Cannot read a negative number of bytes"); + } + if (nbytes == 0 || (!stream_ && offset_ == file_size())) { + return 0; + } ARROW_RETURN_NOT_OK(InitializeStream()); return stream_->Read(nbytes, out); } Result> Read(int64_t nbytes) override { + if (closed()) return Status::Invalid("Cannot read from a closed stream"); + if (nbytes < 0) { + return Status::Invalid("Cannot read a negative number of bytes"); + } + if (nbytes == 0 || (!stream_ && offset_ == file_size())) { + ARROW_ASSIGN_OR_RAISE(auto buffer, arrow::AllocateResizableBuffer(0)); + return std::shared_ptr(std::move(buffer)); + } ARROW_RETURN_NOT_OK(InitializeStream()); return stream_->Read(nbytes); } @@ -289,7 +309,7 @@ class GcsRandomAccessFile : public arrow::io::RandomAccessFile { //@{ // @name RandomAccessFile - Result GetSize() override { return metadata_.size(); } + Result GetSize() override { return file_size(); } Result ReadAt(int64_t position, int64_t nbytes, void* out) override { if (closed()) return Status::Invalid("Cannot read from closed file"); std::shared_ptr stream; @@ -311,23 +331,34 @@ class GcsRandomAccessFile : public arrow::io::RandomAccessFile { // from Seekable Status Seek(int64_t position) override { if (closed()) return Status::Invalid("Cannot seek in a closed file"); - ARROW_ASSIGN_OR_RAISE( - stream_, factory_(gcs::Generation(metadata_.generation()), gcs::ReadRange(), - gcs::ReadFromOffset(position))); + if (position < 0) { + return Status::Invalid("Cannot seek to negative position ", position); + } + if (position > file_size()) { + return Status::Invalid("Cannot seek past end of file (position = ", position, + ", file size = ", file_size(), ")"); + } + stream_.reset(); + offset_ = position; return Status::OK(); } private: + int64_t file_size() const { return static_cast(metadata_.size()); } + Status InitializeStream() const { if (!stream_) { + auto offset = offset_ == 0 ? gcs::ReadFromOffset() : gcs::ReadFromOffset(offset_); ARROW_ASSIGN_OR_RAISE(stream_, factory_(gcs::Generation(metadata_.generation()), - gcs::ReadRange(), gcs::ReadFromOffset())); + gcs::ReadRange(), offset)); } return Status::OK(); } InputStreamFactory factory_; gcs::ObjectMetadata metadata_; std::shared_ptr mutable stream_; + int64_t offset_ = 0; + bool closed_ = false; }; } // namespace diff --git a/cpp/src/arrow/filesystem/gcsfs_test.cc b/cpp/src/arrow/filesystem/gcsfs_test.cc index 7c694de9e25..f5837b47557 100644 --- a/cpp/src/arrow/filesystem/gcsfs_test.cc +++ b/cpp/src/arrow/filesystem/gcsfs_test.cc @@ -1448,6 +1448,79 @@ TEST_F(GcsIntegrationTest, OpenInputFileClosed) { ASSERT_RAISES(Invalid, stream->Seek(2)); } +TEST_F(GcsIntegrationTest, OpenInputFileSeekToEnd) { + ASSERT_OK_AND_ASSIGN(auto fs, GcsFileSystem::Make(TestGcsOptions())); + + const auto path = + internal::ConcatAbstractPath(PreexistingBucketName(), "seek-eof-object.txt"); + CreateFile(fs.get(), path, "abc"); + + ASSERT_OK_AND_ASSIGN(auto file, fs->OpenInputFile(path)); + ASSERT_FALSE(file->closed()); + ASSERT_OK_AND_EQ(3, file->GetSize()); + ASSERT_OK_AND_EQ(0, file->Tell()); + ASSERT_OK_AND_EQ(0, file->Read(0, nullptr)); + + ASSERT_OK(file->Seek(3)); + ASSERT_FALSE(file->closed()); + ASSERT_OK_AND_EQ(3, file->Tell()); + std::array raw_buf{}; + ASSERT_OK_AND_EQ(0, file->Read(raw_buf.size(), raw_buf.data())); + ASSERT_OK_AND_ASSIGN(auto buffer, file->Read(raw_buf.size())); + AssertBufferEqual(*buffer, ""); + ASSERT_OK_AND_EQ(3, file->Tell()); + + ASSERT_OK_AND_ASSIGN(buffer, file->ReadAt(0, 1)); + AssertBufferEqual(*buffer, "a"); + ASSERT_OK_AND_EQ(3, file->Tell()); + + ASSERT_OK(file->Seek(1)); + ASSERT_OK_AND_EQ(1, file->Tell()); + ASSERT_OK_AND_ASSIGN(buffer, file->Read(1)); + AssertBufferEqual(*buffer, "b"); + ASSERT_OK_AND_EQ(2, file->Tell()); + + ASSERT_RAISES(Invalid, file->Seek(-1)); + ASSERT_RAISES(Invalid, file->Seek(4)); + ASSERT_OK_AND_EQ(2, file->Tell()); + + ASSERT_OK(file->Seek(2)); + ASSERT_OK_AND_ASSIGN(buffer, file->Read(2)); + AssertBufferEqual(*buffer, "c"); + ASSERT_OK_AND_EQ(3, file->Tell()); + + ASSERT_OK(file->Close()); + ASSERT_TRUE(file->closed()); + ASSERT_RAISES(Invalid, file->Seek(0)); +} + +TEST_F(GcsIntegrationTest, OpenInputFileEmpty) { + ASSERT_OK_AND_ASSIGN(auto fs, GcsFileSystem::Make(TestGcsOptions())); + + const auto path = + internal::ConcatAbstractPath(PreexistingBucketName(), "empty-input-file.txt"); + CreateFile(fs.get(), path, ""); + + ASSERT_OK_AND_ASSIGN(auto file, fs->OpenInputFile(path)); + ASSERT_FALSE(file->closed()); + ASSERT_OK_AND_EQ(0, file->GetSize()); + ASSERT_OK_AND_EQ(0, file->Tell()); + + std::array raw_buf{}; + ASSERT_OK_AND_EQ(0, file->Read(raw_buf.size(), raw_buf.data())); + ASSERT_OK_AND_ASSIGN(auto buffer, file->Read(raw_buf.size())); + AssertBufferEqual(*buffer, ""); + + ASSERT_OK(file->Seek(0)); + ASSERT_OK_AND_EQ(0, file->Tell()); + ASSERT_RAISES(Invalid, file->Seek(-1)); + ASSERT_RAISES(Invalid, file->Seek(1)); + + ASSERT_OK(file->Close()); + ASSERT_TRUE(file->closed()); + ASSERT_RAISES(Invalid, file->Seek(0)); +} + TEST_F(GcsIntegrationTest, TestFileSystemFromUri) { // Smoke test for FileSystemFromUri std::string path;