/src/main/java/com/google/security/zynamics/zylib/system/SystemHelpers.java

https://gitlab.com/Manouchehri/binnavi · Java · 177 lines · 62 code · 17 blank · 98 comment · 11 complexity · c9ce26ec7cf6286dca52bd1244663ce3 MD5 · raw file

  1. /*
  2. Copyright 2011-2016 Google Inc. All Rights Reserved.
  3. Licensed under the Apache License, Version 2.0 (the "License");
  4. you may not use this file except in compliance with the License.
  5. You may obtain a copy of the License at
  6. http://www.apache.org/licenses/LICENSE-2.0
  7. Unless required by applicable law or agreed to in writing, software
  8. distributed under the License is distributed on an "AS IS" BASIS,
  9. WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  10. See the License for the specific language governing permissions and
  11. limitations under the License.
  12. */
  13. package com.google.security.zynamics.zylib.system;
  14. import com.google.security.zynamics.zylib.io.FileUtils;
  15. import java.io.File;
  16. /**
  17. * Contains a few simple methods that are useful for determining information about the underlying
  18. * operating system in a platform-independent way.
  19. */
  20. // TODO(cblichmann): Replace this class with Google3/GoogleClient utility code.
  21. public final class SystemHelpers {
  22. /**
  23. * Determines the machine-wide application data directory in a platform-independent way. Note that
  24. * the directory returned by this method is usually not writable for users without administrative
  25. * privileges (yes, even on Windows). The path returned by this method always contains a trailing
  26. * path separator.
  27. *
  28. * @return the machine-wide application data directory
  29. */
  30. public static String getAllUsersApplicationDataDirectory() {
  31. String result;
  32. if (isRunningWindows()) {
  33. // This should be the same as passing CSIDL_COMMON_APPDATA to the
  34. // native SHGetFolderPath() Win32 function.
  35. result = System.getenv("ProgramData");
  36. if (result == null) {
  37. // ProgramData should be unset on XP or lower, so the code
  38. // below assumes we're running on Vista or higher. To find the
  39. // "Application Data" folder for all users, we first determine
  40. // the localized folder name and append that to the
  41. // ALLUSERSPROFILE path.
  42. final String appData = System.getenv("APPDATA");
  43. result =
  44. System.getenv("ALLUSERSPROFILE")
  45. + (appData != null ? appData.substring(appData.lastIndexOf('\\'))
  46. : "\\Application Data");
  47. }
  48. } else if (isRunningLinux()) {
  49. result = "/etc/opt";
  50. } else if (isRunningMacOSX()) {
  51. // TODO(cblichmann): Change to "/Library/Application Support".
  52. // For now, be consistent with the the CLI
  53. // versions of BinDiff/BinDetego
  54. result = "/etc/opt"; // Resolves to "/private/etc/opt"
  55. } else {
  56. // Fallback to local user directory
  57. result = System.getProperty("user.home");
  58. }
  59. return FileUtils.ensureTrailingSlash(result);
  60. }
  61. /**
  62. * Determines the machine-wide application data directory for the specified product in a
  63. * platform-independent way. Note that the directory returned by this method is usually not
  64. * writable for users without administrative privileges (yes, even on Windows). The path returned
  65. * by this method always contains a trailing path separator.
  66. *
  67. * @param product the product name to use when building the directory name
  68. * @return the machine-wide application data directory
  69. */
  70. public static String getAllUsersApplicationDataDirectory(final String product) {
  71. return getAllUsersApplicationDataDirectory() + "zynamics" + File.separator + product
  72. + File.separator;
  73. }
  74. /**
  75. * Determines the application data directory in a platform-independent way. The path returned by
  76. * this method always contains a trailing path separator.
  77. *
  78. * @return the application data directory for the current platform.
  79. */
  80. public static String getApplicationDataDirectory() {
  81. return FileUtils.ensureTrailingSlash(isRunningWindows() ? System.getenv("APPDATA") : System
  82. .getProperty("user.home"));
  83. }
  84. /**
  85. * Determines the application data directory for the specified product in a platform-independent
  86. * way. The path returned by this method always contains a trailing path separator.
  87. *
  88. * @param product the product name to use when building the directory name
  89. * @return the application data directory for the current platform.
  90. */
  91. public static String getApplicationDataDirectory(final String product) {
  92. return getApplicationDataDirectory() + (isRunningWindows() ? "" : ".") + "zynamics"
  93. + File.separator + product + File.separator;
  94. }
  95. /**
  96. * Determines the system temporary directory. The path returned by this method always contains a
  97. * trailing path separator.
  98. *
  99. * @return The directory where to save temporary files in.
  100. */
  101. public static String getTempDirectory() {
  102. return FileUtils.ensureTrailingSlash(System.getProperty("java.io.tmpdir"));
  103. }
  104. /**
  105. * Determines the system temporary directory for the specified product. The path returned by this
  106. * method always contains a trailing path separator.
  107. *
  108. * @param product the product name to use when building the directory name
  109. * @return The directory where to save temporary files in.
  110. */
  111. public static String getTempDirectory(final String product) {
  112. return getTempDirectory() + "zynamics" + File.separator + product + File.separator;
  113. }
  114. /**
  115. * Returns the user's home directory. The path returned by this method always contains a trailing
  116. * path separator.
  117. *
  118. * @return the full path to the user's home directory.
  119. */
  120. public static String getUserDirectory() {
  121. return FileUtils.ensureTrailingSlash(System.getProperty("user.home"));
  122. }
  123. /**
  124. * Determines whether the program is running in Linux
  125. *
  126. * @return true, if the program is running in Linux, false otherwise.
  127. */
  128. public static boolean isRunningLinux() {
  129. return System.getProperty("os.name").startsWith("Linux");
  130. }
  131. /**
  132. * Determines whether the program is running in Windows.
  133. *
  134. * @return true, if the program is running in Windows, false otherwise.
  135. */
  136. public static boolean isRunningMacOSX() {
  137. return System.getProperty("os.name").startsWith("Mac");
  138. }
  139. /**
  140. * Determines whether the program is running in Windows.
  141. *
  142. * @return true, if the program is running in Windows, false otherwise.
  143. */
  144. public static boolean isRunningWindows() {
  145. return System.getProperty("os.name").startsWith("Windows");
  146. }
  147. /**
  148. * Returns true if the current system has a 64bit architecture.
  149. */
  150. public static boolean is64BitArchitecture() {
  151. if (isRunningWindows()) {
  152. return "AMD64".equals(System.getenv("PROCESSOR_ARCHITECTURE"))
  153. || "AMD64".equals(System.getenv("PROCESSOR_ARCHITEW6432"));
  154. }
  155. throw new RuntimeException("Not implememted for Linux or Mac yet.");
  156. }
  157. }