片段->sdcard
section.1 main()--------sdcard.c
1 int main(int argc, char **argv) 2 { 3 struct fuse fuse; 4 char opts[256]; 5 int fd; 6 int res; 7 const char *path = NULL; 8 int i; 9 10 for (i = 1; i < argc; i++) { 11 char* arg = argv[i]; 12 if (!path) 13 path = arg; 14 else if (uid == -1) 15 uid = strtoul(arg, 0, 10); 16 else if (gid == -1) 17 gid = strtoul(arg, 0, 10); 18 else { 19 ERROR("too many arguments\n"); 20 return usage(); 21 } 22 } 23 24 if (!path) { 25 ERROR("no path specified\n"); 26 return usage(); 27 } 28 if (uid <= 0 || gid <= 0) { 29 ERROR("uid and gid must be nonzero\n"); 30 return usage(); 31 } 32 33 /* cleanup from previous instance, if necessary */ 34 umount2(MOUNT_POINT, 2);//---不会立即执行umount操作,而会等挂载点退出忙碌状态时才会去卸载它。
//----不过此函数执行会阻止对该挂载点执行新的访问。
//-----之前就在访问此挂载点操作也不会强制其退出,而是会等待其自然退出。35 36 fd = open("/dev/fuse", O_RDWR);//------fuse的内核实现 37 if (fd < 0){ 38 ERROR("cannot open fuse device (%d)\n", errno); 39 return -1; 40 } 41 42 sprintf(opts, "fd=%i,rootmode=40000,default_permissions,allow_other," 43 "user_id=%d,group_id=%d", fd, uid, gid); //fuse 文件系统的挂载
44 //MOUNT_POINT =/storage/sdcard0 45 res = mount("/dev/fuse", MOUNT_POINT, "fuse", MS_NOSUID | MS_NODEV, opts); 46 if (res < 0) { 47 ERROR("cannot mount fuse filesystem (%d)\n", errno); 48 return -1; 49 } 50 51 if (setgid(gid) < 0) { 52 ERROR("cannot setgid!\n"); 53 return -1; 54 } 55 if (setuid(uid) < 0) { 56 ERROR("cannot setuid!\n"); 57 return -1; 58 } 59 60 fuse_init(&fuse, fd, path);//--------见section.2 61 62 umask(0);//---------系统默认文件权限,chmod的补码形式存在 63 handle_fuse_requests(&fuse);//---------see section.3 64 65 return 0; 66 }
section.2
fuse_init()
void fuse_init(struct fuse *fuse, int fd, const char *path)---fuse文件系统的构建
---->static char *rename_node(struct node *node, const char *name)
---->static void node_find_actual_name(struct node *node)
section.3
1 void handle_fuse_requests(struct fuse *fuse) 2 { 3 unsigned char req[256 * 1024 + 128]; 4 int len; 5 6 for (;;) {//---------无限循环......... 7 len = read(fuse->fd, req, sizeof(req));//--------阻塞的等待用户操作dev/fuse设备的请求 8 if (len < 0) { 9 if (errno == EINTR) 10 continue; 11 ERROR("handle_fuse_requests: errno=%d\n", errno); 12 return; 13 }
//-------详细的处理过程 14 handle_fuse_request(fuse, (void*) req, (void*) (req + sizeof(struct fuse_in_header)), len) ; 15 } 16 }
section.4
1 on early-init 2 export EXTERNAL_STORAGE /storage/sdcard0 3 mkdir /storage 0050 system sdcard_r//----------storage目录可读的,
//就是不能往里面写东西 4 mkdir /storage/sdcard0 0000 system system//------sdcard0目录连读权限都没了 5 # for backwards compatibility 6 symlink /storage/sdcard0 /sdcard 7 symlink /storage/sdcard0 /mnt/sdcard 8 9 10 on post-fs-data 11 # we will remap this as /storage/sdcard0 with the sdcard fuse tool 12 mkdir /data/media 0770 media_rw media_rw//------目录的全新啊 13 chown media_rw media_rw /data/media//------目录内文件的权限 14 15 on fs 16 mount_all /fstab.tuna//----------mount system userdata cache...
17 setprop ro.crypto.fuse_sdcard true//---------启动sdcard 18 19 # create virtual SD card at /storage/sdcard0, based on the /data/media directory 20 # daemon will drop to user/group system/media_rw after initializing 21 # underlying files in /data/media will be created with user and group media_rw (1023) 22 service sdcard /system/bin/sdcard /data/media 1023 1023 23 class late_start 24 25 <!-- The <device> element should contain one or more <storage> elements. 26 Exactly one of these should have the attribute primary="true". 27 This storage will be the primary external storage and should have path="/mnt/sdcard". 28 Each storage should have both a path and description attribute set. 29 The following boolean attributes are optional: 30 31 primary: this storage is the primary external storage 32 removable: this is removable storage (for example, a real SD card) 33 emulated: the storage is emulated via the FUSE sdcard daemon 34 mtp-reserve: number of megabytes of storage MTP should reserve for free storage//-----------!!!!!! 35 (used for emulated storage that is shared with system's data partition) 36 37 A storage should not have both emulated and removable set to true 38 --> 39 40 <StorageList xmlns:android="http://schemas.android.com/apk/res/android"> 41 <storage android:mountPoint="/storage/sdcard0" 42 android:storageDescription="@string/storage_internal" 43 android:primary="true" 44 android:emulated="true" 45 android:mtpReserve="100" />//-------------------!!!!!!! 46 </StorageList>
# create virtual SD card at /storage/sdcard0, based on the /data/media directory
# daemon will drop to user/group system/media_rw after initializing
# underlying files in /data/media will be created with user and group media_rw (1023)
service sdcard /system/bin/sdcard /data/media 1023 1023//----------通过data/media目录访问sdcard0内部文件
class late_start
浙公网安备 33010602011771号