Android支持挂载NTFS文件系统

硬件环境:NanoPC-T3 Plus

代码链接:https://github.com/gk969/android-ntfs

下述修改项从git diff信息修改而来,行首+表示增加行,-表示删除行。

project build/
diff –git a/target/product/core_base.mk b/target/product/core_base.mk
增加ntfs-3g相关工具到构建脚本core_base.mk

PRODUCT_PACKAGES += \
     libstagefright_soft_vpxenc \
     libvariablespeed \
     libwebrtc_audio_preprocessing \
+    ntfs-3g \
+    ntfsfix \
+    mkntfs \
     mdnsd \
     requestsync \
     wifi-service

project device/friendly-arm/nanopi3/
diff –git a/fstab.nanopi3.emmc b/fstab.nanopi3.emmc
修改fstab。放宽设备路径,匹配所有USB端口和通过usb集线器级联拓展的接口。

 /devices/platform/dw_mmc.0/mmc_host/mmc2/mmc2       /storage/sdcard1    auto      defaults    voldmanaged=sdcard1:auto,noemulatedsd
-/devices/platform/nxp-ehci/usb1/1-1/1-1:1.0         /storage/usbdisk1   auto      defaults    voldmanaged=usbdisk1:auto,noemulatedsd
-/devices/platform/nxp-ehci/usb1/1-1/1-1.1           /storage/usbdisk1   auto      defaults    voldmanaged=usbdisk1:auto,noemulatedsd
-/devices/platform/nxp-ehci/usb1/1-1/1-1.2           /storage/usbdisk2   auto      defaults    voldmanaged=usbdisk2:auto,noemulatedsd
-/devices/platform/nxp-ehci/usb1/1-1/1-1.3           /storage/usbdisk3   auto      defaults    voldmanaged=usbdisk3:auto,noemulatedsd
-/devices/platform/nxp-ehci/usb1/1-1/1-1.4           /storage/usbdisk4   auto      defaults    voldmanaged=usbdisk4:auto,noemulatedsd
-/devices/platform/dwc_otg/usb2/2-0                  /storage/usbdisk5   vfat      defaults    voldmanaged=usbdisk5:auto,noemulatedsd
-/devices/platform/dwc_otg/usb2/2-1                  /storage/usbdisk6   vfat      defaults    voldmanaged=usbdisk6:auto,noemulatedsd
+/devices/platform/nxp-ehci/usb1/                    /storage/usbdisk1   auto      defaults    voldmanaged=usbdisk1:auto,noemulatedsd
+/devices/platform/nxp-ehci/usb1/                    /storage/usbdisk2   auto      defaults    voldmanaged=usbdisk2:auto,noemulatedsd
+/devices/platform/nxp-ehci/usb1/                    /storage/usbdisk3   auto      defaults    voldmanaged=usbdisk3:auto,noemulatedsd
+/devices/platform/nxp-ehci/usb1/                    /storage/usbdisk4   auto      defaults    voldmanaged=usbdisk4:auto,noemulatedsd
+/devices/platform/nxp-ehci/usb1/                    /storage/usbdisk5   auto      defaults    voldmanaged=usbdisk5:auto,noemulatedsd
+/devices/platform/nxp-ehci/usb1/                    /storage/usbdisk6   auto      defaults    voldmanaged=usbdisk6:auto,noemulatedsd

diff –git a/init.nanopi3.rc b/init.nanopi3.rc
修改初始化脚本init.nanopi3.rc。放宽usb移动硬盘访问权限。

     # See storage config details at http://source.android.com/tech/storage/
-    mkdir /mnt/shell/emulated 0700 shell shell
-    mkdir /storage/emulated 0555 root root
+    mkdir /mnt/shell/emulated 0777 shell shell
+    mkdir /storage/emulated 0555 media_rw media_rw
 
     export EXTERNAL_STORAGE /storage/emulated/legacy
     export EMULATED_STORAGE_SOURCE /mnt/shell/emulated
@@ -19,32 +19,32 @@ on init
     symlink /mnt/shell/emulated/0 /storage/emulated/legacy
 
     # external sdcard
-    mkdir /mnt/media_rw/sdcard1 0700 media_rw media_rw
-    mkdir /storage/sdcard1 0700 root root
+    mkdir /mnt/media_rw/sdcard1 0777 media_rw media_rw
+    mkdir /storage/sdcard1 0777 media_rw media_rw
 
     # usb disk1
-    mkdir /mnt/media_rw/usbdisk1 0700 media_rw media_rw
-    mkdir /storage/usbdisk1 0700 root root
+    mkdir /mnt/media_rw/usbdisk1 0777 media_rw media_rw
+    mkdir /storage/usbdisk1 0777 media_rw media_rw
 
     # usb disk2
-    mkdir /mnt/media_rw/usbdisk2 0700 media_rw media_rw
-    mkdir /storage/usbdisk2 0700 root root
+    mkdir /mnt/media_rw/usbdisk2 0777 media_rw media_rw
+    mkdir /storage/usbdisk2 0777 media_rw media_rw
 
     # usb disk3
-    mkdir /mnt/media_rw/usbdisk3 0700 media_rw media_rw
-    mkdir /storage/usbdisk3 0700 root root
+    mkdir /mnt/media_rw/usbdisk3 0777 media_rw media_rw
+    mkdir /storage/usbdisk3 0777 media_rw media_rw

project frameworks/base/
diff –git a/data/etc/platform.xml b/data/etc/platform.xml
放宽权限组设置

     <permission name="android.permission.WRITE_EXTERNAL_STORAGE" >
         <group gid="sdcard_r" />
         <group gid="sdcard_rw" />
+        <group gid="media_rw" />
     </permission>
 
     <permission name="android.permission.ACCESS_ALL_EXTERNAL_STORAGE" >
         <group gid="sdcard_r" />
         <group gid="sdcard_rw" />
         <group gid="sdcard_all" />
+        <group gid="media_rw" />
     </permission>

project system/vold/
diff –git a/Android.mk b/Android.mk
为vold构建脚本增加ntfs模块

common_src_files := \
 	Process.cpp \
 	Ext4.cpp \
 	Fat.cpp \
+	Ntfs.cpp \
 	Loop.cpp \
 	Devmapper.cpp \
 	ResponseCode.cpp \
LOCAL_SHARED_LIBRARIES := $(common_shared_libraries)
 LOCAL_STATIC_LIBRARIES := $(common_static_libraries)
 
 LOCAL_MODULE_TAGS := eng tests
+LOCAL_CFLAGS += -DHAS_NTFS_3G
 
 include $(BUILD_STATIC_LIBRARY)
 
LOCAL_SRC_FILES := \
 LOCAL_C_INCLUDES := $(common_c_includes)
 
 LOCAL_CFLAGS := -Werror=format
+LOCAL_CFLAGS += -DHAS_NTFS_3G
 
 ifeq ($(TARGET_BOARD_PLATFORM), slsiap)
 LOCAL_CFLAGS += -DPATCH_FOR_SLSIAP

fstab每一行对应一个Volume。DirectVolume继承自Volume。DirectVolume::handleBlockEvent(NetlinkEvent *evt)通过fstab中的设备路径匹配Volume及挂载点路径。放宽设备匹配字符串后,不再严格按照设备路径一对一匹配挂载点,实现自适应动态挂载。挂载时根据挂载点是否空闲状态判断是否允许挂载。卸载时根据设备路径找到相应挂载点。
diff –git a/DirectVolume.cpp b/DirectVolume.cpp

 int DirectVolume::handleBlockEvent(NetlinkEvent *evt) {
     const char *dp = evt->findParam("DEVPATH");
-
+    SLOGD("DirectVolume %s state %s handleBlockEvent devpath %s", getLabel(), getStateStr(), dp);
+    
     PathCollection::iterator  it;
     for (it = mPaths->begin(); it != mPaths->end(); ++it) {
         if ((*it)->match(dp)) {
@@ -140,6 +147,16 @@ int DirectVolume::handleBlockEvent(NetlinkEvent *evt) {
             const char *devtype = evt->findParam("DEVTYPE");
 
             if (action == NetlinkEvent::NlActionAdd) {
+                int st = getState();
+                if(!(st == Volume::State_NoMedia || st == Volume::State_Pending || st == Volume::State_Idle)){
+                    errno = ENODEV;
+                    return -1;
+                }
+                SLOGD("DirectVolume %s NlActionAdd @ %s", getLabel(), dp);
+                
+                free(mDevPath);
+                mDevPath = strdup(dp);
+                
                 int major = atoi(evt->findParam("MAJOR"));
                 int minor = atoi(evt->findParam("MINOR"));
                 char nodepath[255];
@@ -167,12 +184,22 @@ int DirectVolume::handleBlockEvent(NetlinkEvent *evt) {
                                                          msg, false);
                 }
             } else if (action == NetlinkEvent::NlActionRemove) {
+                if(mDevPath != NULL){
+                    if(strcmp(dp, mDevPath)){
+                        errno = ENODEV;
+                        return -1;
+                    }
+                }
+                
+                SLOGD("DirectVolume %s NlActionRemove @ %s", getLabel(), dp);
+                
                 if (!strcmp(devtype, "disk")) {
                     handleDiskRemoved(dp, evt);
                 } else {
                     handlePartitionRemoved(dp, evt);
                 }
             } else if (action == NetlinkEvent::NlActionChange) {
+                SLOGD("DirectVolume %s NlActionChange", getLabel());
                 if (!strcmp(devtype, "disk")) {
                     handleDiskChanged(dp, evt);
                 } else {

发表评论

电子邮件地址不会被公开。

*