Android uses Java and C to find the SD card mount path (SD card path) method _android

Source: Internet
Author: User
Tags mixed strtok

Method One:

Analyze the return information for the Mount command, for example:

Copy Code code as follows:

$ 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

The Java version of the code is as follows:
A function to obtain all SD card paths to a shared library callback written by C + +
Public String
Getallsdpath () throws Tokenexception
{
String strmountinfo = "";

1. First get the system loaded file system Information
Try
{
To create a system process Builder object
Processbuilder Objprocessbuilder = new Processbuilder ();
Executive Mount-h can see Mount:list mounted filesystems
This command lists the file systems that have been loaded
Objprocessbuilder.command ("mount"); The new operating system program and its parameters
Setting error output will be merged with standard output
Objprocessbuilder.redirecterrorstream (TRUE);
Starts a new process based on the state of the current system process builder and returns the process instance
Process objprocess = Objprocessbuilder.start ();
Blocking the thread to the end of the local operating system program, returning the return value of the local operating system program
Objprocess.waitfor ();

Gets the input stream of the process object that is already connected to the standard output stream (stdout) of the local operating system program for the process object.
InputStream Objinputstream = Objprocess.getinputstream ();

byte[] buffer = new byte[1024];

Read the message text returned by the Mount command program
while (-1!= objinputstream.read (buffer))
{
Strmountinfo = Strmountinfo + new String (buffer);
}

To close the input stream for a process object
Objinputstream.close ();

Terminate the process and release any streams associated with it
Objprocess.destroy ();
}
catch (Exception e)
{
E.printstacktrace ();
}

2. Then find the SD card path in the system loaded file system Information
The loaded file system information returned by mount is reflected in one line of information,
So split the string with the newline character first.
String[] lines = strmountinfo.split ("\ n");

Empty the String object and use it to load the list of truly useful SD card paths
Strmountinfo = "";

for (int i = 0;
i < lines.length;
i++)
{
If there are/mnt/and VFAT strings in the line, it may be the mount path of the internal/external SD card
if (-1!= lines[i].indexof ("/mnt/") &&//preceded by a space to prevent out of context
-1!= lines[i].indexof ("VFAT"))//before and after spaces
{
and split the string with the 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 a/mnt/string, it may be the SD card mount Path we are looking for
if (-1!= blocks[j].indexof ("/mnt/"))
{
To exclude duplicate paths
if (-1 = strmountinfo.indexof (Blocks[j]))
{
With a semicolon character (;) separate the SD card path list,
Strmountinfo + = Blocks[j] + ";";
}
}
}
}
}

return strmountinfo;
}

C version of the code is as follows:

Copy Code code as follows:

Char castdoutline[1024]; One line of information in the standard output of the Mount command
char* Pctmpsdpath = NULL;

//re-use the Mount command to obtain the identity authentication lock
do//non loop, just to facilitate control of the branch level, easy to control the branch flow to
{
   //By creating a pipe, call fork to produce a subprocess,
   //execute a Shell to run a command to open a process.
   /This process must be closed by the Pclose () function.
    file* fp = popen (mount,//A pointer to a NULL-terminated shell command string,
     &nbs p;                         //This line of command will be uploaded to Bin/sh and using the-C flag,
                                /Then the shell will execute this command to read from this string.
                       "R");  //file pointer connected to the standard output of the shell command

    if (NULL = fp)
    {
        break;
   }

    while (NULL!= fgets (castdoutline,
                           sizeof ( Castdoutline),
                           fp)
    {
        //If you find the SD card mount path you want,
        if (judgment condition)
         {
           // Note: The data in the pipeline must be read out, or it will break down the
            continue; You don't try the next mount address
       }

If there are/mnt/and VFAT strings in the line, it may be the mount path of the internal/external SD card
if (NULL = = Strstr (castdoutline, "/mnt/") &&//preceded by a space to prevent out of context
NULL = = Strstr (castdoutline, "/storage/"))//preceded by a space to prevent out of context
{
Continue Conditions are not satisfied this line is not an internal/external SD card mount path
}

if (NULL = = Strstr (castdoutline, "VFAT"))//preceded by a space
{
Continue Conditions are not satisfied this line is not an internal/external SD card mount path
}

and split the string with the space separator
Pctmpsdpath = Strtok (Castdoutline, "");

Do//Here is the loop, try every path
{
if (NULL = Pctmpsdpath) | |
(' *pctmpsdpath ' = =)]
{
Continue
}

If the string contains a/mnt/string, it may be the SD card mount Path we are looking for
if (NULL = = Strstr (Pctmpsdpath, "/mnt/") &&
NULL = = Strstr (Pctmpsdpath, "/storage/"))
{
Continue
}

TODO: Add a statement to use for the SD card path here, and if you just use one, don't forget to set the identity of the SD card path you've found

}while (Pctmpsdpath = Strtok (NULL, ""));
}

Close the standard I/O stream, wait for the command to finish, and then return to the shell's termination state.
If the shell cannot be executed,
Then the Pclose () returns the same termination status as the shell has executed exit.
Pclose (FP);

}while (0);



Method Two:
Analyze the return information for the Cat/system/etc/vold.fstab command, for example:
Copy Code code as follows:

$ 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 would be mounted
# # Part-partition # (1 based), or ' auto ' for the 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 of the code is as follows:
Char castdoutline[1024]; One line of information in the standard output of the cat command
char* Pctmpsdpath = NULL;
char* pcnotspace = NULL;

Using SD card path obtained by/system/etc/vold.fstab to find identity authentication lock
Do//non loop, just for easy control of branch level, easy to control branch flow
{
By creating a pipe, call fork to produce a child process,
Executes a shell to run a command to open a process.
This process must be closed by the Pclose () function.
file* fp = popen ("Cat/system/etc/vold.fstab",//A pointer to a NULL-terminated shell command string,
This line of command will be uploaded to Bin/sh and using the-C flag,
Then the shell will execute this command to read from this string.
"R"); Standard output of a file pointer connected to a shell command

if (NULL = fp)
{
Break
}

    while (NULL!= fgets (castdoutline,
                           sizeof ( Castdoutline),
                           fp)
    {
        //If you find the SD card mount path you want,
        if (judgment condition)
         {
           // Note: The data in the pipeline must be read out, or it will break down the
            continue; You don't try the next mount address
       }

Format:dev_mount <label> <mount_point> <part> <sysfs_path1...>
Remove the opening space
Pcnotspace = Castdoutline + strspn (castdoutline, "");

if (NULL = = Pcnotspace | |
' *pcnotspace ' = = | |
' # ' = = *pcnotspace | | The beginning character is # description is a comment line
' d '!= pcnotspace[0] | | The beginning of a sentence is not 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 Conditions are not satisfied this line is not an internal/external SD card mount path
}

and split the string with the space separator
Pctmpsdpath = Strtok (Pcnotspace, "");

Do//Here is the loop, try every path
{
if (NULL = Pctmpsdpath) | |
(' *pctmpsdpath ' = =)]
{
Continue
}

If the string contains a/mnt/string, it may be the SD card mount Path we are looking for
if (NULL = = Strstr (Pctmpsdpath, "/mnt/") &&
NULL = = Strstr (Pctmpsdpath, "/storage/"))
{
Continue
}

TODO: Add a statement to use for the SD card path here, and if you just use one, don't forget to set the identity of the SD card path you've found

}while (Pctmpsdpath = Strtok (NULL, ""));
}

Close the standard I/O stream, wait for the command to finish, and then return to the shell's termination state.
If the shell cannot be executed,
Then the Pclose () returns the same termination status as the shell has executed exit.
Pclose (FP);
}while (0);

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

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.