From 8b6bfea8494e3466a622eb69609d64e89ca425c8 Mon Sep 17 00:00:00 2001 From: Keith Mok Date: Thu, 3 Sep 2015 11:36:51 -0700 Subject: [PATCH] Add darwin support for the host tools There is some symbol name conflict in the make_f2fs and the library it loaded which darwin and linux handle that in a different way. Remove the symbol name conflicts as cannot find a way to set symbol resolving priority in darwin. Change-Id: Iabe5c1b594daacd65b9ec2f694a2b5ab5575cfce --- f2fs_utils/Android.mk | 4 ++-- f2fs_utils/f2fs_dlutils.c | 31 +++++++++++++++++++++++++++++-- f2fs_utils/f2fs_ioutils.c | 15 ++++++++++++++- f2fs_utils/f2fs_utils.c | 28 +++++++++++----------------- 4 files changed, 56 insertions(+), 22 deletions(-) diff --git a/f2fs_utils/Android.mk b/f2fs_utils/Android.mk index 22e12008..a2cebd20 100644 --- a/f2fs_utils/Android.mk +++ b/f2fs_utils/Android.mk @@ -2,7 +2,7 @@ LOCAL_PATH:= $(call my-dir) -ifeq ($(HOST_OS),linux) +ifneq (,$(filter linux darwin,$(HOST_OS))) include $(CLEAR_VARS) LOCAL_MODULE := libf2fs_utils_host @@ -38,7 +38,7 @@ LOCAL_MODULE := make_f2fs LOCAL_LDFLAGS := -ldl -rdynamic # The following libf2fs_* are from system/extras/f2fs_utils, # and do not use code in external/f2fs-tools. -LOCAL_STATIC_LIBRARIES := libf2fs_utils_host libf2fs_ioutils_host libf2fs_dlutils_host +LOCAL_STATIC_LIBRARIES := libf2fs_utils_host libf2fs_dlutils_host LOCAL_REQUIRED_MODULES := libf2fs_fmt_host_dyn LOCAL_STATIC_LIBRARIES += \ libsparse_host \ diff --git a/f2fs_utils/f2fs_dlutils.c b/f2fs_utils/f2fs_dlutils.c index 40be4161..10e49d97 100644 --- a/f2fs_utils/f2fs_dlutils.c +++ b/f2fs_utils/f2fs_dlutils.c @@ -35,10 +35,20 @@ #include #include +#if defined(__linux__) #define F2FS_DYN_LIB "libf2fs_fmt_host_dyn.so" +#elif defined(__APPLE__) && defined(__MACH__) +#define F2FS_DYN_LIB "libf2fs_fmt_host_dyn.dylib" +#else +#error "Not supported OS" +#endif int (*f2fs_format_device_dl)(void); void (*f2fs_init_configuration_dl)(struct f2fs_configuration *); +void (*flush_sparse_buffs_dl)(void); +void (*init_sparse_file_dl)(unsigned int, int64_t); +void (*finalize_sparse_file_dl)(int); +struct f2fs_configuration *f2fs_config; int f2fs_format_device(void) { assert(f2fs_format_device_dl); @@ -48,7 +58,18 @@ void f2fs_init_configuration(struct f2fs_configuration *config) { assert(f2fs_init_configuration_dl); f2fs_init_configuration_dl(config); } - +void flush_sparse_buffs(void) { + assert(flush_sparse_buffs_dl); + return flush_sparse_buffs_dl(); +} +void init_sparse_file(unsigned int block_size, int64_t len) { + assert(init_sparse_file_dl); + return init_sparse_file_dl(block_size, len); +} +void finalize_sparse_file(int fd) { + assert(finalize_sparse_file_dl); + return finalize_sparse_file_dl(fd); +} int dlopenf2fs() { void* f2fs_lib; @@ -58,7 +79,13 @@ int dlopenf2fs() { } f2fs_format_device_dl = dlsym(f2fs_lib, "f2fs_format_device"); f2fs_init_configuration_dl = dlsym(f2fs_lib, "f2fs_init_configuration"); - if (!f2fs_format_device_dl || !f2fs_init_configuration_dl) { + flush_sparse_buffs_dl = dlsym(f2fs_lib, "flush_sparse_buffs"); + init_sparse_file_dl = dlsym(f2fs_lib, "init_sparse_file"); + finalize_sparse_file_dl = dlsym(f2fs_lib, "finalize_sparse_file"); + f2fs_config = dlsym(f2fs_lib, "config"); + if (!f2fs_format_device_dl || !f2fs_init_configuration_dl || + !flush_sparse_buffs_dl || !f2fs_config || + !init_sparse_file_dl || !finalize_sparse_file_dl) { return -1; } return 0; diff --git a/f2fs_utils/f2fs_ioutils.c b/f2fs_utils/f2fs_ioutils.c index a050cf8f..d54f1995 100644 --- a/f2fs_utils/f2fs_ioutils.c +++ b/f2fs_utils/f2fs_ioutils.c @@ -28,9 +28,11 @@ #define _LARGEFILE64_SOURCE -#include #include +#ifdef __linux__ +#include #include +#endif #include #include #include /* memset() */ @@ -135,6 +137,17 @@ static int dev_write_sparse(void *buf, __u64 byte_offset, size_t byte_len) return 0; } +void init_sparse_file(unsigned int block_size, int64_t len) +{ + f2fs_sparse_file = sparse_file_new(block_size, len); +} + +void finalize_sparse_file(int fd) +{ + sparse_file_write(f2fs_sparse_file, fd, /*gzip*/0, /*sparse*/1, /*crc*/0); + sparse_file_destroy(f2fs_sparse_file); +} + void f2fs_finalize_device(struct f2fs_configuration *c) { } diff --git a/f2fs_utils/f2fs_utils.c b/f2fs_utils/f2fs_utils.c index 4c92622b..9833ace5 100644 --- a/f2fs_utils/f2fs_utils.c +++ b/f2fs_utils/f2fs_utils.c @@ -41,20 +41,16 @@ struct selabel_handle; #include "make_f2fs.h" extern void flush_sparse_buffs(); +extern void init_sparse_file(unsigned int block_size, int64_t len); +extern void finalize_sparse_file(int fd); -struct f2fs_configuration config; -struct sparse_file *f2fs_sparse_file; +extern struct f2fs_configuration *f2fs_config; extern int dlopenf2fs(); static void reset_f2fs_info() { - // Reset all the global data structures used by make_f2fs so it - // can be called again. - memset(&config, 0, sizeof(config)); - config.fd = -1; - if (f2fs_sparse_file) { - sparse_file_destroy(f2fs_sparse_file); - f2fs_sparse_file = NULL; - } + memset(f2fs_config, 0, sizeof(*f2fs_config)); + f2fs_config->fd = -1; + f2fs_config->kd = -1; } int make_f2fs_sparse_fd(int fd, long long len, @@ -64,15 +60,13 @@ int make_f2fs_sparse_fd(int fd, long long len, return -1; } reset_f2fs_info(); - f2fs_init_configuration(&config); + f2fs_init_configuration(f2fs_config); len &= ~((__u64)F2FS_BLKSIZE); - config.total_sectors = len / config.sector_size; - config.start_sector = 0; - f2fs_sparse_file = sparse_file_new(F2FS_BLKSIZE, len); + f2fs_config->total_sectors = len / f2fs_config->sector_size; + f2fs_config->start_sector = 0; + init_sparse_file(F2FS_BLKSIZE, len); f2fs_format_device(); - sparse_file_write(f2fs_sparse_file, fd, /*gzip*/0, /*sparse*/1, /*crc*/0); - sparse_file_destroy(f2fs_sparse_file); + finalize_sparse_file(fd); flush_sparse_buffs(); - f2fs_sparse_file = NULL; return 0; } -- 2.11.0