#define PERSISTENT_PROPERTY_DIR "/data/property" static void write_persistent_property(const char *name, const char *value) { const char *tempPath = PERSISTENT_PROPERTY_DIR "/.temp"; char path[PATH_MAX]; int fd, length; snprintf(path, sizeof(path), "%s/%s", PERSISTENT_PROPERTY_DIR, name); fd = open(tempPath, O_WRONLY|O_CREAT|O_TRUNC, 0600); if (fd < 0) { ERROR("Unable to write persistent property to temp file %s errno: %d\n", tempPath, errno); return; } write(fd, value, strlen(value)); close(fd); if (rename(tempPath, path)) { unlink(tempPath); ERROR("Unable to rename persistent property file %s to %s\n", tempPath, path); } }
加载永久属性时,会读入在目录/data/property下所有名字以persist.开头的文件内容,作为该名字对应的属性值。