LsOutputBean build() { return new LsOutputBean( isFile, isDir, isLink, userReadable, userWritable, userExecutable, groupReadable, groupWritable, groupExecutable, otherReadable, otherWritable, otherExecutable, hasAcl, linkCount, size, date, user, group, name, linkTarget ); } }
switch (attribute) { case Owner: logger.trace(" [{}] getAttribute({}) -> {}", new Object[]{name, attribute, bean.getUser()}); return bean.getUser(); case Group: logger.trace(" [{}] getAttribute({}) -> {}", new Object[]{name, attribute, bean.getGroup()}); return bean.getGroup(); case IsSymbolicLink: logger.trace(" [{}] getAttribute({}) -> {}", new Object[]{name, attribute, bean.isLink()}); return bean.isLink(); case Permissions: Set<Permission> tmp = new HashSet<>(); if (bean.isUserReadable()) tmp.add(SshFile.Permission.UserRead); if (bean.isUserWritable()) tmp.add(Permission.UserWrite); if (bean.isUserExecutable()) tmp.add(Permission.UserExecute); if (bean.isGroupReadable()) tmp.add(Permission.GroupRead); if (bean.isGroupWritable()) tmp.add(Permission.GroupWrite); if (bean.isGroupExecutable()) tmp.add(Permission.GroupExecute); if (bean.isOtherReadable()) tmp.add(Permission.OthersRead); if (bean.isOtherWritable()) tmp.add(Permission.OthersWrite); if (bean.isOtherExecutable())
Assert.assertTrue(dir.isExists()); Assert.assertTrue(dir.isDir()); Assert.assertFalse(dir.isFile()); Assert.assertFalse(dir.isLink()); Assert.assertTrue(dir.isUserReadable()); Assert.assertTrue(dir.isUserWritable()); Assert.assertTrue(dir.isUserExecutable()); Assert.assertTrue(dir.isGroupReadable()); Assert.assertFalse(dir.isGroupWritable()); Assert.assertTrue(dir.isGroupExecutable()); Assert.assertTrue(dir.isOtherReadable()); Assert.assertFalse(dir.isOtherWritable()); Assert.assertTrue(dir.isOtherExecutable()); Assert.assertFalse(dir.isHasAcl()); Assert.assertEquals(1, dir.getLinkCount()); Assert.assertEquals("user", dir.getUser()); Assert.assertEquals("group", dir.getGroup()); Assert.assertEquals(1234, dir.getSize()); Assert.assertEquals("dir_name", dir.getName()); Assert.assertNull(dir.getLinkTarget()); Assert.assertNotNull(dir.getDate()); Assert.assertEquals(88200000, dir.getDate().getTime()); Assert.assertTrue(file1.isFile()); Assert.assertFalse(file1.isDir()); Assert.assertFalse(file1.isLink()); Assert.assertEquals("file_name", file1.getName()); Assert.assertNotNull(file1.getDate()); Assert.assertEquals(86400000, file1.getDate().getTime());
@Test public void symLink() throws IOException { List<LsOutputBean> beans = new LsOutputParser().parse(stream("sym-link.txt")); Assert.assertEquals(1, beans.size()); LsOutputBean link = beans.get(0); Assert.assertFalse(link.isDir()); Assert.assertFalse(link.isFile()); Assert.assertTrue(link.isLink()); Assert.assertEquals("link_name", link.getName()); Assert.assertEquals("/absolute/link/target", link.getLinkTarget()); Assert.assertNotNull(link.getDate()); Assert.assertTrue(link.getDate().getTime() > 0); }
LsOutputBean bean = wrapper[0]; if (bean != null) { if (bean.isLink()) { bean = findFinalLinkTarget(bean, parser); file = bean.getName(); name = file; bean = new LsOutputBean(name); return createFile(bean, file);
@Test public void notExists() { LsOutputBean bean = new LsOutputBean("name"); Assert.assertFalse(bean.isExists()); }
protected LsOutputBean findFinalLinkTarget(LsOutputBean bean, final LsOutputParser parser ) { LsOutputBean tmp = bean; final LsOutputBean[] wrapper = new LsOutputBean[1]; int i=0; while (tmp.isLink()) { shell.addCommand("ls -lad \"" + tmp.getLinkTarget() + "\"", 0, new Shell.OnCommandLineListener() { @Override public void onLine(String s) { wrapper[0] = parser.parseLine(s); } @Override public void onCommandResult(int i, int i1) { } }); shell.waitForIdle(); tmp = wrapper[0]; i++; if (i > 20) { break; } } return tmp; }
@Test public void nameWithSpaces() throws IOException { List<LsOutputBean> beans = new LsOutputParser().parse(stream("name-with-spaces.txt")); Assert.assertEquals(4, beans.size()); Assert.assertEquals("dir name", beans.get(0).getName()); Assert.assertEquals("file name", beans.get(1).getName()); Assert.assertEquals("file 2", beans.get(2).getName()); Assert.assertEquals("link name", beans.get(3).getName()); Assert.assertEquals("/absolute/link target", beans.get(3).getLinkTarget()); }
@Override public String readSymbolicLink() throws IOException { logger.trace("[{}] readSymbolicLink()", name); return bean.getLinkTarget(); }
public OutputStream createOutputStream(long offset) throws IOException { logger.trace("[{}] createOutputStream(offset: {})", name, offset); if (!bean.isExists()) { // if file does not exist, explicitly create it as root, see GH issue #117 runCommand("touch" + " \"" + absPath + "\""); } ProcessBuilder processBuilder = new ProcessBuilder(); processBuilder.command("su", "-c", "\"dd of=" + escapePathForDD(absPath) + "\""); Process proc = processBuilder.start(); return new BufferedOutputStream(proc.getOutputStream()); }
public List<T> listFiles() { logger.trace("[{}] listFiles()", name); List<T> result = new ArrayList<>(); final LsOutputParser parser = new LsOutputParser(); final List<LsOutputBean> beans = new ArrayList<>(); shell.addCommand("ls -la " + absPath, 0, new Shell.OnCommandLineListener() { @Override public void onLine(String s) { LsOutputBean bean = parser.parseLine(s); if (bean != null) { beans.add(bean); } } @Override public void onCommandResult(int i, int i1) { } }); shell.waitForIdle(); for (LsOutputBean bean : beans) { String path = absPath + "/" + bean.getName(); result.add(createFile(shell, bean, path)); } return result; }
Assert.assertTrue(dir.isExists()); Assert.assertTrue(dir.isDir()); Assert.assertFalse(dir.isFile()); Assert.assertFalse(dir.isLink()); Assert.assertTrue(dir.isUserReadable()); Assert.assertTrue(dir.isUserWritable()); Assert.assertTrue(dir.isUserExecutable()); Assert.assertTrue(dir.isGroupReadable()); Assert.assertFalse(dir.isGroupWritable()); Assert.assertTrue(dir.isGroupExecutable()); Assert.assertTrue(dir.isOtherReadable()); Assert.assertFalse(dir.isOtherWritable()); Assert.assertTrue(dir.isOtherExecutable()); Assert.assertFalse(dir.isHasAcl()); Assert.assertEquals(0, dir.getLinkCount()); Assert.assertEquals("user", dir.getUser()); Assert.assertEquals("group", dir.getGroup()); Assert.assertEquals(1234, dir.getSize()); Assert.assertEquals("dir_name", dir.getName()); Assert.assertNull(dir.getLinkTarget()); Assert.assertNotNull(dir.getDate()); Assert.assertEquals(88200000, dir.getDate().getTime()); Assert.assertTrue(file1.isFile()); Assert.assertFalse(file1.isDir()); Assert.assertFalse(file1.isLink()); Assert.assertEquals("file_name", file1.getName()); Assert.assertNotNull(file1.getDate()); Assert.assertEquals(86400000, file1.getDate().getTime());