Codemod asserts to assertxs in the runtime
[hiphop-php.git] / hphp / runtime / ext / zlib / zip-file.cpp
blobc9b16e38493b4d3468520f7a24d8ca9746ddb058
1 /*
2 +----------------------------------------------------------------------+
3 | HipHop for PHP |
4 +----------------------------------------------------------------------+
5 | Copyright (c) 2010-present Facebook, Inc. (http://www.facebook.com) |
6 | Copyright (c) 1997-2010 The PHP Group |
7 +----------------------------------------------------------------------+
8 | This source file is subject to version 3.01 of the PHP license, |
9 | that is bundled with this package in the file LICENSE, and is |
10 | available through the world-wide-web at the following url: |
11 | http://www.php.net/license/3_01.txt |
12 | If you did not receive a copy of the PHP license and are unable to |
13 | obtain it through the world-wide-web, please send a note to |
14 | license@php.net so we can mail you a copy immediately. |
15 +----------------------------------------------------------------------+
18 #include "hphp/runtime/ext/zlib/zip-file.h"
19 #include "hphp/runtime/base/mem-file.h"
20 #include "hphp/runtime/base/temp-file.h"
21 #include "hphp/runtime/base/runtime-error.h"
23 namespace HPHP {
25 ///////////////////////////////////////////////////////////////////////////////
27 ZipFile::ZipFile() : m_gzFile(nullptr) {
28 setIsLocal(true);
29 setEof(false);
32 ZipFile::~ZipFile() {
33 ZipFile::closeImpl();
36 void ZipFile::sweep() {
37 closeImpl();
38 File::sweep();
41 bool ZipFile::open(const String& filename, const String& mode) {
42 assertx(m_gzFile == nullptr);
44 if (strchr(mode.c_str(), '+')) {
45 raise_warning("cannot open a zlib stream for reading and writing "
46 "at the same time!");
47 return false;
50 m_innerFile = File::Open(filename, mode);
51 if (isa<MemFile>(m_innerFile)) {
52 // We need an FD for the correct zlib APIs; MemFiles don't have an FD
53 if (strchr(mode.c_str(), 'w')) {
54 raise_warning("Cannot write to this stream type");
55 return false;
57 auto file = req::make<TempFile>();
58 while (!m_innerFile->eof()) {
59 file->write(m_innerFile->read(file->getChunkSize()));
61 file->rewind();
62 m_tempFile = file;
63 return (m_gzFile = gzdopen(dup(file->fd()), mode.data()));
65 if(m_innerFile) {
66 m_tempFile.reset();
67 return (m_gzFile = gzdopen(dup(m_innerFile->fd()), mode.data()));
69 return false;
72 bool ZipFile::close() {
73 invokeFiltersOnClose();
74 return closeImpl();
77 bool ZipFile::closeImpl() {
78 bool ret = true;
79 s_pcloseRet = 0;
80 if (!isClosed()) {
81 if (m_gzFile) {
82 s_pcloseRet = gzclose(m_gzFile);
83 ret = (s_pcloseRet == 0);
84 m_gzFile = nullptr;
86 setIsClosed(true);
87 if (m_innerFile) {
88 m_innerFile->close();
90 if (m_tempFile) {
91 m_tempFile->close();
92 m_tempFile.reset();
95 File::closeImpl();
96 return ret;
99 ///////////////////////////////////////////////////////////////////////////////
101 int64_t ZipFile::readImpl(char *buffer, int64_t length) {
102 assertx(m_gzFile);
103 int64_t nread = gzread(m_gzFile, buffer, length);
104 if (nread == 0 || gzeof(m_gzFile)) {
105 setEof(true);
106 } else {
107 errno = 0;
108 gzerror(m_gzFile, &errno);
109 if (errno == 1) { // Z_STREAM_END = 1
110 setEof(true);
113 return (nread < 0) ? 0 : nread;
116 int64_t ZipFile::writeImpl(const char *buffer, int64_t length) {
117 assertx(m_gzFile);
118 return gzwrite(m_gzFile, buffer, length);
121 bool ZipFile::seek(int64_t offset, int whence /* = SEEK_SET */) {
122 assertx(m_gzFile);
124 if (whence == SEEK_CUR) {
125 off_t result = gzseek(m_gzFile, 0, SEEK_CUR);
126 if (result != (off_t)-1) {
127 offset += result - (bufferedLen() + getPosition());
129 if (offset > 0 && offset < bufferedLen()) {
130 setReadPosition(getReadPosition() + offset);
131 setPosition(getPosition() + offset);
132 return true;
134 offset += getPosition();
135 whence = SEEK_SET;
138 // invalidate the current buffer
139 setWritePosition(0);
140 setReadPosition(0);
141 setEof(false);
142 gzclearerr(m_gzFile);
143 flush();
144 off_t result = gzseek(m_gzFile, offset, whence);
145 setPosition(result);
146 return result != (off_t)-1;
149 int64_t ZipFile::tell() {
150 assertx(m_gzFile);
151 return getPosition();
154 bool ZipFile::eof() {
155 assertx(m_gzFile);
156 int64_t avail = bufferedLen();
157 return avail > 0 ? false : getEof();
160 bool ZipFile::rewind() {
161 assertx(m_gzFile);
162 seek(0);
163 setWritePosition(0);
164 setReadPosition(0);
165 setPosition(0);
166 setEof(false);
167 gzrewind(m_gzFile);
168 return true;
171 bool ZipFile::flush() {
172 assertx(m_gzFile);
173 return gzflush(m_gzFile, Z_SYNC_FLUSH);
176 ///////////////////////////////////////////////////////////////////////////////