ArafatKhan2198 commented on code in PR #4682:
URL: https://github.com/apache/ozone/pull/4682#discussion_r1195661070
##########
hadoop-hdds/common/src/main/java/org/apache/hadoop/hdds/server/ServerUtils.java:
##########
@@ -178,12 +184,90 @@ public static File
getDirectoryFromConfig(ConfigurationSource conf,
dbDirPath + " specified in configuration setting " +
key);
}
+ try {
+ Path path = dbDirPath.toPath();
+ // Fetch the permissions for the respective component from the config
+ String permissionValue = getPermissions(key, conf);
+ String symbolicPermission = getSymbolicPermission(permissionValue);
+
+ // Set the permissions for the directory
+ Files.setPosixFilePermissions(path,
+ PosixFilePermissions.fromString(symbolicPermission));
+ } catch (Exception e) {
+ throw new RuntimeException("Failed to set directory permissions for " +
+ dbDirPath + ": " + e.getMessage(), e);
+ }
return dbDirPath;
}
return null;
}
+ /**
+ * Fetches the symbolic representation of the permission value.
+ *
+ * @param permissionValue the permission value (octal or symbolic)
+ * @return the symbolic representation of the permission value
+ */
+ private static String getSymbolicPermission(String permissionValue) {
+ if (isSymbolic(permissionValue)) {
+ // For symbolic representation, use it directly
+ return permissionValue;
+ } else {
+ // For octal representation, convert it to FsPermission object and then
+ // to symbolic representation
+ short octalPermission = Short.parseShort(permissionValue, 8);
+ FsPermission fsPermission = new FsPermission(octalPermission);
+ return fsPermission.toString();
+ }
+ }
+
+ /**
+ * Checks if the permission value is in symbolic representation.
+ *
+ * @param permissionValue the permission value to check
+ * @return true if the permission value is in symbolic representation,
+ * false otherwise
+ */
+ private static boolean isSymbolic(String permissionValue) {
+ return permissionValue.matches(".*[rwx].*");
+ }
+
+
+ /**
+ * Retrieves the permissions' configuration value for a given config key.
+ *
+ * @param key The configuration key.
+ * @param conf The ConfigurationSource object containing the config
+ * @return The permissions' configuration value for the specified key.
+ * @throws IllegalArgumentException If the configuration value is not defined
+ */
+ public static String getPermissions(String key, ConfigurationSource conf) {
+ String configName = "";
+
+ // Assign the appropriate config name based on the KEY
+ if (key.equals(ReconConfigKeys.OZONE_RECON_DB_DIR)) {
+ configName = ReconConfigKeys.OZONE_RECON_DB_DIRS_PERMISSIONS;
+ } else if (key.equals(ScmConfigKeys.OZONE_SCM_DB_DIRS)) {
+ configName = ScmConfigKeys.OZONE_SCM_DB_DIRS_PERMISSIONS;
+ } else if (key.equals(OzoneConfigKeys.OZONE_OM_DB_DIRS)) {
+ configName = OzoneConfigKeys.OZONE_OM_DB_DIRS_PERMISSIONS;
+ } else {
+ // If the permissions are not defined for the config, we make it fall
+ // back to the default permissions for metadata files and directories
+ configName = OzoneConfigKeys.OZONE_METADATA_DIRS_PERMISSIONS;
+ }
+
+ String configValue = conf.get(configName);
+ if (configValue != null) {
+ return configValue;
+ }
+
+ throw new IllegalArgumentException(
+ "Invalid configuration value for key: " + key);
Review Comment:
The get() method in the `ConfigurationSource` interface utilizes the
in-memory map of configurations and their respective values stored in the
`InMemoryConfiguration` class. This map holds the defined configurations. When
the specified key does not exist in the map, the get() method will return null.
Similarly, if the key has not been set or is not present in the map, the method
will not find a corresponding value and also return null.
Hence, in a case where the configuration does not exist which means a null
value will be given to `configValue`, an `IllegalArgumentException` will be
thrown.
--
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.
To unsubscribe, e-mail: [email protected]
For queries about this service, please contact Infrastructure at:
[email protected]
---------------------------------------------------------------------
To unsubscribe, e-mail: [email protected]
For additional commands, e-mail: [email protected]