Android 如何在java/native層修改一個檔案的許可權(mode)與使用者(owner)?

來源:互聯網
上載者:User

標籤:google   frameworks   csdn   apk   android   

前言
         歡迎大家我分享和推薦好用的程式碼片段~~
聲明
         歡迎轉載,但請保留文章原始出處:
         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);

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.