Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Java Creating files and directories with a certain owner (user/group) [duplicate]

Is possible in Java to manage creating files / directories (if program runs as ROOT) with different user/group ?

like image 904
To Kra Avatar asked Mar 03 '15 10:03

To Kra


2 Answers

You can achieve JDK 7 (Java NIO)

Use setOwner() method....

public static Path setOwner(Path path,
                            UserPrincipal owner)
                     throws IOException

Usage Example: Suppose we want to make "joe" the owner of a file:

 Path path = ...
 UserPrincipalLookupService lookupService =
     provider(path).getUserPrincipalLookupService();
 UserPrincipal joe = lookupService.lookupPrincipalByName("joe");
 Files.setOwner(path, joe);

Get more information about same from below url

http://docs.oracle.com/javase/tutorial/essential/io/file.html

Example : Set Owner

import java.nio.file.FileSystems;
import java.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.Paths;
import java.nio.file.attribute.FileOwnerAttributeView;
import java.nio.file.attribute.UserPrincipal;
import java.nio.file.attribute.UserPrincipalLookupService;

public class Test {

  public static void main(String[] args) throws Exception {
    Path path = Paths.get("C:/home/docs/users.txt");
    FileOwnerAttributeView view = Files.getFileAttributeView(path,
        FileOwnerAttributeView.class);
    UserPrincipalLookupService lookupService = FileSystems.getDefault()
        .getUserPrincipalLookupService();
    UserPrincipal userPrincipal = lookupService.lookupPrincipalByName("mary");

    Files.setOwner(path, userPrincipal);
    System.out.println("Owner: " + view.getOwner().getName());

  }
}

Example : GetOwner

import java.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.Paths;
import java.nio.file.attribute.FileOwnerAttributeView;
import java.nio.file.attribute.UserPrincipal;

public class Test {
  public static void main(String[] args) throws Exception {
    Path path = Paths.get("C:/home/docs/users.txt");
    FileOwnerAttributeView view = Files.getFileAttributeView(path,
        FileOwnerAttributeView.class);
    UserPrincipal userPrincipal = view.getOwner();
    System.out.println(userPrincipal.getName());
  }
}
like image 118
Android Team Avatar answered Oct 19 '22 22:10

Android Team


You can achieve this in JDK 7 using new IO API (Java NIO)

There are getOwner()/setOwner() methods available to manage ownership of files, and to manage groups you can use PosixFileAttributeView.setGroup()

The following code snippet shows how to set the file owner by using the setOwner method:

Path file = ...;
UserPrincipal owner = file.GetFileSystem().getUserPrincipalLookupService()
        .lookupPrincipalByName("sally");
Files.setOwner(file, owner);

There is no special-purpose method in the Files class for setting a group owner. However, a safe way to do so directly is through the POSIX file attribute view, as follows:

Path file = ...;
GroupPrincipal group =
    file.getFileSystem().getUserPrincipalLookupService()
        .lookupPrincipalByGroupName("green");
Files.getFileAttributeView(file, PosixFileAttributeView.class)
     .setGroup(group);
like image 28
Abhijeet Dhumal Avatar answered Oct 19 '22 22:10

Abhijeet Dhumal