package org.snmp4j.agent.tutorial.impl; import org.snmp4j.agent.mo.snmp.DateAndTime; import org.snmp4j.agent.mo.snmp.tc.DateAndTimeTC; import org.snmp4j.agent.tutorial.Snmp4jAgentTutorialMib; import org.snmp4j.mp.SnmpConstants; import org.snmp4j.smi.*; import java.io.File; import java.io.IOException; import java.nio.file.Files; import java.nio.file.LinkOption; import java.nio.file.Path; import java.nio.file.attribute.BasicFileAttributes; import java.nio.file.attribute.PosixFilePermission; import java.util.Date; import java.util.GregorianCalendar; import java.util.Set; /** * Created by fock on 20.06.2016. */ public class Snmp4JAgentTutorialFileTreeRowHelper { private static final byte[] PERMS = { 'r', 'w', 'x' }; public static Variable[] createRowFromFile(File file) throws IOException { return createRowFromPath(file.toPath()); } public static Variable[] createRowFromPath(Path path) throws IOException { Variable[] row = new Variable[10]; String pathString = path.toString(); row[0] = new OctetString(pathString.substring(0, Math.min(pathString.length(), 255))); BasicFileAttributes attrs = Files.readAttributes(path, BasicFileAttributes.class); if (Files.isDirectory(path, LinkOption.NOFOLLOW_LINKS)) { row[1] = new Integer32(Snmp4jAgentTutorialMib.Snmp4jAgentTutorialFileType.directory); } else if (Files.isRegularFile(path, LinkOption.NOFOLLOW_LINKS)) { row[1] = new Integer32(Snmp4jAgentTutorialMib.Snmp4jAgentTutorialFileType.regularFile); } else if (Files.isSymbolicLink(path)) { row[1] = new Integer32(Snmp4jAgentTutorialMib.Snmp4jAgentTutorialFileType.symbolicLink); } else { row[1] = new Integer32(Snmp4jAgentTutorialMib.Snmp4jAgentTutorialFileType.other); } GregorianCalendar creationDateAndTime = new GregorianCalendar(); creationDateAndTime.setTimeInMillis(attrs.creationTime().toMillis()); row[2] = DateAndTime.makeDateAndTime(creationDateAndTime); GregorianCalendar lastModifiedDateAndTime = new GregorianCalendar(); creationDateAndTime.setTimeInMillis(attrs.lastModifiedTime().toMillis()); row[3] = DateAndTime.makeDateAndTime(lastModifiedDateAndTime); GregorianCalendar lastAccessDateAndTime = new GregorianCalendar(); creationDateAndTime.setTimeInMillis(attrs.lastAccessTime().toMillis()); row[4] = DateAndTime.makeDateAndTime(lastAccessDateAndTime); long fileSize = Files.size(path); row[5] = new UnsignedInteger32(Math.min(fileSize, 4294967295L)); if (fileSize < 1024) { row[6] = new UnsignedInteger32(fileSize); row[7] = new Integer32(Snmp4jAgentTutorialMib.Snmp4jAgentTutorialFileSizeUnit.bytes); } else if (fileSize < (1L << 20)) { row[6] = new UnsignedInteger32(fileSize / 1024); row[7] = new Integer32(Snmp4jAgentTutorialMib.Snmp4jAgentTutorialFileSizeUnit.kiloBytes); } else if (fileSize < (1L << 30)) { row[6] = new UnsignedInteger32(fileSize / (1L << 20)); row[7] = new Integer32(Snmp4jAgentTutorialMib.Snmp4jAgentTutorialFileSizeUnit.megaBytes); } else if (fileSize < (1L << 40)) { row[6] = new UnsignedInteger32(fileSize / (1L << 30)); row[7] = new Integer32(Snmp4jAgentTutorialMib.Snmp4jAgentTutorialFileSizeUnit.gigaBytes); } else { row[6] = new UnsignedInteger32(fileSize / (1L << 50)); row[7] = new Integer32(Snmp4jAgentTutorialMib.Snmp4jAgentTutorialFileSizeUnit.petaBytes); } row[8] = new Integer32(path.hashCode()); OctetString permissions = new OctetString(" "); try { Set permissionSet = Files.getPosixFilePermissions(path, LinkOption.NOFOLLOW_LINKS); for (int i = 0; i < PosixFilePermission.values().length; i++) { if (permissionSet.contains(PosixFilePermission.values()[i])) { permissions.set(i, PERMS[i % 3]); } } } catch (UnsupportedOperationException uoex) { // ignore } row[9] = permissions; return row; } }