安卓软件栈
framework层
StorageManager
frameworks/base/core/java/android/os/storage/StorageManager.java
java libcore
有个java.io.file在路径:
libcore/ojluni/src/main/java/java/io/File.java
里面有文件相关操作:
public boolean createNewFile() throws IOException {
SecurityManager security = System.getSecurityManager();
if (security != null) security.checkWrite(path);
if (isInvalid()) {
throw new IOException("Invalid file path");
}
return fs.createFileExclusively(path);
}
/**
* readObject is called to restore this filename.
* The original separator character is read. If it is different
* than the separator character on this system, then the old separator
* is replaced by the local separator.
*/
private synchronized void readObject(java.io.ObjectInputStream s)
throws IOException, ClassNotFoundException
{
ObjectInputStream.GetField fields = s.readFields();
String pathField = (String)fields.get("path", null);
char sep = s.readChar(); // read the previous separator char
if (sep != separatorChar)
pathField = pathField.replace(sep, separatorChar);
String path = fs.normalize(pathField);
UNSAFE.putObject(this, PATH_OFFSET, path);
UNSAFE.putIntVolatile(this, PREFIX_LENGTH_OFFSET, fs.prefixLength(path));
}
另外一个类:
frameworks/base/core/java/android/content/ContextWrapper.java
frameworks/base/core/java/android/app/ContextImpl.java:
@Override
public FileOutputStream openFileOutput(String name, int mode) throws FileNotFoundException {
checkMode(mode);
final boolean append = (mode&MODE_APPEND) != 0;
File f = makeFilename(getFilesDir(), name);
try {
FileOutputStream fos = new FileOutputStream(f, append);
setFilePermissionsFromMode(f.getPath(), mode, 0);
return fos;
} catch (FileNotFoundException e) {
}
File parent = f.getParentFile();
parent.mkdir();
FileUtils.setPermissions(
parent.getPath(),
FileUtils.S_IRWXU|FileUtils.S_IRWXG|FileUtils.S_IXOTH,
-1, -1);
FileOutputStream fos = new FileOutputStream(f, append);
setFilePermissionsFromMode(f.getPath(), mode, 0);
return fos;
}
According to this slide deck from 2016, Libcore is Google's implementation of some core Java libraries such as java.net, java.util, java.icu (Unicode), java.math, java.reflect, and possibly more. Libcore also allows for POSIX system calls from Java.Libcore's master branch can be found here. Most of Google's implementation is found in folder luni , but libcore also mixes with Oracle code. For example, Oracle's JNI implementation from OpenJDK is used in Android in folder ojluni. So libcore replaces some functionality from OpenJDK, but not all.
libc库
源码位置:bionic/libc/stdio/stdio.cpp
例如fopen:
FILE* fopen(const char* file, const char* mode) {
int mode_flags;
int flags = __sflags(mode, &mode_flags);
if (flags == 0) return nullptr;
int fd = open(file, mode_flags, DEFFILEMODE);
if (fd == -1) {
return nullptr;
}
FILE* fp = __FILE_init(__sfp(), fd, flags);
if (fp == nullptr) {
ErrnoRestorer errno_restorer;
close(fd);
return nullptr;
}
里面的open就会最终调用到read系统调用。
libc里面调用的系统调用,根据这个文件里的系统调用列表,会用python脚本生成头文件:
https://android.googlesource.com/platform/bionic/+/refs/heads/master/libc/SYSCALLS.TXT
# This file is used to automatically generate bionic's system call stubs.
#
# Each non-blank, non-comment line has the following format:
#
# return_type func_name[|alias_list][:syscall_name[:socketcall_id]]([parameter_list]) arch_list
#
# where:
# arch_list ::= "all" | arches
# arches ::= arch | arch "," arches
# arch ::= "arm" | "arm64" | "x86" | "x86_64" | "lp32" | "lp64"
#
# Note:
# - syscall_name corresponds to the name of the syscall, which may differ from
# the exported function name (example: the exit syscall is implemented by the _exit()
# function, which is not the same as the standard C exit() function which calls it)
#
# - alias_list is optional comma separated list of function aliases.
#
# - The call_id parameter, given that func_name and syscall_name have
# been provided, allows the user to specify dispatch style syscalls.
# For example, socket() syscall on i386 actually becomes:
# socketcall(__NR_socket, 1, *(rest of args on stack)).
#
# - Each parameter type is assumed to be stored in 32 bits.
#
# This file is processed by a python script named gensyscalls.py, run via
# genrules in Android.bp.
引用