2015-11-27 17:29:37 +08:00
|
|
|
#include "fs.h"
|
|
|
|
|
2014-03-12 09:28:15 +08:00
|
|
|
#include "fastboot.h"
|
2014-06-18 08:01:14 +08:00
|
|
|
#include "make_f2fs.h"
|
2014-03-12 09:28:15 +08:00
|
|
|
|
|
|
|
#include <errno.h>
|
|
|
|
#include <stdio.h>
|
|
|
|
#include <stdlib.h>
|
|
|
|
#include <string.h>
|
|
|
|
#include <sys/stat.h>
|
|
|
|
#include <sys/types.h>
|
|
|
|
#include <unistd.h>
|
|
|
|
|
2016-10-06 08:53:30 +08:00
|
|
|
#include <ext4_utils/make_ext4fs.h>
|
2015-04-08 11:12:50 +08:00
|
|
|
#include <sparse/sparse.h>
|
2014-03-12 09:28:15 +08:00
|
|
|
|
2015-11-27 17:29:37 +08:00
|
|
|
static int generate_ext4_image(int fd, long long partSize, const std::string& initial_dir)
|
2014-03-12 09:28:15 +08:00
|
|
|
{
|
2015-11-27 17:29:37 +08:00
|
|
|
if (initial_dir.empty()) {
|
|
|
|
make_ext4fs_sparse_fd(fd, partSize, NULL, NULL);
|
|
|
|
} else {
|
|
|
|
make_ext4fs_sparse_fd_directory(fd, partSize, NULL, NULL, initial_dir.c_str());
|
|
|
|
}
|
2014-03-12 09:28:15 +08:00
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2014-06-18 14:43:18 +08:00
|
|
|
#ifdef USE_F2FS
|
2015-11-27 17:29:37 +08:00
|
|
|
static int generate_f2fs_image(int fd, long long partSize, const std::string& initial_dir)
|
2014-06-18 08:01:14 +08:00
|
|
|
{
|
2015-11-27 17:29:37 +08:00
|
|
|
if (!initial_dir.empty()) {
|
|
|
|
fprintf(stderr, "Unable to set initial directory on F2FS filesystem\n");
|
|
|
|
return -1;
|
|
|
|
}
|
2014-06-18 14:43:18 +08:00
|
|
|
return make_f2fs_sparse_fd(fd, partSize, NULL, NULL);
|
2014-06-18 08:01:14 +08:00
|
|
|
}
|
2014-06-18 14:43:18 +08:00
|
|
|
#endif
|
2014-06-18 08:01:14 +08:00
|
|
|
|
2014-03-12 09:28:15 +08:00
|
|
|
static const struct fs_generator {
|
2015-06-24 11:27:58 +08:00
|
|
|
const char* fs_type; //must match what fastboot reports for partition type
|
2015-11-27 17:29:37 +08:00
|
|
|
|
|
|
|
//returns 0 or error value
|
|
|
|
int (*generate)(int fd, long long partSize, const std::string& initial_dir);
|
2014-03-12 09:28:15 +08:00
|
|
|
|
|
|
|
} generators[] = {
|
2014-06-18 08:01:14 +08:00
|
|
|
{ "ext4", generate_ext4_image},
|
2014-06-18 14:43:18 +08:00
|
|
|
#ifdef USE_F2FS
|
2014-06-18 08:01:14 +08:00
|
|
|
{ "f2fs", generate_f2fs_image},
|
2014-06-18 14:43:18 +08:00
|
|
|
#endif
|
2014-03-12 09:28:15 +08:00
|
|
|
};
|
|
|
|
|
2015-11-03 06:05:57 +08:00
|
|
|
const struct fs_generator* fs_get_generator(const std::string& fs_type) {
|
2015-04-08 11:12:50 +08:00
|
|
|
for (size_t i = 0; i < sizeof(generators) / sizeof(*generators); i++) {
|
2015-11-03 06:05:57 +08:00
|
|
|
if (fs_type == generators[i].fs_type) {
|
2014-03-12 09:28:15 +08:00
|
|
|
return generators + i;
|
2015-04-08 11:12:50 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
return nullptr;
|
2014-03-12 09:28:15 +08:00
|
|
|
}
|
|
|
|
|
2015-11-27 17:29:37 +08:00
|
|
|
int fs_generator_generate(const struct fs_generator* gen, int tmpFileNo, long long partSize,
|
|
|
|
const std::string& initial_dir)
|
2014-03-12 09:28:15 +08:00
|
|
|
{
|
2015-11-27 17:29:37 +08:00
|
|
|
return gen->generate(tmpFileNo, partSize, initial_dir);
|
2014-03-12 09:28:15 +08:00
|
|
|
}
|