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

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

Introduction

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

Prototype

int DEFAULT_DIR_MODE

To view the source code for org.apache.commons.compress.archivers.tar TarArchiveEntry DEFAULT_DIR_MODE.

Click Source Link

Document

Default permissions bits for directories

Usage

From source file:com.mobilesorcery.sdk.builder.linux.deb.DebBuilder.java

/**
 * Adds a new file to package//w ww .  j a va 2  s .c om
 *
 * @param p Path to file
 */
public void addFile(String p) throws IOException, FileNotFoundException, NoSuchAlgorithmException {
    File f = new File(p);
    int m = TarArchiveEntry.DEFAULT_FILE_MODE;
    if (f.isDirectory() == true)
        m = TarArchiveEntry.DEFAULT_DIR_MODE;

    addFile(p, f, m);
}

From source file:com.mobilesorcery.sdk.builder.linux.deb.DebBuilder.java

/**
 * Adds a new file to package/*from  ww w. j a v  a2 s  .  c  o m*/
 *
 * @param f The actual file to add
 * @param p Path to file
 */
public void addFile(String p, File f) throws IOException, FileNotFoundException, NoSuchAlgorithmException {
    int m = TarArchiveEntry.DEFAULT_FILE_MODE;
    if (f.isDirectory() == true)
        m = TarArchiveEntry.DEFAULT_DIR_MODE;

    addFile(p, f, m);
}

From source file:consulo.cold.runner.execute.target.artifacts.Generator.java

private static int extractMode(ArchiveEntry entry) {
    if (entry instanceof TarArchiveEntry) {
        return ((TarArchiveEntry) entry).getMode();
    }/*from  w w  w  .j ava  2s  . c  o  m*/
    return entry.isDirectory() ? TarArchiveEntry.DEFAULT_DIR_MODE : TarArchiveEntry.DEFAULT_FILE_MODE;
}

From source file:org.ngrinder.common.util.CompressionUtils.java

/**
 * Add a file into tar./* ww w  .  jav  a  2  s. c o  m*/
 *
 * @param tarStream TarArchive outputStream
 * @param file      file
 * @param path      relative path to append
 * @throws IOException thrown when having IO problem.
 */
public static void addFileToTar(TarArchiveOutputStream tarStream, File file, String path) throws IOException {
    int mode = file.isDirectory() ? TarArchiveEntry.DEFAULT_DIR_MODE : TarArchiveEntry.DEFAULT_FILE_MODE;
    addFileToTar(tarStream, file, path, mode);
}

From source file:org.ngrinder.common.util.CompressionUtils.java

/**
 * Add a folder into tar./*  w  w w  .  ja v  a2s.com*/
 *
 * @param tarStream TarArchive outputStream
 * @param path      path to append
 * @throws IOException thrown when having IO problem.
 */
public static void addFolderToTar(TarArchiveOutputStream tarStream, String path) throws IOException {
    TarArchiveEntry archiveEntry = new TarArchiveEntry(path);
    archiveEntry.setMode(TarArchiveEntry.DEFAULT_DIR_MODE);
    tarStream.putArchiveEntry(archiveEntry);
    tarStream.closeArchiveEntry();
}

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

/**
 * Build the data archive of the deb from the provided DataProducers
 *
 * @param producers//from ww w  .  ja v a 2 s. co  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.producers.DataProducerDirectory.java

public void produce(final DataConsumer pReceiver) throws IOException {

    scanner.scan();//from   www. jav  a  2 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.DataProducerPathTemplate.java

public void produce(DataConsumer pReceiver) throws IOException {
    for (String literalPath : literalPaths) {
        TarArchiveEntry entry = new TarArchiveEntry(literalPath, true);
        entry.setUserId(0);/*from   ww w  .ja v a  2  s . co  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.DataProducerPathTemplateTestCase.java

public void testTypical() throws Exception {

    String[] paths = { "/var/log", "/var/lib" };
    dataProducer = new DataProducerPathTemplate(paths, INCLUDES, EXCLUDES, mappers);
    dataProducer.produce(captureDataConsumer);

    assertEquals(2, captureDataConsumer.invocations.size());

    CaptureDataConsumer.Invocation invocation = captureDataConsumer.invocations.get(0);
    assertEquals(invocation.dirname, "/var/log");
    assertEquals(invocation.gid, 0);//from   w ww .  j  a va  2  s. co m
    assertEquals(invocation.group, "root");
    assertEquals(invocation.linkname, "");
    assertEquals(invocation.mode, TarArchiveEntry.DEFAULT_DIR_MODE);
    assertEquals(invocation.size, 0);
    assertEquals(invocation.uid, 0);
    assertEquals(invocation.user, "root");

    invocation = captureDataConsumer.invocations.get(1);
    assertEquals(invocation.dirname, "/var/lib");
    assertEquals(invocation.gid, 0);
    assertEquals(invocation.group, "root");
    assertEquals(invocation.linkname, "");
    assertEquals(invocation.mode, TarArchiveEntry.DEFAULT_DIR_MODE);
    assertEquals(invocation.size, 0);
    assertEquals(invocation.uid, 0);
    assertEquals(invocation.user, "root");
}

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

/**
 * Creates a tar directory entry with defaults parameters.
 * @param dirName the directory name/*  w w  w  .  j av  a  2  s .  c om*/
 * @return dir entry with reasonable defaults
 */
static TarArchiveEntry defaultDirEntryWithName(final String dirName) {
    TarArchiveEntry entry = new TarArchiveEntry(dirName, true);
    entry.setUserId(ROOT_UID);
    entry.setUserName(ROOT_NAME);
    entry.setGroupId(ROOT_UID);
    entry.setGroupName(ROOT_NAME);
    entry.setMode(TarArchiveEntry.DEFAULT_DIR_MODE);
    return entry;
}