001/*
002 * Licensed to the Apache Software Foundation (ASF) under one
003 * or more contributor license agreements.  See the NOTICE file
004 * distributed with this work for additional information
005 * regarding copyright ownership.  The ASF licenses this file
006 * to you under the Apache License, Version 2.0 (the
007 * "License"); you may not use this file except in compliance
008 * with the License.  You may obtain a copy of the License at
009 *
010 * http://www.apache.org/licenses/LICENSE-2.0
011 *
012 * Unless required by applicable law or agreed to in writing,
013 * software distributed under the License is distributed on an
014 * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
015 * KIND, either express or implied.  See the License for the
016 * specific language governing permissions and limitations
017 * under the License.
018 */
019package org.apache.commons.compress.archivers.tar;
020
021import java.io.File;
022import java.io.IOException;
023import java.io.OutputStream;
024import java.io.StringWriter;
025import java.io.UnsupportedEncodingException;
026import java.nio.ByteBuffer;
027import java.util.Arrays;
028import java.util.Date;
029import java.util.HashMap;
030import java.util.Map;
031import org.apache.commons.compress.archivers.ArchiveEntry;
032import org.apache.commons.compress.archivers.ArchiveOutputStream;
033import org.apache.commons.compress.archivers.zip.ZipEncoding;
034import org.apache.commons.compress.archivers.zip.ZipEncodingHelper;
035import org.apache.commons.compress.utils.CharsetNames;
036import org.apache.commons.compress.utils.CountingOutputStream;
037import org.apache.commons.compress.utils.FixedLengthBlockOutputStream;
038
039/**
040 * The TarOutputStream writes a UNIX tar archive as an OutputStream. Methods are provided to put
041 * entries, and then write their contents by writing to this stream using write().
042 *
043 * @NotThreadSafe
044 */
045public class TarArchiveOutputStream extends ArchiveOutputStream {
046
047    /**
048     * Fail if a long file name is required in the archive.
049     */
050    public static final int LONGFILE_ERROR = 0;
051
052    /**
053     * Long paths will be truncated in the archive.
054     */
055    public static final int LONGFILE_TRUNCATE = 1;
056
057    /**
058     * GNU tar extensions are used to store long file names in the archive.
059     */
060    public static final int LONGFILE_GNU = 2;
061
062    /**
063     * POSIX/PAX extensions are used to store long file names in the archive.
064     */
065    public static final int LONGFILE_POSIX = 3;
066
067    /**
068     * Fail if a big number (e.g. size > 8GiB) is required in the archive.
069     */
070    public static final int BIGNUMBER_ERROR = 0;
071
072    /**
073     * star/GNU tar/BSD tar extensions are used to store big number in the archive.
074     */
075    public static final int BIGNUMBER_STAR = 1;
076
077    /**
078     * POSIX/PAX extensions are used to store big numbers in the archive.
079     */
080    public static final int BIGNUMBER_POSIX = 2;
081    private static final int RECORD_SIZE = 512;
082
083    private long currSize;
084    private String currName;
085    private long currBytes;
086    private final byte[] recordBuf;
087    private int longFileMode = LONGFILE_ERROR;
088    private int bigNumberMode = BIGNUMBER_ERROR;
089    private int recordsWritten;
090    private final int recordsPerBlock;
091
092    private boolean closed = false;
093
094    /**
095     * Indicates if putArchiveEntry has been called without closeArchiveEntry
096     */
097    private boolean haveUnclosedEntry = false;
098
099    /**
100     * indicates if this archive is finished
101     */
102    private boolean finished = false;
103
104    private final FixedLengthBlockOutputStream out;
105    private final CountingOutputStream countingOut;
106
107    private final ZipEncoding zipEncoding;
108
109    // the provided encoding (for unit tests)
110    final String encoding;
111
112    private boolean addPaxHeadersForNonAsciiNames = false;
113    private static final ZipEncoding ASCII =
114        ZipEncodingHelper.getZipEncoding("ASCII");
115
116    private static final int BLOCK_SIZE_UNSPECIFIED = -511;
117
118    /**
119     * Constructor for TarInputStream.
120     *
121     * @param os the output stream to use
122     */
123    public TarArchiveOutputStream(final OutputStream os) {
124        this(os, BLOCK_SIZE_UNSPECIFIED);
125    }
126
127    /**
128     * Constructor for TarInputStream.
129     *
130     * @param os the output stream to use
131     * @param encoding name of the encoding to use for file names
132     * @since 1.4
133     */
134    public TarArchiveOutputStream(final OutputStream os, final String encoding) {
135        this(os, BLOCK_SIZE_UNSPECIFIED, encoding);
136    }
137
138    /**
139     * Constructor for TarInputStream.
140     *
141     * @param os the output stream to use
142     * @param blockSize the block size to use. Must be a multiple of 512 bytes.
143     */
144    public TarArchiveOutputStream(final OutputStream os, final int blockSize) {
145        this(os, blockSize, null);
146    }
147
148
149    /**
150     * Constructor for TarInputStream.
151     *
152     * @param os the output stream to use
153     * @param blockSize the block size to use
154     * @param recordSize the record size to use. Must be 512 bytes.
155     * @deprecated recordSize must always be 512 bytes. An IllegalArgumentException will be thrown
156     * if any other value is used
157     */
158    @Deprecated
159    public TarArchiveOutputStream(final OutputStream os, final int blockSize,
160        final int recordSize) {
161        this(os, blockSize, recordSize, null);
162    }
163
164    /**
165     * Constructor for TarInputStream.
166     *
167     * @param os the output stream to use
168     * @param blockSize the block size to use . Must be a multiple of 512 bytes.
169     * @param recordSize the record size to use. Must be 512 bytes.
170     * @param encoding name of the encoding to use for file names
171     * @since 1.4
172     * @deprecated recordSize must always be 512 bytes. An IllegalArgumentException will be thrown
173     * if any other value is used.
174     */
175    @Deprecated
176    public TarArchiveOutputStream(final OutputStream os, final int blockSize,
177        final int recordSize, final String encoding) {
178        this(os, blockSize, encoding);
179        if (recordSize != RECORD_SIZE) {
180            throw new IllegalArgumentException(
181                "Tar record size must always be 512 bytes. Attempt to set size of " + recordSize);
182        }
183
184    }
185
186    /**
187     * Constructor for TarInputStream.
188     *
189     * @param os the output stream to use
190     * @param blockSize the block size to use. Must be a multiple of 512 bytes.
191     * @param encoding name of the encoding to use for file names
192     * @since 1.4
193     */
194    public TarArchiveOutputStream(final OutputStream os, final int blockSize,
195        final String encoding) {
196        int realBlockSize;
197        if (BLOCK_SIZE_UNSPECIFIED == blockSize) {
198            realBlockSize = RECORD_SIZE;
199        } else {
200            realBlockSize = blockSize;
201        }
202
203        if (realBlockSize <=0 || realBlockSize % RECORD_SIZE != 0) {
204            throw new IllegalArgumentException("Block size must be a multiple of 512 bytes. Attempt to use set size of " + blockSize);
205        }
206        out = new FixedLengthBlockOutputStream(countingOut = new CountingOutputStream(os),
207                                               RECORD_SIZE);
208        this.encoding = encoding;
209        this.zipEncoding = ZipEncodingHelper.getZipEncoding(encoding);
210
211        this.recordBuf = new byte[RECORD_SIZE];
212        this.recordsPerBlock = realBlockSize / RECORD_SIZE;
213    }
214
215    /**
216     * Set the long file mode. This can be LONGFILE_ERROR(0), LONGFILE_TRUNCATE(1) or
217     * LONGFILE_GNU(2). This specifies the treatment of long file names (names &gt;=
218     * TarConstants.NAMELEN). Default is LONGFILE_ERROR.
219     *
220     * @param longFileMode the mode to use
221     */
222    public void setLongFileMode(final int longFileMode) {
223        this.longFileMode = longFileMode;
224    }
225
226    /**
227     * Set the big number mode. This can be BIGNUMBER_ERROR(0), BIGNUMBER_POSIX(1) or
228     * BIGNUMBER_STAR(2). This specifies the treatment of big files (sizes &gt;
229     * TarConstants.MAXSIZE) and other numeric values to big to fit into a traditional tar header.
230     * Default is BIGNUMBER_ERROR.
231     *
232     * @param bigNumberMode the mode to use
233     * @since 1.4
234     */
235    public void setBigNumberMode(final int bigNumberMode) {
236        this.bigNumberMode = bigNumberMode;
237    }
238
239    /**
240     * Whether to add a PAX extension header for non-ASCII file names.
241     *
242     * @param b whether to add a PAX extension header for non-ASCII file names.
243     * @since 1.4
244     */
245    public void setAddPaxHeadersForNonAsciiNames(final boolean b) {
246        addPaxHeadersForNonAsciiNames = b;
247    }
248
249    @Deprecated
250    @Override
251    public int getCount() {
252        return (int) getBytesWritten();
253    }
254
255    @Override
256    public long getBytesWritten() {
257        return countingOut.getBytesWritten();
258    }
259
260    /**
261     * Ends the TAR archive without closing the underlying OutputStream.
262     *
263     * An archive consists of a series of file entries terminated by an
264     * end-of-archive entry, which consists of two 512 blocks of zero bytes.
265     * POSIX.1 requires two EOF records, like some other implementations.
266     *
267     * @throws IOException on error
268     */
269    @Override
270    public void finish() throws IOException {
271        if (finished) {
272            throw new IOException("This archive has already been finished");
273        }
274
275        if (haveUnclosedEntry) {
276            throw new IOException("This archive contains unclosed entries.");
277        }
278        writeEOFRecord();
279        writeEOFRecord();
280        padAsNeeded();
281        out.flush();
282        finished = true;
283    }
284
285    /**
286     * Closes the underlying OutputStream.
287     *
288     * @throws IOException on error
289     */
290    @Override
291    public void close() throws IOException {
292        if (!finished) {
293            finish();
294        }
295
296        if (!closed) {
297            out.close();
298            closed = true;
299        }
300    }
301
302    /**
303     * Get the record size being used by this stream's TarBuffer.
304     *
305     * @return The TarBuffer record size.
306     * @deprecated
307     */
308    @Deprecated
309    public int getRecordSize() {
310        return RECORD_SIZE;
311    }
312
313    /**
314     * Put an entry on the output stream. This writes the entry's header record and positions the
315     * output stream for writing the contents of the entry. Once this method is called, the stream
316     * is ready for calls to write() to write the entry's contents. Once the contents are written,
317     * closeArchiveEntry() <B>MUST</B> be called to ensure that all buffered data is completely
318     * written to the output stream.
319     *
320     * @param archiveEntry The TarEntry to be written to the archive.
321     * @throws IOException on error
322     * @throws ClassCastException if archiveEntry is not an instance of TarArchiveEntry
323     */
324    @Override
325    public void putArchiveEntry(final ArchiveEntry archiveEntry) throws IOException {
326        if (finished) {
327            throw new IOException("Stream has already been finished");
328        }
329        final TarArchiveEntry entry = (TarArchiveEntry) archiveEntry;
330        if (entry.isGlobalPaxHeader()) {
331            final byte[] data = encodeExtendedPaxHeadersContents(entry.getExtraPaxHeaders());
332            entry.setSize(data.length);
333            entry.writeEntryHeader(recordBuf, zipEncoding, bigNumberMode == BIGNUMBER_STAR);
334            writeRecord(recordBuf);
335            currSize= entry.getSize();
336            currBytes = 0;
337            this.haveUnclosedEntry = true;
338            write(data);
339            closeArchiveEntry();
340        } else {
341            final Map<String, String> paxHeaders = new HashMap<>();
342            final String entryName = entry.getName();
343            final boolean paxHeaderContainsPath = handleLongName(entry, entryName, paxHeaders, "path",
344                TarConstants.LF_GNUTYPE_LONGNAME, "file name");
345
346            final String linkName = entry.getLinkName();
347            final boolean paxHeaderContainsLinkPath = linkName != null && linkName.length() > 0
348                && handleLongName(entry, linkName, paxHeaders, "linkpath",
349                TarConstants.LF_GNUTYPE_LONGLINK, "link name");
350
351            if (bigNumberMode == BIGNUMBER_POSIX) {
352                addPaxHeadersForBigNumbers(paxHeaders, entry);
353            } else if (bigNumberMode != BIGNUMBER_STAR) {
354                failForBigNumbers(entry);
355            }
356
357            if (addPaxHeadersForNonAsciiNames && !paxHeaderContainsPath
358                && !ASCII.canEncode(entryName)) {
359                paxHeaders.put("path", entryName);
360            }
361
362            if (addPaxHeadersForNonAsciiNames && !paxHeaderContainsLinkPath
363                && (entry.isLink() || entry.isSymbolicLink())
364                && !ASCII.canEncode(linkName)) {
365                paxHeaders.put("linkpath", linkName);
366            }
367            paxHeaders.putAll(entry.getExtraPaxHeaders());
368
369            if (paxHeaders.size() > 0) {
370                writePaxHeaders(entry, entryName, paxHeaders);
371            }
372
373            entry.writeEntryHeader(recordBuf, zipEncoding, bigNumberMode == BIGNUMBER_STAR);
374            writeRecord(recordBuf);
375
376            currBytes = 0;
377
378            if (entry.isDirectory()) {
379                currSize = 0;
380            } else {
381                currSize = entry.getSize();
382            }
383            currName = entryName;
384            haveUnclosedEntry = true;
385        }
386    }
387
388    /**
389     * Close an entry. This method MUST be called for all file entries that contain data. The reason
390     * is that we must buffer data written to the stream in order to satisfy the buffer's record
391     * based writes. Thus, there may be data fragments still being assembled that must be written to
392     * the output stream before this entry is closed and the next entry written.
393     *
394     * @throws IOException on error
395     */
396    @Override
397    public void closeArchiveEntry() throws IOException {
398        if (finished) {
399            throw new IOException("Stream has already been finished");
400        }
401        if (!haveUnclosedEntry) {
402            throw new IOException("No current entry to close");
403        }
404        out.flushBlock();
405        if (currBytes < currSize) {
406            throw new IOException("entry '" + currName + "' closed at '"
407                + currBytes
408                + "' before the '" + currSize
409                + "' bytes specified in the header were written");
410        }
411        recordsWritten += (currSize / RECORD_SIZE);
412        if (0 != currSize % RECORD_SIZE) {
413            recordsWritten++;
414        }
415        haveUnclosedEntry = false;
416    }
417
418    /**
419     * Writes bytes to the current tar archive entry. This method is aware of the current entry and
420     * will throw an exception if you attempt to write bytes past the length specified for the
421     * current entry.
422     *
423     * @param wBuf The buffer to write to the archive.
424     * @param wOffset The offset in the buffer from which to get bytes.
425     * @param numToWrite The number of bytes to write.
426     * @throws IOException on error
427     */
428    @Override
429    public void write(final byte[] wBuf, int wOffset, int numToWrite) throws IOException {
430        if (!haveUnclosedEntry) {
431            throw new IllegalStateException("No current tar entry");
432        }
433        if (currBytes + numToWrite > currSize) {
434            throw new IOException("request to write '" + numToWrite
435                + "' bytes exceeds size in header of '"
436                + currSize + "' bytes for entry '"
437                + currName + "'");
438        }
439        out.write(wBuf, wOffset, numToWrite);
440        currBytes += numToWrite;
441    }
442
443    /**
444     * Writes a PAX extended header with the given map as contents.
445     *
446     * @since 1.4
447     */
448    void writePaxHeaders(final TarArchiveEntry entry,
449        final String entryName,
450        final Map<String, String> headers) throws IOException {
451        String name = "./PaxHeaders.X/" + stripTo7Bits(entryName);
452        if (name.length() >= TarConstants.NAMELEN) {
453            name = name.substring(0, TarConstants.NAMELEN - 1);
454        }
455        final TarArchiveEntry pex = new TarArchiveEntry(name,
456            TarConstants.LF_PAX_EXTENDED_HEADER_LC);
457        transferModTime(entry, pex);
458
459        final byte[] data = encodeExtendedPaxHeadersContents(headers);
460        pex.setSize(data.length);
461        putArchiveEntry(pex);
462        write(data);
463        closeArchiveEntry();
464    }
465
466    private byte[] encodeExtendedPaxHeadersContents(Map<String, String> headers)
467        throws UnsupportedEncodingException {
468        final StringWriter w = new StringWriter();
469        for (final Map.Entry<String, String> h : headers.entrySet()) {
470            final String key = h.getKey();
471            final String value = h.getValue();
472            int len = key.length() + value.length()
473                + 3 /* blank, equals and newline */
474                + 2 /* guess 9 < actual length < 100 */;
475            String line = len + " " + key + "=" + value + "\n";
476            int actualLength = line.getBytes(CharsetNames.UTF_8).length;
477            while (len != actualLength) {
478                // Adjust for cases where length < 10 or > 100
479                // or where UTF-8 encoding isn't a single octet
480                // per character.
481                // Must be in loop as size may go from 99 to 100 in
482                // first pass so we'd need a second.
483                len = actualLength;
484                line = len + " " + key + "=" + value + "\n";
485                actualLength = line.getBytes(CharsetNames.UTF_8).length;
486            }
487            w.write(line);
488        }
489        return w.toString().getBytes(CharsetNames.UTF_8);
490    }
491
492    private String stripTo7Bits(final String name) {
493        final int length = name.length();
494        final StringBuilder result = new StringBuilder(length);
495        for (int i = 0; i < length; i++) {
496            final char stripped = (char) (name.charAt(i) & 0x7F);
497            if (shouldBeReplaced(stripped)) {
498                result.append("_");
499            } else {
500                result.append(stripped);
501            }
502        }
503        return result.toString();
504    }
505
506    /**
507     * @return true if the character could lead to problems when used inside a TarArchiveEntry name
508     * for a PAX header.
509     */
510    private boolean shouldBeReplaced(final char c) {
511        return c == 0 // would be read as Trailing null
512            || c == '/' // when used as last character TAE will consider the PAX header a directory
513            || c == '\\'; // same as '/' as slashes get "normalized" on Windows
514    }
515
516    /**
517     * Write an EOF (end of archive) record to the tar archive. An EOF record consists of a record
518     * of all zeros.
519     */
520    private void writeEOFRecord() throws IOException {
521        Arrays.fill(recordBuf, (byte) 0);
522        writeRecord(recordBuf);
523    }
524
525    @Override
526    public void flush() throws IOException {
527        out.flush();
528    }
529
530    @Override
531    public ArchiveEntry createArchiveEntry(final File inputFile, final String entryName)
532        throws IOException {
533        if (finished) {
534            throw new IOException("Stream has already been finished");
535        }
536        return new TarArchiveEntry(inputFile, entryName);
537    }
538
539    /**
540     * Write an archive record to the archive.
541     *
542     * @param record The record data to write to the archive.
543     * @throws IOException on error
544     */
545    private void writeRecord(final byte[] record) throws IOException {
546        if (record.length != RECORD_SIZE) {
547            throw new IOException("record to write has length '"
548                + record.length
549                + "' which is not the record size of '"
550                + RECORD_SIZE + "'");
551        }
552
553        out.write(record);
554        recordsWritten++;
555    }
556
557    private void padAsNeeded() throws IOException {
558        final int start = recordsWritten % recordsPerBlock;
559        if (start != 0) {
560            for (int i = start; i < recordsPerBlock; i++) {
561                writeEOFRecord();
562            }
563        }
564    }
565
566    private void addPaxHeadersForBigNumbers(final Map<String, String> paxHeaders,
567        final TarArchiveEntry entry) {
568        addPaxHeaderForBigNumber(paxHeaders, "size", entry.getSize(),
569            TarConstants.MAXSIZE);
570        addPaxHeaderForBigNumber(paxHeaders, "gid", entry.getLongGroupId(),
571            TarConstants.MAXID);
572        addPaxHeaderForBigNumber(paxHeaders, "mtime",
573            entry.getModTime().getTime() / 1000,
574            TarConstants.MAXSIZE);
575        addPaxHeaderForBigNumber(paxHeaders, "uid", entry.getLongUserId(),
576            TarConstants.MAXID);
577        // star extensions by J\u00f6rg Schilling
578        addPaxHeaderForBigNumber(paxHeaders, "SCHILY.devmajor",
579            entry.getDevMajor(), TarConstants.MAXID);
580        addPaxHeaderForBigNumber(paxHeaders, "SCHILY.devminor",
581            entry.getDevMinor(), TarConstants.MAXID);
582        // there is no PAX header for file mode
583        failForBigNumber("mode", entry.getMode(), TarConstants.MAXID);
584    }
585
586    private void addPaxHeaderForBigNumber(final Map<String, String> paxHeaders,
587        final String header, final long value,
588        final long maxValue) {
589        if (value < 0 || value > maxValue) {
590            paxHeaders.put(header, String.valueOf(value));
591        }
592    }
593
594    private void failForBigNumbers(final TarArchiveEntry entry) {
595        failForBigNumber("entry size", entry.getSize(), TarConstants.MAXSIZE);
596        failForBigNumberWithPosixMessage("group id", entry.getLongGroupId(), TarConstants.MAXID);
597        failForBigNumber("last modification time",
598            entry.getModTime().getTime() / 1000,
599            TarConstants.MAXSIZE);
600        failForBigNumber("user id", entry.getLongUserId(), TarConstants.MAXID);
601        failForBigNumber("mode", entry.getMode(), TarConstants.MAXID);
602        failForBigNumber("major device number", entry.getDevMajor(),
603            TarConstants.MAXID);
604        failForBigNumber("minor device number", entry.getDevMinor(),
605            TarConstants.MAXID);
606    }
607
608    private void failForBigNumber(final String field, final long value, final long maxValue) {
609        failForBigNumber(field, value, maxValue, "");
610    }
611
612    private void failForBigNumberWithPosixMessage(final String field, final long value,
613        final long maxValue) {
614        failForBigNumber(field, value, maxValue,
615            " Use STAR or POSIX extensions to overcome this limit");
616    }
617
618    private void failForBigNumber(final String field, final long value, final long maxValue,
619        final String additionalMsg) {
620        if (value < 0 || value > maxValue) {
621            throw new RuntimeException(field + " '" + value //NOSONAR
622                + "' is too big ( > "
623                + maxValue + " )." + additionalMsg);
624        }
625    }
626
627    /**
628     * Handles long file or link names according to the longFileMode setting.
629     *
630     * <p>I.e. if the given name is too long to be written to a plain tar header then <ul> <li>it
631     * creates a pax header who's name is given by the paxHeaderName parameter if longFileMode is
632     * POSIX</li> <li>it creates a GNU longlink entry who's type is given by the linkType parameter
633     * if longFileMode is GNU</li> <li>it throws an exception if longFileMode is ERROR</li> <li>it
634     * truncates the name if longFileMode is TRUNCATE</li> </ul></p>
635     *
636     * @param entry entry the name belongs to
637     * @param name the name to write
638     * @param paxHeaders current map of pax headers
639     * @param paxHeaderName name of the pax header to write
640     * @param linkType type of the GNU entry to write
641     * @param fieldName the name of the field
642     * @return whether a pax header has been written.
643     */
644    private boolean handleLongName(final TarArchiveEntry entry, final String name,
645        final Map<String, String> paxHeaders,
646        final String paxHeaderName, final byte linkType, final String fieldName)
647        throws IOException {
648        final ByteBuffer encodedName = zipEncoding.encode(name);
649        final int len = encodedName.limit() - encodedName.position();
650        if (len >= TarConstants.NAMELEN) {
651
652            if (longFileMode == LONGFILE_POSIX) {
653                paxHeaders.put(paxHeaderName, name);
654                return true;
655            } else if (longFileMode == LONGFILE_GNU) {
656                // create a TarEntry for the LongLink, the contents
657                // of which are the link's name
658                final TarArchiveEntry longLinkEntry = new TarArchiveEntry(TarConstants.GNU_LONGLINK,
659                    linkType);
660
661                longLinkEntry.setSize(len + 1l); // +1 for NUL
662                transferModTime(entry, longLinkEntry);
663                putArchiveEntry(longLinkEntry);
664                write(encodedName.array(), encodedName.arrayOffset(), len);
665                write(0); // NUL terminator
666                closeArchiveEntry();
667            } else if (longFileMode != LONGFILE_TRUNCATE) {
668                throw new RuntimeException(fieldName + " '" + name //NOSONAR
669                    + "' is too long ( > "
670                    + TarConstants.NAMELEN + " bytes)");
671            }
672        }
673        return false;
674    }
675
676    private void transferModTime(final TarArchiveEntry from, final TarArchiveEntry to) {
677        Date fromModTime = from.getModTime();
678        final long fromModTimeSeconds = fromModTime.getTime() / 1000;
679        if (fromModTimeSeconds < 0 || fromModTimeSeconds > TarConstants.MAXSIZE) {
680            fromModTime = new Date(0);
681        }
682        to.setModTime(fromModTime);
683    }
684}