android USES java and c to find the of sd card mount path
- 2020-05-27 07:07:11
- OfStack
Method 1:
Analyze the return information of the mount command, for example:
$ mount
rootfs / rootfs ro,relatime 0 0
tmpfs /dev tmpfs rw,nosuid,relatime,mode=755 0 0
devpts /dev/pts devpts rw,relatime,mode=600 0 0
proc /proc proc rw,relatime 0 0
sysfs /sys sysfs rw,relatime 0 0
debugfs /sys/kernel/debug debugfs rw,relatime 0 0
none /acct cgroup rw,relatime,cpuacct 0 0
tmpfs /mnt/asec tmpfs rw,relatime,mode=755,gid=1000 0 0
tmpfs /mnt/obb tmpfs rw,relatime,mode=755,gid=1000 0 0
none /dev/cpuctl cgroup rw,relatime,cpu 0 0
/dev/block/platform/sdhci-tegra.3/by-name/system /system ext4 ro,relatime,barrier=1,data=ordered 0 0
/dev/block/platform/sdhci-tegra.3/by-name/userdata /data ext4 rw,nosuid,nodev,noatime,barrier=1,data=ordered 0 0
/dev/block/platform/sdhci-tegra.3/by-name/cache /cache ext4 rw,nosuid,nodev,noatime,barrier=1,data=ordered 0 0
/dev/block/platform/sdhci-tegra.3/by-name/pdsb /pds ext2 ro,relatime 0 0
/dev/fuse /mnt/sdcard fuse rw,nosuid,nodev,relatime,user_id=1023,group_id=1023,default_permissions,allow_other 0 0
/dev/block/vold/179:9 /mnt/sdcard-ext vfat rw,dirsync,nosuid,nodev,noexec,relatime,uid=1000,gid=1015,fmask=0702,dmask=0702,allow_utime=0020,codepage=cp437,iocharset=iso8859-1,shortname=mixed,utf8,errors=remount-ro 0 0
/dev/block/vold/179:9 /mnt/secure/asec vfat rw,dirsync,nosuid,nodev,noexec,relatime,uid=1000,gid=1015,fmask=0702,dmask=0702,allow_utime=0020,codepage=cp437,iocharset=iso8859-1,shortname=mixed,utf8,errors=remount-ro 0 0
tmpfs /mnt/sdcard-ext/.android_secure tmpfs ro,relatime,size=0k,mode=000 0 0
Java Version code is as follows:
// to C/C++ The written Shared library callback gets all SD The function of the card path
public String
GetAllSDPath() throws TokenException
{
String strMountInfo = "";
// 1. First get the file system information that the system has loaded
try
{
// Create the system process generator object
ProcessBuilder objProcessBuilder = new ProcessBuilder();
// perform mount -h You can see mount : list mounted filesystems
// This command lists the loaded file systems
objProcessBuilder.command( "mount" ); // The new operating system program and its parameters
// Set the error output to merge with standard output
objProcessBuilder.redirectErrorStream( true );
// Start based on the state of the current system process generator 1 Two new processes, and returns an instance of the process
Process objProcess = objProcessBuilder.start();
// Returns the return value of the local operating system program when the thread is blocked to the end of execution of the local operating system program
objProcess.waitFor();
// Gets the input stream of the process object, which to the process object is the standard output stream of the local operating system program (stdout) Connected to the
InputStream objInputStream = objProcess.getInputStream();
byte[] buffer = new byte[1024];
// read mount The message text returned by the command program
while ( -1 != objInputStream.read( buffer ) )
{
strMountInfo = strMountInfo + new String( buffer );
}
// Closes the input stream of the process object
objInputStream.close();
// Terminate the process and release any flow associated with it
objProcess.destroy();
}
catch ( Exception e )
{
e.printStackTrace();
}
// 2. Then look it up in the file system information that the system has loaded SD The card path
// mount The loaded file system information returned is 1 line 1 In the form of information,
// So split the string first with a newline
String[] lines = strMountInfo.split( "\n" );
// Empty the string object and use it to load the really useful ones SD Card path list
strMountInfo = "";
for ( int i = 0;
i < lines.length;
i++ )
{
// If there is /mnt/ and vfat String indicating that it may be inside / The outer SD The mount path of the card
if ( -1 != lines[i].indexOf( " /mnt/" ) && // Put a space in front of it to prevent it from being taken out of context
-1 != lines[i].indexOf( " vfat " ) ) // There are Spaces before and after
{
// Split the string with a space separator
String[] blocks = lines[i].split( "\\s" ); // \\s Is a space character
for ( int j = 0;
j < blocks.length;
j++ )
{
// If the string contains /mnt/ A string that says maybe that's what we're looking for SD The card mount path
if ( -1 != blocks[j].indexOf( "/mnt/" ) )
{
// Eliminate duplicate paths
if ( -1 == strMountInfo.indexOf( blocks[j] ) )
{
// Use a semicolon character (;) separated SD Card path list,
strMountInfo += blocks[j] + ";";
}
}
}
}
}
return strMountInfo;
}
The code of C is as follows:
char caStdOutLine[1024]; // mount In the standard output of the command 1 Line information
char* pcTmpSDPath = NULL;
// Then use mount Command to find the identity authentication lock
do // Non-circular, just to facilitate the control of the branch hierarchy, to facilitate the control of the branch flow
{
// By creating a 1 Two pipes, call fork produce 1 Child process,
// perform 1 a shell Start by running the command 1 A process.
// This process must be completed by pclose() The function closes.
FILE* fp = popen( "mount", // 1 A point to NULL The end of the shell A pointer to a command string,
// This command will be passed bin/sh And use -c Mark,
// then shell The command will be executed to read from the string.
"r" ); // The file pointer is connected to shell Standard output of the command
if ( NULL == fp )
{
break;
}
while( NULL != fgets( caStdOutLine,
sizeof( caStdOutLine ),
fp ) )
{
// if You found what you wanted SD The card mount path ,
if ( Judge conditions )
{
// Note: data in pipeline 1 Make sure you read it, or you'll crash
continue; // I won't try again 1 Mount address
}
// If there is /mnt/ and vfat String indicating that it may be inside / The outer SD The mount path of the card
if ( NULL == strstr( caStdOutLine, " /mnt/" ) && // Put a space in front of it to prevent it from being taken out of context
NULL == strstr( caStdOutLine, " /storage/" ) ) // Put a space in front of it to prevent it from being taken out of context
{
continue; // The row is not inside if it doesn't satisfy the condition / The outer SD The mount path of the card
}
if ( NULL == strstr( caStdOutLine, " vfat " ) ) // There are Spaces before and after
{
continue; // The row is not inside if it doesn't satisfy the condition / The outer SD The mount path of the card
}
// Split the string with a space separator
pcTmpSDPath = strtok( caStdOutLine, " " );
do // Here's the loop, try each 1 A path
{
if ( ( NULL == pcTmpSDPath ) ||
( '\0' == *pcTmpSDPath ) )
{
continue;
}
// If the string contains /mnt/ A string that says maybe that's what we're looking for SD The card mount path
if ( NULL == strstr( pcTmpSDPath, "/mnt/" ) &&
NULL == strstr( pcTmpSDPath, "/storage/" ) )
{
continue;
}
// TODO: Add the pair here SD Card path USES statements if only used within them 1 Don't forget to set the Settings you want SD The id of the card path
}while ( pcTmpSDPath = strtok( NULL, " " ) );
}
// Close the standard I/O Flow, wait for the command to finish, and then return shell The termination state.
// if shell Can't be executed,
// the pclose() Returns the termination status and shell Has been performed exit 1 The sample.
pclose( fp );
}while ( 0 );
Method 2:
Analysis cat system/etc/vold fstab command return information, such as:
$ cat /system/etc/vold.fstab
## Vold 2.0 fstab for Stingray
#######################
## Regular device mount
##
## Format: dev_mount <label> <mount_point> <part> <sysfs_path1...>
## label - Label for the volume
## mount_point - Where the volume will be mounted
## part - Partition # (1 based), or 'auto' for first usable partition.
## <sysfs_path> - List of sysfs paths to source devices
######################
# external sd card
dev_mount sdcard-ext /mnt/sdcard-ext auto /devices/platform/sdhci-tegra.2/mmc_host/mmc1 /devices/platform/sdhci-tegra.2/mmc_host/mmc2
# flash drive connection to USB1
dev_mount usbdisk_1.0 /mnt/usbdisk_1.0 auto /devices/platform/tegra-ehci.0/usb2/2-1/2-1:1.0
# flash drive connection through hub connected to USB1
dev_mount usbdisk_1.1 /mnt/usbdisk_1.1 auto /devices/platform/tegra-ehci.0/usb2/2-1/2-1.1
dev_mount usbdisk_1.2 /mnt/usbdisk_1.2 auto /devices/platform/tegra-ehci.0/usb2/2-1/2-1.2
dev_mount usbdisk_1.3 /mnt/usbdisk_1.3 auto /devices/platform/tegra-ehci.0/usb2/2-1/2-1.3
dev_mount usbdisk_1.4 /mnt/usbdisk_1.4 auto /devices/platform/tegra-ehci.0/usb2/2-1/2-1.4
dev_mount usbdisk_1.5 /mnt/usbdisk_1.5 auto /devices/platform/tegra-ehci.0/usb2/2-1/2-1.5
dev_mount usbdisk_1.6 /mnt/usbdisk_1.6 auto /devices/platform/tegra-ehci.0/usb2/2-1/2-1.6
dev_mount usbdisk_1.7 /mnt/usbdisk_1.7 auto /devices/platform/tegra-ehci.0/usb2/2-1/2-1.7
C Version code is as follows:
char caStdOutLine[1024]; // cat In the standard output of the command 1 Line information
char* pcTmpSDPath = NULL;
char* pcNotSpace = NULL;
// with /system/etc/vold.fstab To obtain the SD The card path looks for the identity lock
do // Non-circular, just to facilitate the control of the branch hierarchy, to facilitate the control of the branch flow
{
// By creating a 1 Two pipes, call fork produce 1 Child process,
// perform 1 a shell Start by running the command 1 A process.
// This process must be completed by pclose() The function closes.
FILE* fp = popen( "cat /system/etc/vold.fstab", // 1 A point to NULL The end of the shell A pointer to a command string,
// This command will be passed bin/sh And use -c Mark,
// then shell The command will be executed to read from the string.
"r" ); // The file pointer is connected to shell Standard output of the command
if ( NULL == fp )
{
break;
}
while( NULL != fgets( caStdOutLine,
sizeof( caStdOutLine ),
fp ) )
{
// if You found what you wanted SD The card mount path ,
if ( Judge conditions )
{
// Note: data in pipeline 1 Make sure you read it, or you'll crash
continue; // I won't try again 1 Mount address
}
// Format: dev_mount <label> <mount_point> <part> <sysfs_path1...>
// Remove the leading space
pcNotSpace = caStdOutLine + strspn( caStdOutLine, " " );
if ( NULL == pcNotSpace ||
'\0' == *pcNotSpace ||
'#' == *pcNotSpace || // The first character of the line is # The instructions are comment lines
'd' != pcNotSpace[0] || // It doesn't start with dev_mount
'e' != pcNotSpace[1] ||
'v' != pcNotSpace[2] ||
'_' != pcNotSpace[3] ||
'm' != pcNotSpace[4] ||
'o' != pcNotSpace[5] ||
'u' != pcNotSpace[6] ||
'n' != pcNotSpace[7] ||
't' != pcNotSpace[8] )
{
continue; // The row is not inside if it doesn't satisfy the condition / The outer SD The mount path of the card
}
// Split the string with a space separator
pcTmpSDPath = strtok( pcNotSpace, " " );
do // Here's the loop, try each 1 A path
{
if ( ( NULL == pcTmpSDPath ) ||
( '\0' == *pcTmpSDPath ) )
{
continue;
}
// If the string contains /mnt/ A string that says maybe that's what we're looking for SD The card mount path
if ( NULL == strstr( pcTmpSDPath, "/mnt/" ) &&
NULL == strstr( pcTmpSDPath, "/storage/" ) )
{
continue;
}
// TODO: Add the pair here SD Card path USES statements if only used within them 1 Don't forget to set the Settings you want SD The id of the card path
}while ( pcTmpSDPath = strtok( NULL, " " ) );
}
// Close the standard I/O Flow, wait for the command to finish, and then return shell The termination state.
// if shell Can't be executed,
// the pclose() Returns the termination status and shell Has been performed exit 1 The sample.
pclose( fp );
}while ( 0 );