All Downloads are FREE. Search and download functionalities are using the official Maven repository.

xapi.file.impl.FileServiceJava7 Maven / Gradle / Ivy

Go to download

Everything needed to run a comprehensive dev environment. Just type X_ and pick a service from autocomplete; new dev modules will be added as they are built. The only dev service not included in the uber jar is xapi-dev-maven, as it includes all runtime dependencies of maven, adding ~4 seconds to build time, and 6 megabytes to the final output jar size (without xapi-dev-maven, it's ~1MB).

The newest version!
package xapi.file.impl;

import java.io.File;
import java.io.IOException;
import java.nio.file.Files;
import java.nio.file.Paths;
import java.nio.file.attribute.PosixFilePermission;
import java.util.HashSet;
import java.util.Set;

import xapi.annotation.inject.SingletonOverride;
import xapi.file.api.FileService;
import xapi.log.X_Log;
import xapi.platform.JrePlatform;
import xapi.util.X_Debug;

@JrePlatform(version=7)
@SingletonOverride(implFor=FileService.class)
public class FileServiceJava7 extends FileServiceImpl {

  @Override
  public File chmod(int chmod, File file) {
    assertValidChmod(chmod);
    Set perms = getPermissions(chmod);
    try {
      Files.setPosixFilePermissions(Paths.get(file.getCanonicalPath()), perms);
    } catch (IOException e) {
      X_Log.warn(getClass(), "Could not chmod file",file,"with permissions","0x"+Integer.toHexString(chmod), e);
      X_Debug.maybeRethrow(e);
    }
    return file;
  }

  private Set getPermissions(int chmod) {
    Set perms = new HashSet<>();
    if ((chmod & 1) > 0) {
      perms.add(PosixFilePermission.OTHERS_EXECUTE);
    }
    if ((chmod & 2) > 0) {
      perms.add(PosixFilePermission.OTHERS_WRITE);
    }
    if ((chmod & 4) > 0) {
      perms.add(PosixFilePermission.OTHERS_READ);
    }
    if ((chmod & 0x10) > 0) {
      perms.add(PosixFilePermission.GROUP_EXECUTE);
    }
    if ((chmod & 0x20) > 0) {
      perms.add(PosixFilePermission.GROUP_WRITE);
    }
    if ((chmod & 0x40) > 0) {
      perms.add(PosixFilePermission.GROUP_READ);
    }
    if ((chmod & 0x100) > 0) {
      perms.add(PosixFilePermission.OWNER_EXECUTE);
    }
    if ((chmod & 0x200) > 0) {
      perms.add(PosixFilePermission.OWNER_WRITE);
    }
    if ((chmod & 0x400) > 0) {
      perms.add(PosixFilePermission.OWNER_READ);
    }
    return perms;
  }
  
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy