Android 如何在java/native层修改一个文件的权限(mode)与用户(owner)?

前言
         欢迎大家我分享和推荐好用的代码段~~
声明
         欢迎转载,但请保留文章原始出处:
         CSDN:http://www.csdn.net
         雨季o莫忧离:http://blog.csdn.net/luckkof

正文

 

[Description]
如何在java/native层修改一个文件的权限(mode),用户(owner),组(group),以满足安全需要?
[Keyword]
文件权限 文件用户 mode owner chomd chown permission
[Solution]
在native 层:
#include <sys/types.h>
#include <sys/stat.h>
#include <unistd.h>
 
//chmod/fchmod 用来更新访问权限
int chmod(const char *path, mode_t mode);
int fchmod(int fildes, mode_t mode);
 
//chown/fchown/lchown 用来更新文件owner 和 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);
 
//用来读取文件元数据
int stat(const char *path, struct stat *buf);
int fstat(int filedes, struct stat *buf);
int lstat(const char *path, struct stat *buf);
 
更多的资讯可以在linux 中 man chmod ; man chown ; man stat
 
在java 层:
java default 并不提供这样的功能,android 为满足内部需要,在android.os.FileUtils 类中提供了setPermissions 方法,结合了chmod 与chown. 参数中mode 即chmod 参数中的mode,当不需要设置file 的uid 和 group 时,可将uid 和 gid 都设置成-1;
android.os.FileUtils
public static native int setPermissions(String file, int mode, int uid, int gid);

Android 如何在java/native层修改一个文件的权限(mode)与用户(owner)?,布布扣,bubuko.com

Android 如何在java/native层修改一个文件的权限(mode)与用户(owner)?

上一篇:Android 如何将一个app 设置为持久app, 不被low memory kill 关闭


下一篇:Android native CursorWindow数据保存原理