id() ); $staging = $dir . '/staging'; $size = (int) $job->get( 'size', 0 ); $block = Environment::copyBlockBytes(); try { $archive = fopen( Paths::importArchive( $job ), 'rb' ); } catch ( \RuntimeException $e ) { return StageResult::fail( 'om_bad_source', $e->getMessage() ); } if ( false === $archive ) { return StageResult::fail( 'om_read', __( 'Cannot read the uploaded archive.', 'migratex' ) ); } if ( ! wp_mkdir_p( $staging ) ) { fclose( $archive ); return StageResult::fail( 'om_staging', __( 'Cannot create the staging folder.', 'migratex' ) ); } $reader = new ArchiveReader( $archive, $size ); $off = (int) $job->cursor( 'off', $reader->firstEntryOffset() ); $entry = $job->cursor( 'entry' ); $entries = (int) $job->cursor( 'entries', 0 ); $out = null; try { while ( ! $budget->exhausted() ) { if ( null === $entry ) { if ( $off >= $size ) { return StageResult::fail( 'om_no_eof', __( 'The archive has no end marker; it is incomplete.', 'migratex' ) ); } $head = $reader->header( $off ); if ( Format::TYPE_EOF === $head['type'] ) { if ( $head['count'] !== $entries ) { return StageResult::fail( 'om_count', __( 'The archive entry count does not match; it is damaged.', 'migratex' ) ); } $job->setStageProgress( 1.0 ); return StageResult::next(); } if ( Format::TYPE_FILE !== $head['type'] ) { return StageResult::fail( 'om_entry_type', __( 'The archive contains an unknown entry type.', 'migratex' ) ); } if ( ! Format::isSafeName( $head['name'] ) || ! Format::isAllowedName( $head['name'] ) ) { return StageResult::fail( 'om_unsafe_entry', __( 'The archive contains an unsafe file path and was rejected.', 'migratex' ) ); } $dest = $staging . '/' . $head['name']; $folder = dirname( $dest ); if ( ! wp_mkdir_p( $folder ) || ! Paths::contains( $staging, $folder ) ) { return StageResult::fail( 'om_unsafe_entry', __( 'The archive contains an unsafe file path and was rejected.', 'migratex' ) ); } $entry = array( 'name' => $head['name'], 'dest' => $dest, 'size' => $head['size'], 'data' => $head['data'], 'next' => $head['next'], 'done' => 0, ); } if ( null === $out ) { // Resume point: anything past `done` is from a tick that did not finish. $out = SafeFile::openAppend( $entry['dest'], (int) $entry['done'] ); } $reader->seek( (int) $entry['data'] + (int) $entry['done'] ); while ( $entry['done'] < $entry['size'] && ! $budget->exhausted() ) { $want = (int) min( $block, $entry['size'] - $entry['done'] ); $data = fread( $archive, $want ); if ( false === $data || strlen( $data ) !== $want ) { return StageResult::fail( 'om_read', __( 'The archive ended while reading a file.', 'migratex' ) ); } SafeFile::writeAll( $out, $data ); $entry['done'] += $want; // Kept current inside the loop too, so a heartbeat in the middle of a big file shows it moving. $job->setStageProgress( min( 0.99, ( $entry['data'] + $entry['done'] ) / max( 1, $size ) ) ); $job->setDetail( array( 'files' => $entries, 'file' => $entry['name'], 'bytes' => $entry['data'] + $entry['done'], 'total_bytes' => $size, ) ); $budget->unit(); } if ( $entry['done'] >= $entry['size'] ) { fclose( $out ); $out = null; $off = (int) $entry['next']; $entry = null; ++$entries; $budget->unit(); } else { fflush( $out ); } $job->setCursor( 'off', $off ); $job->setCursor( 'entry', $entry ); $job->setCursor( 'entries', $entries ); $job->setStageProgress( min( 0.99, ( $entry ? $entry['data'] + $entry['done'] : $off ) / max( 1, $size ) ) ); $job->setDetail( array( 'files' => $entries, 'file' => $entry ? $entry['name'] : '', 'bytes' => $entry ? $entry['data'] + $entry['done'] : $off, 'total_bytes' => $size, ) ); } } catch ( \RuntimeException $e ) { return StageResult::fail( 'om_extract', $e->getMessage() ); } finally { if ( is_resource( $out ) ) { fclose( $out ); } fclose( $archive ); } return StageResult::more(); } }