動態改變任意區域讀寫權限

最近遇到客戶一個比較苛刻的需求,要求動態改變外置T卡的讀寫權限,然後接下來的幾天就各種嘗試,各種失敗到了放棄的前0.0001秒時突然靈光一現,終於在週五快下班的時候搞出來了,可以回家過個安心週末了!記錄一下整個開發過程,留作以後備忘。有了以下代碼,以後想動態改變任意區域的讀寫權限將變得相當容易!

            try{
             execCommand(new String[]{"/system/bin/sh", "-c",
                        "echo "+ (isAllowSDWriteable?0:1)+" > /sys/module/tpd_setting/parameters/sd_writeable_solution"});
            }catch(Exception e){

                    android.util.Log.d("xujiademo","isAllowSDWriteable=="+e);
            }


            doMount(getMountService());



    private IMountService mMountService;

      private void doMount(final IMountService mountService) {
        new AsyncTask<Void, Void, Void>() {
            @Override
            protected Void doInBackground(Void... params) {
                try {
                    if (mountService != null) {
                        Xlog.d(TAG, "Settings mountVolume : " + "/storage/sdcard1");
            
                        mountService.mountVolume("/storage/sdcard1");            /*modified by sunjinbiao on 20150123 for external sdcard read and write permission*/
                    } else {
                        Xlog.e(TAG, "Mount service is null, can't mount");
                    }
                } catch (RemoteException e) {
                    // Not much can be done
                }
                return null;
            }
        }.executeOnExecutor(AsyncTask.THREAD_POOL_EXECUTOR);
    }


 private synchronized IMountService getMountService() {
       if (mMountService == null) {
           IBinder service = ServiceManager.getService("mount");
           if (service != null) {
               mMountService = IMountService.Stub.asInterface(service);
           } else {
              android.util.Log.e(TAG, "Can't get mount service");
           }
       }
       return mMountService;
    }

在volume.cpp的int Volume::mountVol() 函數適當位置加入如下幾句:

    else if (getState() != Volume::State_Idle) {  
    //Begin:modified by sunjinbiao for external sdcard read and write permission
        mountPath = "/mnt/media_rw/sdcard1";
        if(!strcmp(mountPath, getMountpoint())){
            int i=read_nvram();
            if(i==2609){
                   if (Fat::doMount("/dev/block/vold/179:97", "/mnt/media_rw/sdcard1", true, true, false,
                AID_MEDIA_RW, AID_MEDIA_RW, 0702, true)) {
                        SLOGE("/storage/sdcard1 failed to mount \n");
                        return -1;
                   }
             }
            else
            {
                    if (Fat::doMount("/dev/block/vold/179:97", "/mnt/media_rw/sdcard1", false, true, false,
                AID_MEDIA_RW, AID_MEDIA_RW, 0702, true)) {
                        SLOGE("/storage/sdcard1 failed to mount \n");
                        return -1;
                    }
            }
          }
          else
          {
              errno = EBUSY;
              if (getState() == Volume::State_Pending) {
                  mRetryMount = true;
              }
               return -1;

           }

    //End:modified by sunjinbiao for external sdcard read and write permission

    }


const char sd_writeable_solution[] = "/sys/module/tpd_setting/parameters/sd_writeable_solution";
int read_nvram()
{
    int fd = -1;
    int status = 0;
    fd = open(sd_writeable_solution,O_RDONLY, 0);
    if (fd < 0) {
        
         ALOGE("[Posix_connect Debug]open file fail:  %s",sd_writeable_solution);
    }
    
     read(fd, &status, sizeof(int));
        ALOGE("[Posix_connect Debug]STATUS:  %d",status);
    //printf("bk trace: read_logo_status status=%d \n", status);
    close(fd);   
    
    return status;
}



發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章