List of usage examples for org.apache.commons.compress.archivers.tar TarArchiveEntry getMode
public int getMode()
From source file:org.vafer.jdeb.DataBuilder.java
/** * Build the data archive of the deb from the provided DataProducers * * @param producers/*from w ww. j av a2 s. 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.DebMakerTestCase.java
public void testControlFilesPermissions() throws Exception { File deb = new File("target/test-classes/test-control.deb"); if (deb.exists() && !deb.delete()) { fail("Couldn't delete " + deb); }//from w w w. j a va 2s. c om Collection<DataProducer> producers = Arrays.asList(new DataProducer[] { new EmptyDataProducer() }); DebMaker maker = new DebMaker(new NullConsole(), producers); maker.setDeb(deb); maker.setControl(new File("target/test-classes/org/vafer/jdeb/deb/control")); maker.createDeb(Compression.NONE); // now reopen the package and check the control files assertTrue("package not build", deb.exists()); boolean found = ArchiveWalker.walkControl(deb, new ArchiveVisitor<TarArchiveEntry>() { public void visit(TarArchiveEntry entry, byte[] content) throws IOException { assertFalse("directory found in the control archive", entry.isDirectory()); assertTrue("prefix", entry.getName().startsWith("./")); InformationInputStream infoStream = new InformationInputStream(new ByteArrayInputStream(content)); IOUtils.copy(infoStream, NullOutputStream.NULL_OUTPUT_STREAM); if (infoStream.isShell()) { assertTrue("Permissions on " + entry.getName() + " should be 755", entry.getMode() == 0755); } else { assertTrue("Permissions on " + entry.getName() + " should be 644", entry.getMode() == 0644); } assertTrue(entry.getName() + " doesn't have Unix line endings", infoStream.hasUnixLineEndings()); assertEquals("user", "root", entry.getUserName()); assertEquals("group", "root", entry.getGroupName()); } }); assertTrue("Control files not found in the package", found); }
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 . java 2 s.c om*/ return pEntry; }
From source file:org.vafer.jdeb.mapping.LsMapperTestCase.java
public void testModes() throws Exception { final ByteArrayInputStream is = new ByteArrayInputStream(output.getBytes("UTF-8")); final Mapper mapper = new LsMapper(is); final TarArchiveEntry entry1 = mapper .map(new TarArchiveEntry("trunk/target/test-classes/org/vafer/dependency", true)); assertEquals(493, entry1.getMode()); assertEquals("tcurdt", entry1.getUserName()); assertEquals("tcurdt", entry1.getGroupName()); final TarArchiveEntry entry2 = mapper.map(new TarArchiveEntry( "trunk/target/test-classes/org/vafer/dependency/DependenciesTestCase.class", true)); assertEquals(420, entry2.getMode()); assertEquals("tcurdt", entry2.getUserName()); assertEquals("tcurdt", entry2.getGroupName()); }
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);//w w w . j a v a 2s . co 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 .j a v a 2 s . co m 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 w w w. ja v a 2 s . c o m 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 ww w . j a va 2 s . c om*/ 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);// ww w. j a v a 2 s. co 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 ww w . j av a2s. com 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()); } }