1 #include "fs.h"
2 
3 #include "fastboot.h"
4 #include "make_ext4fs.h"
5 #include "make_f2fs.h"
6 
7 #include <errno.h>
8 #include <stdio.h>
9 #include <stdlib.h>
10 #include <string.h>
11 #include <sys/stat.h>
12 #include <sys/types.h>
13 #include <unistd.h>
14 
15 #include <sparse/sparse.h>
16 
generate_ext4_image(int fd,long long partSize,const std::string & initial_dir)17 static int generate_ext4_image(int fd, long long partSize, const std::string& initial_dir)
18 {
19     if (initial_dir.empty()) {
20         make_ext4fs_sparse_fd(fd, partSize, NULL, NULL);
21     } else {
22         make_ext4fs_sparse_fd_directory(fd, partSize, NULL, NULL, initial_dir.c_str());
23     }
24     return 0;
25 }
26 
27 #ifdef USE_F2FS
generate_f2fs_image(int fd,long long partSize,const std::string & initial_dir)28 static int generate_f2fs_image(int fd, long long partSize, const std::string& initial_dir)
29 {
30     if (!initial_dir.empty()) {
31         fprintf(stderr, "Unable to set initial directory on F2FS filesystem\n");
32         return -1;
33     }
34     return make_f2fs_sparse_fd(fd, partSize, NULL, NULL);
35 }
36 #endif
37 
38 static const struct fs_generator {
39     const char* fs_type;  //must match what fastboot reports for partition type
40 
41     //returns 0 or error value
42     int (*generate)(int fd, long long partSize, const std::string& initial_dir);
43 
44 } generators[] = {
45     { "ext4", generate_ext4_image},
46 #ifdef USE_F2FS
47     { "f2fs", generate_f2fs_image},
48 #endif
49 };
50 
fs_get_generator(const std::string & fs_type)51 const struct fs_generator* fs_get_generator(const std::string& fs_type) {
52     for (size_t i = 0; i < sizeof(generators) / sizeof(*generators); i++) {
53         if (fs_type == generators[i].fs_type) {
54             return generators + i;
55         }
56     }
57     return nullptr;
58 }
59 
fs_generator_generate(const struct fs_generator * gen,int tmpFileNo,long long partSize,const std::string & initial_dir)60 int fs_generator_generate(const struct fs_generator* gen, int tmpFileNo, long long partSize,
61     const std::string& initial_dir)
62 {
63     return gen->generate(tmpFileNo, partSize, initial_dir);
64 }
65