Objective
Welcome everyone I share and recommend useful code Snippets ~ ~
Statement
Welcome reprint, but please keep the original source of the article:
Csdn:http://www.csdn.net
Rainy season o mo away: http://blog.csdn.net/luckkof
Body
[Description]
How to modify the permissions of a file in the Java/native layer (mode), user (owner), group, to meet security needs?
[Keyword]
File permissions file user mode owner CHOMD chown permission
[Solution]
On the native layer:
#include <sys/types.h>
#include <sys/stat.h>
#include <unistd.h> //chmod/fchmod used to update access rights
int chmod (const char *path, mode_t mode);
int fchmod (int fildes, mode_t mode); Chown/fchown/lchown to update the file owner and group
int chown (const char *path, uid_t owner, gid_t Group);
int fchown (int fd, uid_t owner, gid_t Group);
int Lchown (const char *path, uid_t owner, gid_t Group); //used to read file meta data
int stat (const char *path, struct stat *buf);
int fstat (int filedes, struct stat *buf);
int Lstat (const char *path, struct stat *buf); more information can be chmod in Linux, man chown, man stat in Java layer:
Java default does not provide such functionality, Android to meet internal needs, in the Android.os.FileUtils The SetPermissions method is provided in the class, combining chmod with Chown. Parameter mode is the mode in the chmod parameter, when you do not need to set the UID and group of file, both UID and GID can be set to-1;
Android.os.FileUtils
public static native int SetPermissions (String file, int mode, int uid, int gid);