protected void tarResource()

in src/main/org/apache/tools/ant/taskdefs/Tar.java [390:524]


    protected void tarResource(final Resource r, final TarOutputStream tOut, String vPath,
                               final TarFileSet tarFileSet)
        throws IOException {

        if (!r.isExists()) {
            return;
        }

        boolean preserveLeadingSlashes = false;

        if (tarFileSet != null) {
            final String fullpath = tarFileSet.getFullpath(this.getProject());
            if (fullpath.isEmpty()) {
                // don't add "" to the archive
                if (vPath.isEmpty()) {
                    return;
                }

                vPath = getCanonicalPrefix(tarFileSet, this.getProject()) + vPath;
            } else {
                vPath = fullpath;
            }

            preserveLeadingSlashes = tarFileSet.getPreserveLeadingSlashes();

            if (vPath.startsWith("/") && !preserveLeadingSlashes) {
                final int l = vPath.length();
                if (l <= 1) {
                    // we would end up adding "" to the archive
                    return;
                }
                vPath = vPath.substring(1, l);
            }
        }

        if (r.isDirectory() && !vPath.endsWith("/")) {
            vPath += "/";
        }

        if (vPath.length() >= TarConstants.NAMELEN) {
            if (longFileMode.isOmitMode()) {
                log("Omitting: " + vPath, Project.MSG_INFO);
                return;
            } else if (longFileMode.isWarnMode()) {
                log("Entry: " + vPath + " longer than "
                    + TarConstants.NAMELEN + " characters.",
                    Project.MSG_WARN);
                if (!longWarningGiven) {
                    log("Resulting tar file can only be processed "
                        + "successfully by GNU compatible tar commands",
                        Project.MSG_WARN);
                    longWarningGiven = true;
                }
            } else if (longFileMode.isFailMode()) {
                throw new BuildException("Entry: " + vPath
                        + " longer than " + TarConstants.NAMELEN
                        + "characters.", getLocation());
            }
        }

        final TarEntry te = new TarEntry(vPath, preserveLeadingSlashes);
        te.setModTime(r.getLastModified());
        // preserve permissions
        if (r instanceof ArchiveResource) {
            final ArchiveResource ar = (ArchiveResource) r;
            te.setMode(ar.getMode());
            if (r instanceof TarResource) {
                final TarResource tr = (TarResource) r;
                te.setUserName(tr.getUserName());
                te.setUserId(tr.getLongUid());
                te.setGroupName(tr.getGroup());
                te.setGroupId(tr.getLongGid());
                String linkName = tr.getLinkName();
                byte linkFlag = tr.getLinkFlag();
                if (linkFlag == TarConstants.LF_LINK &&
                    linkName != null && linkName.length() > 0 && !linkName.startsWith("/")) {
                    linkName = getCanonicalPrefix(tarFileSet, this.getProject()) + linkName;
                }
                te.setLinkName(linkName);
                te.setLinkFlag(linkFlag);
            }
        }

        if (!r.isDirectory()) {
            if (r.size() > TarConstants.MAXSIZE) {
                throw new BuildException(
                    "Resource: " + r + " larger than "
                    + TarConstants.MAXSIZE + " bytes.");
            }
            te.setSize(r.getSize());
            // override permissions if set explicitly
            if (tarFileSet != null && tarFileSet.hasFileModeBeenSet()) {
                te.setMode(tarFileSet.getMode());
            }
        } else if (tarFileSet != null && tarFileSet.hasDirModeBeenSet()) {
            // override permissions if set explicitly
            te.setMode(tarFileSet.getDirMode(this.getProject()));
        }

        if (tarFileSet != null) {
            // only override permissions if set explicitly
            if (tarFileSet.hasUserNameBeenSet()) {
                te.setUserName(tarFileSet.getUserName());
            }
            if (tarFileSet.hasGroupBeenSet()) {
                te.setGroupName(tarFileSet.getGroup());
            }
            if (tarFileSet.hasUserIdBeenSet()) {
                te.setUserId(tarFileSet.getUid());
            }
            if (tarFileSet.hasGroupIdBeenSet()) {
                te.setGroupId(tarFileSet.getGid());
            }
        }

        InputStream in = null;
        try {
            tOut.putNextEntry(te);

            if (!r.isDirectory()) {
                in = r.getInputStream();

                final byte[] buffer = new byte[BUFFER_SIZE];
                int count = 0;
                do {
                    tOut.write(buffer, 0, count);
                    count = in.read(buffer, 0, buffer.length);
                } while (count != -1);
            }

            tOut.closeEntry();
        } finally {
            FileUtils.close(in);
        }
    }