PageRenderTime 83ms CodeModel.GetById 11ms RepoModel.GetById 0ms app.codeStats 0ms

/src/org/joval/os/unix/io/driver/SolarisDriver.java

http://github.com/joval/jOVAL
Java | 165 lines | 134 code | 20 blank | 11 comment | 23 complexity | 32471d589c78eb2eb4751f3a3db42f58 MD5 | raw file
Possible License(s): GPL-3.0, AGPL-3.0
  1. // Copyright (C) 2011 jOVAL.org. All rights reserved.
  2. // This software is licensed under the AGPL 3.0 license available at http://www.joval.org/agpl_v3.txt
  3. package org.joval.os.unix.io.driver;
  4. import java.text.ParseException;
  5. import java.text.SimpleDateFormat;
  6. import java.util.Collection;
  7. import java.util.Iterator;
  8. import java.util.StringTokenizer;
  9. import java.util.Vector;
  10. import java.util.regex.Pattern;
  11. import org.slf4j.cal10n.LocLogger;
  12. import org.joval.intf.io.IFile;
  13. import org.joval.intf.io.IFilesystem;
  14. import org.joval.intf.io.IReader;
  15. import org.joval.intf.unix.io.IUnixFileInfo;
  16. import org.joval.intf.unix.io.IUnixFilesystem;
  17. import org.joval.intf.unix.io.IUnixFilesystemDriver;
  18. import org.joval.intf.unix.system.IUnixSession;
  19. import org.joval.intf.util.IProperty;
  20. import org.joval.io.fs.FileInfo;
  21. import org.joval.io.PerishableReader;
  22. import org.joval.os.unix.io.UnixFileInfo;
  23. import org.joval.util.JOVALMsg;
  24. import org.joval.util.SafeCLI;
  25. import org.joval.util.StringTools;
  26. /**
  27. * IUnixFilesystemDriver implementation for Solaris.
  28. *
  29. * @author David A. Solin
  30. * @version %I% %G%
  31. */
  32. public class SolarisDriver implements IUnixFilesystemDriver {
  33. private IUnixSession us;
  34. private IProperty props;
  35. private LocLogger logger;
  36. public SolarisDriver(IUnixSession us) {
  37. this.us = us;
  38. props = us.getProperties();
  39. logger = us.getLogger();
  40. }
  41. // Implement IUnixFilesystemDriver
  42. public Collection<String> getMounts(Pattern typeFilter) throws Exception {
  43. Collection<String> mounts = new Vector<String>();
  44. IFile f = us.getFilesystem().getFile("/etc/vfstab");
  45. IReader reader = PerishableReader.newInstance(f.getInputStream(), us.getTimeout(IUnixSession.Timeout.S));
  46. String line = null;
  47. while ((line = reader.readLine()) != null) {
  48. if (!line.startsWith("#")) { // skip comments
  49. StringTokenizer tok = new StringTokenizer(line);
  50. String dev = tok.nextToken();
  51. String fixdev = tok.nextToken();
  52. String mountPoint = tok.nextToken();
  53. String fsType = tok.nextToken();
  54. if (typeFilter != null && typeFilter.matcher(fsType).find()) {
  55. logger.info(JOVALMsg.STATUS_FS_MOUNT_SKIP, mountPoint, fsType);
  56. } else if (mountPoint.startsWith(IUnixFilesystem.DELIM_STR)) {
  57. logger.info(JOVALMsg.STATUS_FS_MOUNT_ADD, mountPoint, fsType);
  58. mounts.add(mountPoint);
  59. }
  60. }
  61. }
  62. return mounts;
  63. }
  64. public String getFindCommand() {
  65. return "find %MOUNT% -mount -exec " + getStatCommand() + " {} \\;";
  66. }
  67. public String getStatCommand() {
  68. return "ls -dnE";
  69. }
  70. public UnixFileInfo nextFileInfo(Iterator<String> lines) {
  71. String line = null;
  72. if (lines.hasNext()) {
  73. line = lines.next();
  74. } else {
  75. return null;
  76. }
  77. char unixType = line.charAt(0);
  78. String permissions = line.substring(1, 10);
  79. boolean hasExtendedAcl = false;
  80. if (line.charAt(10) == '+') {
  81. hasExtendedAcl = true;
  82. }
  83. StringTokenizer tok = new StringTokenizer(line.substring(11));
  84. String linkCount = tok.nextToken();
  85. int uid = -1;
  86. try {
  87. uid = Integer.parseInt(tok.nextToken());
  88. } catch (NumberFormatException e) {
  89. //DAS -- could be, e.g., 4294967294 (illegal "nobody" value)
  90. }
  91. int gid = -1;
  92. try {
  93. gid = Integer.parseInt(tok.nextToken());
  94. } catch (NumberFormatException e) {
  95. //DAS -- could be, e.g., 4294967294 (illegal "nobody" value)
  96. }
  97. FileInfo.Type type = FileInfo.Type.FILE;
  98. switch(unixType) {
  99. case IUnixFileInfo.DIR_TYPE:
  100. type = FileInfo.Type.DIRECTORY;
  101. break;
  102. case IUnixFileInfo.LINK_TYPE:
  103. type = FileInfo.Type.LINK;
  104. break;
  105. case IUnixFileInfo.CHAR_TYPE:
  106. case IUnixFileInfo.BLOCK_TYPE:
  107. int ptr = -1;
  108. if ((ptr = line.indexOf(",")) > 11) {
  109. tok = new StringTokenizer(line.substring(ptr+1));
  110. }
  111. break;
  112. }
  113. long length = 0;
  114. try {
  115. length = Long.parseLong(tok.nextToken());
  116. } catch (NumberFormatException e) {
  117. }
  118. long mtime = IFile.UNKNOWN_TIME;
  119. String dateStr = tok.nextToken("/").trim();
  120. try {
  121. String parsable = new StringBuffer(dateStr.substring(0, 23)).append(dateStr.substring(29)).toString();
  122. mtime = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss.SSS Z").parse(parsable).getTime();
  123. } catch (ParseException e) {
  124. e.printStackTrace();
  125. }
  126. String path = null, linkPath = null;
  127. int begin = line.indexOf(us.getFilesystem().getDelimiter());
  128. if (begin > 0) {
  129. int end = line.indexOf("->");
  130. if (end == -1) {
  131. path = line.substring(begin).trim();
  132. } else if (end > begin) {
  133. path = line.substring(begin, end).trim();
  134. linkPath = line.substring(end+2).trim();
  135. }
  136. }
  137. if (type == FileInfo.Type.LINK && linkPath == null) {
  138. logger.warn(JOVALMsg.ERROR_LINK_NOWHERE, path);
  139. return nextFileInfo(lines);
  140. } else {
  141. return new UnixFileInfo(IFile.UNKNOWN_TIME, mtime, IFile.UNKNOWN_TIME, type, length, path, linkPath,
  142. unixType, permissions, uid, gid, hasExtendedAcl);
  143. }
  144. }
  145. }