Example usage for org.apache.commons.compress.archivers.tar TarArchiveEntry getGroupId

List of usage examples for org.apache.commons.compress.archivers.tar TarArchiveEntry getGroupId

Introduction

In this page you can find the example usage for org.apache.commons.compress.archivers.tar TarArchiveEntry getGroupId.

Prototype

public int getGroupId() 

Source Link

Document

Get this entry's group id.

Usage

From source file:org.fabrician.maven.plugins.CompressUtils.java

private static ArchiveEntry createArchiveEntry(ArchiveEntry entry, OutputStream out, String alternateBaseDir)
        throws IOException {
    String substitutedName = substituteAlternateBaseDir(entry, alternateBaseDir);
    if (out instanceof TarArchiveOutputStream) {
        TarArchiveEntry newEntry = new TarArchiveEntry(substitutedName);
        newEntry.setSize(entry.getSize());
        newEntry.setModTime(entry.getLastModifiedDate());

        if (entry instanceof TarArchiveEntry) {
            TarArchiveEntry old = (TarArchiveEntry) entry;
            newEntry.setSize(old.getSize());
            newEntry.setIds(old.getUserId(), old.getGroupId());
            newEntry.setNames(old.getUserName(), old.getGroupName());
        }/* www . j av a 2 s .c om*/
        return newEntry;
    } else if (entry instanceof ZipArchiveEntry) {
        ZipArchiveEntry old = (ZipArchiveEntry) entry;
        ZipArchiveEntry zip = new ZipArchiveEntry(substitutedName);
        zip.setInternalAttributes(old.getInternalAttributes());
        zip.setExternalAttributes(old.getExternalAttributes());
        zip.setExtraFields(old.getExtraFields(true));
        return zip;
    } else {
        return new ZipArchiveEntry(substitutedName);
    }
}

From source file:org.vafer.jdeb.DataBuilder.java

/**
 * Build the data archive of the deb from the provided DataProducers
 *
 * @param producers//from  w w w  . ja v  a 2s  . c  o  m
 * @param output
 * @param checksums
 * @param compression the compression method used for the data file
 * @return
 * @throws java.security.NoSuchAlgorithmException
 * @throws java.io.IOException
 * @throws org.apache.commons.compress.compressors.CompressorException
 */
BigInteger buildData(Collection<DataProducer> producers, File output, final StringBuilder checksums,
        Compression compression) throws NoSuchAlgorithmException, IOException, CompressorException {

    final File dir = output.getParentFile();
    if (dir != null && (!dir.exists() || !dir.isDirectory())) {
        throw new IOException("Cannot write data file at '" + output.getAbsolutePath() + "'");
    }

    final TarArchiveOutputStream tarOutputStream = new TarArchiveOutputStream(
            compression.toCompressedOutputStream(new FileOutputStream(output)));
    tarOutputStream.setLongFileMode(TarArchiveOutputStream.LONGFILE_GNU);

    final MessageDigest digest = MessageDigest.getInstance("MD5");

    final Total dataSize = new Total();

    final List<String> addedDirectories = new ArrayList<String>();
    final DataConsumer receiver = new DataConsumer() {
        public void onEachDir(String dirname, String linkname, String user, int uid, String group, int gid,
                int mode, long size) throws IOException {
            dirname = fixPath(dirname);

            createParentDirectories(dirname, user, uid, group, gid);

            // The directory passed in explicitly by the caller also gets the passed-in mode.  (Unlike
            // the parent directories for now.  See related comments at "int mode =" in
            // createParentDirectories, including about a possible bug.)
            createDirectory(dirname, user, uid, group, gid, mode, 0);

            console.info("dir: " + dirname);
        }

        public void onEachFile(InputStream inputStream, String filename, String linkname, String user, int uid,
                String group, int gid, int mode, long size) throws IOException {
            filename = fixPath(filename);

            createParentDirectories(filename, user, uid, group, gid);

            final TarArchiveEntry entry = new TarArchiveEntry(filename, true);

            entry.setUserName(user);
            entry.setUserId(uid);
            entry.setGroupName(group);
            entry.setGroupId(gid);
            entry.setMode(mode);
            entry.setSize(size);

            tarOutputStream.putArchiveEntry(entry);

            dataSize.add(size);
            digest.reset();

            Utils.copy(inputStream, new DigestOutputStream(tarOutputStream, digest));

            final String md5 = Utils.toHex(digest.digest());

            tarOutputStream.closeArchiveEntry();

            console.info("file:" + entry.getName() + " size:" + entry.getSize() + " mode:" + entry.getMode()
                    + " linkname:" + entry.getLinkName() + " username:" + entry.getUserName() + " userid:"
                    + entry.getUserId() + " groupname:" + entry.getGroupName() + " groupid:"
                    + entry.getGroupId() + " modtime:" + entry.getModTime() + " md5: " + md5);

            // append to file md5 list
            checksums.append(md5).append(" ").append(entry.getName()).append('\n');
        }

        public void onEachLink(String path, String linkName, boolean symlink, String user, int uid,
                String group, int gid, int mode) throws IOException {
            path = fixPath(path);

            createParentDirectories(path, user, uid, group, gid);

            final TarArchiveEntry entry = new TarArchiveEntry(path,
                    symlink ? TarArchiveEntry.LF_SYMLINK : TarArchiveEntry.LF_LINK);
            entry.setLinkName(linkName);

            entry.setUserName(user);
            entry.setUserId(uid);
            entry.setGroupName(group);
            entry.setGroupId(gid);
            entry.setMode(mode);

            tarOutputStream.putArchiveEntry(entry);
            tarOutputStream.closeArchiveEntry();

            console.info("link:" + entry.getName() + " mode:" + entry.getMode() + " linkname:"
                    + entry.getLinkName() + " username:" + entry.getUserName() + " userid:" + entry.getUserId()
                    + " groupname:" + entry.getGroupName() + " groupid:" + entry.getGroupId());
        }

        private void createDirectory(String directory, String user, int uid, String group, int gid, int mode,
                long size) throws IOException {
            // All dirs should end with "/" when created, or the test DebAndTaskTestCase.testTarFileSet() thinks its a file
            // and so thinks it has the wrong permission.
            // This consistency also helps when checking if a directory already exists in addedDirectories.

            if (!directory.endsWith("/")) {
                directory += "/";
            }

            if (!addedDirectories.contains(directory)) {
                TarArchiveEntry entry = new TarArchiveEntry(directory, true);
                entry.setUserName(user);
                entry.setUserId(uid);
                entry.setGroupName(group);
                entry.setGroupId(gid);
                entry.setMode(mode);
                entry.setSize(size);

                tarOutputStream.putArchiveEntry(entry);
                tarOutputStream.closeArchiveEntry();
                addedDirectories.add(directory); // so addedDirectories consistently have "/" for finding duplicates.
            }
        }

        private void createParentDirectories(String filename, String user, int uid, String group, int gid)
                throws IOException {
            String dirname = fixPath(new File(filename).getParent());

            // Debian packages must have parent directories created
            // before sub-directories or files can be installed.
            // For example, if an entry of ./usr/lib/foo/bar existed
            // in a .deb package, but the ./usr/lib/foo directory didn't
            // exist, the package installation would fail.  The .deb must
            // then have an entry for ./usr/lib/foo and then ./usr/lib/foo/bar

            if (dirname == null) {
                return;
            }

            // The loop below will create entries for all parent directories
            // to ensure that .deb packages will install correctly.
            String[] pathParts = dirname.split("/");
            String parentDir = "./";
            for (int i = 1; i < pathParts.length; i++) {
                parentDir += pathParts[i] + "/";
                // Make it so the dirs can be traversed by users.
                // We could instead try something more granular, like setting the directory
                // permission to 'rx' for each of the 3 user/group/other read permissions
                // found on the file being added (ie, only if "other" has read
                // permission on the main node, then add o+rx permission on all the containing
                // directories, same w/ user & group), and then also we'd have to
                // check the parentDirs collection of those already added to
                // see if those permissions need to be similarly updated.  (Note, it hasn't
                // been demonstrated, but there might be a bug if a user specifically
                // requests a directory with certain permissions,
                // that has already been auto-created because it was a parent, and if so, go set
                // the user-requested mode on that directory instead of this automatic one.)
                // But for now, keeping it simple by making every dir a+rx.   Examples are:
                // drw-r----- fs/fs   # what you get with setMode(mode)
                // drwxr-xr-x fs/fs   # Usable. Too loose?
                int mode = TarArchiveEntry.DEFAULT_DIR_MODE;

                createDirectory(parentDir, user, uid, group, gid, mode, 0);
            }
        }
    };

    try {
        for (DataProducer data : producers) {
            data.produce(receiver);
        }
    } finally {
        tarOutputStream.close();
    }

    console.info("Total size: " + dataSize);

    return dataSize.count;
}

From source file:org.vafer.jdeb.mapping.LsMapper.java

public TarArchiveEntry map(final TarArchiveEntry pEntry) {

    final TarArchiveEntry entry = mapping.get(pEntry.getName());

    if (entry != null) {

        final TarArchiveEntry newEntry = new TarArchiveEntry(entry.getName(), true);
        newEntry.setUserId(entry.getUserId());
        newEntry.setGroupId(entry.getGroupId());
        newEntry.setUserName(entry.getUserName());
        newEntry.setGroupName(entry.getGroupName());
        newEntry.setMode(entry.getMode());
        newEntry.setSize(entry.getSize());

        return newEntry;
    }//ww  w. ja v a 2 s  . c om

    return pEntry;
}

From source file:org.vafer.jdeb.mapping.PermMapper.java

public TarArchiveEntry map(final TarArchiveEntry entry) {
    final String name = entry.getName();

    final TarArchiveEntry newEntry = new TarArchiveEntry(prefix + '/' + Utils.stripPath(strip, name), true);

    // Set ownership
    if (uid > -1) {
        newEntry.setUserId(uid);//from  w w  w . j a v  a2  s .c o m
    } else {
        newEntry.setUserId(entry.getUserId());
    }
    if (gid > -1) {
        newEntry.setGroupId(gid);
    } else {
        newEntry.setGroupId(entry.getGroupId());
    }
    if (user != null) {
        newEntry.setUserName(user);
    } else {
        newEntry.setUserName(entry.getUserName());
    }
    if (group != null) {
        newEntry.setGroupName(group);
    } else {
        newEntry.setGroupName(entry.getGroupName());
    }

    // Set permissions
    if (newEntry.isDirectory()) {
        if (dirMode > -1) {
            newEntry.setMode(dirMode);
        } else {
            newEntry.setMode(entry.getMode());
        }
    } else {
        if (fileMode > -1) {
            newEntry.setMode(fileMode);
        } else {
            newEntry.setMode(entry.getMode());

        }
    }

    newEntry.setSize(entry.getSize());

    return newEntry;
}

From source file:org.vafer.jdeb.producers.DataProducerArchive.java

public void produce(final DataConsumer pReceiver) throws IOException {

    InputStream is = new BufferedInputStream(new FileInputStream(archive));

    CompressorInputStream compressorInputStream = null;

    try {/*from   w  w  w  .ja v a2 s  .  c om*/
        compressorInputStream = new CompressorStreamFactory().createCompressorInputStream(is);
    } catch (CompressorException e) {
        // expected if the input file is a zip archive
    }

    if (compressorInputStream != null) {
        is = new BufferedInputStream(compressorInputStream);
    }

    ArchiveInputStream archiveInputStream = null;

    try {
        archiveInputStream = new ArchiveStreamFactory().createArchiveInputStream(is);
    } catch (ArchiveException e) {
        throw new IOException("Unsupported archive format: " + archive, e);
    }

    EntryConverter converter = null;

    if (archiveInputStream instanceof TarArchiveInputStream) {

        converter = new EntryConverter() {
            public TarArchiveEntry convert(ArchiveEntry entry) {
                TarArchiveEntry src = (TarArchiveEntry) entry;
                TarArchiveEntry dst = new TarArchiveEntry(src.getName(), true);

                dst.setSize(src.getSize());
                dst.setGroupName(src.getGroupName());
                dst.setGroupId(src.getGroupId());
                dst.setUserId(src.getUserId());
                dst.setMode(src.getMode());
                dst.setModTime(src.getModTime());

                return dst;
            }
        };

    } else if (archiveInputStream instanceof ZipArchiveInputStream) {

        converter = new EntryConverter() {
            public TarArchiveEntry convert(ArchiveEntry entry) {
                ZipArchiveEntry src = (ZipArchiveEntry) entry;
                TarArchiveEntry dst = new TarArchiveEntry(src.getName(), true);

                dst.setSize(src.getSize());
                dst.setMode(src.getUnixMode());
                dst.setModTime(src.getTime());

                return dst;
            }
        };

    } else {
        throw new IOException("Unsupported archive format: " + archive);
    }

    try {
        while (true) {

            ArchiveEntry archiveEntry = archiveInputStream.getNextEntry();

            if (archiveEntry == null) {
                break;
            }

            if (!isIncluded(archiveEntry.getName())) {
                continue;
            }

            TarArchiveEntry entry = converter.convert(archiveEntry);

            entry = map(entry);

            if (entry.isDirectory()) {
                pReceiver.onEachDir(entry.getName(), entry.getLinkName(), entry.getUserName(),
                        entry.getUserId(), entry.getGroupName(), entry.getGroupId(), entry.getMode(),
                        entry.getSize());
                continue;
            }
            pReceiver.onEachFile(archiveInputStream, entry.getName(), entry.getLinkName(), entry.getUserName(),
                    entry.getUserId(), entry.getGroupName(), entry.getGroupId(), entry.getMode(),
                    entry.getSize());
        }

    } finally {
        if (archiveInputStream != null) {
            archiveInputStream.close();
        }
    }
}

From source file:org.vafer.jdeb.producers.DataProducerDirectory.java

public void produce(final DataConsumer pReceiver) throws IOException {

    scanner.scan();//from ww w.  j  a  v  a2  s . com

    final File baseDir = scanner.getBasedir();

    for (String dir : scanner.getIncludedDirectories()) {
        final File file = new File(baseDir, dir);
        String dirname = getFilename(baseDir, file);

        if ("".equals(dirname)) {
            continue;
        }

        if (!isIncluded(dirname)) {
            continue;
        }

        if ('/' != File.separatorChar) {
            dirname = dirname.replace(File.separatorChar, '/');
        }

        if (!dirname.endsWith("/")) {
            dirname += "/";
        }

        TarArchiveEntry entry = new TarArchiveEntry(dirname, true);
        entry.setUserId(0);
        entry.setUserName("root");
        entry.setGroupId(0);
        entry.setGroupName("root");
        entry.setMode(TarArchiveEntry.DEFAULT_DIR_MODE);

        entry = map(entry);

        entry.setSize(0);

        pReceiver.onEachDir(entry.getName(), entry.getLinkName(), entry.getUserName(), entry.getUserId(),
                entry.getGroupName(), entry.getGroupId(), entry.getMode(), entry.getSize());
    }

    for (String f : scanner.getIncludedFiles()) {
        final File file = new File(baseDir, f);
        String filename = getFilename(baseDir, file);

        if (!isIncluded(filename)) {
            continue;
        }

        if ('/' != File.separatorChar) {
            filename = filename.replace(File.separatorChar, '/');
        }

        TarArchiveEntry entry = new TarArchiveEntry(filename, true);
        entry.setUserId(0);
        entry.setUserName("root");
        entry.setGroupId(0);
        entry.setGroupName("root");
        entry.setMode(TarArchiveEntry.DEFAULT_FILE_MODE);

        entry = map(entry);

        entry.setSize(file.length());

        final InputStream inputStream = new FileInputStream(file);
        try {
            pReceiver.onEachFile(inputStream, entry.getName(), entry.getLinkName(), entry.getUserName(),
                    entry.getUserId(), entry.getGroupName(), entry.getGroupId(), entry.getMode(),
                    entry.getSize());
        } finally {
            inputStream.close();
        }
    }
}

From source file:org.vafer.jdeb.producers.DataProducerFile.java

public void produce(final DataConsumer pReceiver) throws IOException {
    String fileName;/*from w  w w . jav  a 2  s  .com*/
    if (destinationName != null && destinationName.trim().length() > 0) {
        fileName = destinationName.trim();
    } else {
        fileName = file.getName();
    }

    TarArchiveEntry entry = new TarArchiveEntry(fileName, true);
    entry.setUserId(0);
    entry.setUserName("root");
    entry.setGroupId(0);
    entry.setGroupName("root");
    entry.setMode(TarArchiveEntry.DEFAULT_FILE_MODE);

    entry = map(entry);

    entry.setSize(file.length());

    final InputStream inputStream = new FileInputStream(file);
    try {
        pReceiver.onEachFile(inputStream, entry.getName(), entry.getLinkName(), entry.getUserName(),
                entry.getUserId(), entry.getGroupName(), entry.getGroupId(), entry.getMode(), entry.getSize());
    } finally {
        inputStream.close();
    }

}

From source file:org.vafer.jdeb.producers.DataProducerLink.java

@Override
public void produce(final DataConsumer pReceiver) throws IOException {
    TarArchiveEntry entry = new TarArchiveEntry(path,
            symlink ? TarArchiveEntry.LF_SYMLINK : TarArchiveEntry.LF_LINK);
    entry.setLinkName(linkName);//w ww .ja v  a 2  s.  c o m

    entry.setUserId(0);
    entry.setUserName("root");
    entry.setGroupId(0);
    entry.setGroupName("root");
    entry.setMode(TarArchiveEntry.DEFAULT_FILE_MODE);

    entry = map(entry);

    pReceiver.onEachLink(path, linkName, symlink, entry.getUserName(), entry.getUserId(), entry.getGroupName(),
            entry.getGroupId(), entry.getMode());
}

From source file:org.vafer.jdeb.producers.DataProducerPathTemplate.java

public void produce(DataConsumer pReceiver) throws IOException {
    for (String literalPath : literalPaths) {
        TarArchiveEntry entry = new TarArchiveEntry(literalPath, true);
        entry.setUserId(0);/*from w w w. ja v a 2  s .c  o  m*/
        entry.setUserName("root");
        entry.setGroupId(0);
        entry.setGroupName("root");
        entry.setMode(TarArchiveEntry.DEFAULT_DIR_MODE);

        entry = map(entry);

        entry.setSize(0);

        pReceiver.onEachDir(entry.getName(), entry.getLinkName(), entry.getUserName(), entry.getUserId(),
                entry.getGroupName(), entry.getGroupId(), entry.getMode(), entry.getSize());
    }
}

From source file:org.vafer.jdeb.producers.Producers.java

/**
 * Forwards tar archive entry entry to a consumer.
 * @param consumer the consumer/*from  w  w  w.j a  v  a2  s  .c o  m*/
 * @param entry the entry to pass
 * @throws IOException
 */
static void produceDirEntry(final DataConsumer consumer, final TarArchiveEntry entry) throws IOException {
    consumer.onEachDir(entry.getName(), entry.getLinkName(), entry.getUserName(), entry.getUserId(),
            entry.getGroupName(), entry.getGroupId(), entry.getMode(), entry.getSize());
}