erofs-utils: mkfs: introduce source_mode enumeration
PATCH v7 1/4 erofs-utils: mkfs: introduce source_mode enumeration
From: Yifan Zhao <[email protected]>
Currently, mkfs controls different image build execution flows through the global variables tar_mode and rebuild_mode, while these two modes together with localdir mode are mutually exclusive.
Let's replace them with a new variable source_mode to simplify the logic.
--- mkfs/main.c | 78 ++++++++++++++++++++++++++++++----------------------- 1 file changed, 44 insertions(+), 34 deletions(-)
mkfs/main.c +44 -34
@@ -244,7 +244,7 @@ static int pclustersize_metabox = -1;static struct erofs_tarfile erofstar = { .global.xattrs =LIST_HEAD_INIT (erofstar.global.xattrs) }; -static bool tar_mode, rebuild_mode, incremental_mode; +static bool incremental_mode;static u8 metabox_algorithmid;enum {
@@ -254,6 +254,12 @@ enum {EROFS_MKFS_DATA_IMPORT_SPARSE , } dataimport_mode; +static enum { +EROFS_MKFS_SOURCE_LOCALDIR , +EROFS_MKFS_SOURCE_TAR , +EROFS_MKFS_SOURCE_REBUILD , +} source_mode; +static unsigned int rebuild_src_count, total_ccfgs;static LIST_HEAD (rebuild_src_list);static u8 fixeduuid[16 ];
@@ -499,7 +505,7 @@ static void mkfs_parse_tar_cfg(char *cfg) {char *p; - tar_mode = true; + source_mode =EROFS_MKFS_SOURCE_TAR ;if (!cfg)return ; p =strchr (cfg,',' );
@@ -616,7 +622,30 @@ static int mkfs_parse_sources(int argc, char *argv[], int optind)int err, fd;char *s; -if (tar_mode) { +switch (source_mode) { +case EROFS_MKFS_SOURCE_LOCALDIR : + err =lstat ((s = argv[optind++]), &st); +if (err) { +erofs_err ("failed to stat %s: %s" , s, +erofs_strerror (-errno)); +return -ENOENT ; + } +if (S_ISDIR (st.st_mode)) { + cfg.c_src_path =realpath (s,NULL ); +if (!cfg.c_src_path) { +erofs_err ("failed to parse source directory: %s" , +erofs_strerror (-errno)); +return -ENOENT ; + } +erofs_set_fs_root (cfg.c_src_path); + }else { + cfg.c_src_path =strdup (s); +if (!cfg.c_src_path) +return -ENOMEM ; + source_mode =EROFS_MKFS_SOURCE_REBUILD ; + } +break ; +case EROFS_MKFS_SOURCE_TAR : cfg.c_src_path =strdup (argv[optind++]);if (!cfg.c_src_path)return -ENOMEM ;
@@ -640,30 +669,13 @@ static int mkfs_parse_sources(int argc, char *argv[], int optind) } erofstar.ios.dumpfd = fd; } - }else { - err =lstat ((s = argv[optind++]), &st); -if (err) { -erofs_err ("failed to stat %s: %s" , s, -erofs_strerror (-errno)); -return -ENOENT ; - } -if (S_ISDIR (st.st_mode)) { - cfg.c_src_path =realpath (s,NULL ); -if (!cfg.c_src_path) { -erofs_err ("failed to parse source directory: %s" , -erofs_strerror (-errno)); -return -ENOENT ; - } -erofs_set_fs_root (cfg.c_src_path); - }else { - cfg.c_src_path =strdup (s); -if (!cfg.c_src_path) -return -ENOMEM ; - rebuild_mode = true; - } +break ; +default : +erofs_err ("unexpected source_mode: %d" , source_mode); +return -EINVAL ; } -if (rebuild_mode) { +if (source_mode ==EROFS_MKFS_SOURCE_REBUILD ) {char *srcpath = cfg.c_src_path;struct erofs_sb_info *src;
@@ -1083,7 +1095,7 @@ static int mkfs_parse_options_cfg(int argc, char *argv[]) err =mkfs_parse_sources (argc, argv, optind);if (err)return err; - }else if (!tar_mode) { + }else if (source_mode !=EROFS_MKFS_SOURCE_TAR ) {erofs_err ("missing argument: SOURCE(s)" );return -EINVAL ; }else {
@@ -1383,7 +1395,7 @@ int main(int argc, char **argv)if (cfg.c_random_pclusterblks)srand (time (NULL ));#endif -if (tar_mode) { +if (source_mode ==EROFS_MKFS_SOURCE_TAR ) {if (dataimport_mode ==EROFS_MKFS_DATA_IMPORT_RVSP ) erofstar.rvsp_mode = true; erofstar.dev = rebuild_src_count +1 ;
@@ -1403,9 +1415,7 @@ int main(int argc, char **argv) g_sbi.blkszbits =9 ; tar_index_512b = true; } - } - -if (rebuild_mode) { + }else if (source_mode ==EROFS_MKFS_SOURCE_REBUILD ) {struct erofs_sb_info *src;erofs_warn ("EXPERIMENTAL rebuild mode in use. Use at your own risk!" );
@@ -1465,7 +1475,7 @@ int main(int argc, char **argv)else if (!incremental_mode)erofs_uuid_generate (g_sbi.uuid); -if (tar_mode && !erofstar.index_mode) { +if (source_mode ==EROFS_MKFS_SOURCE_TAR && !erofstar.index_mode) { err =erofs_diskbuf_init (1 );if (err) { erofs_err("failed to initialize diskbuf: %s",
@@ -1528,7 +1538,7 @@ int main(int argc, char **argv)erofs_inode_manager_init (); -if (tar_mode) { +if (source_mode ==EROFS_MKFS_SOURCE_TAR ) { root =erofs_rebuild_make_root (&g_sbi);if (IS_ERR (root)) { err =PTR_ERR (root);
@@ -1543,7 +1553,7 @@ int main(int argc, char **argv) err =erofs_rebuild_dump_tree (root, incremental_mode);if (err <0 ) goto exit; - }else if (rebuild_mode) { + }else if (source_mode ==EROFS_MKFS_SOURCE_REBUILD ) { root =erofs_rebuild_make_root (&g_sbi);if (IS_ERR (root)) { err =PTR_ERR (root);
@@ -1663,7 +1673,7 @@ exit:erofs_rebuild_cleanup ();erofs_diskbuf_exit ();erofs_exit_configure (); -if (tar_mode) { +if (source_mode ==EROFS_MKFS_SOURCE_TAR ) {erofs_iostream_close (&erofstar.ios);if (erofstar.ios.dumpfd >=0 )close (erofstar.ios.dumpfd);
Signature · 1 line
2.43.5
PATCH v7 2/4 erofs-utils: introduce extra build dependencies for S3 support
From: Yifan Zhao <[email protected]>
This patch adds additional dependencies on libcurl, openssl and libxml2 library for the upcoming S3 data source support, with libcurl to interact with S3 API, openssl to generate S3 auth signature and libxml2 to parse response body.
erofs-utils: introduce extra build dependencies for S3 support
This patch adds additional dependencies on libcurl, openssl, and libxml2 for the upcoming S3 data source support:
- libcurl is used to interact with the S3 API;
- openssl is used to generate S3 authentication signatures;
- libxml2 is used to parse response bodies;
These dependencies are optional and controlled using the --with-{libcurl,openssl,libxml2} configure options.
Additionally, a new --enable-s3 option is introduced to enable S3 support. It will report an error if any of the three required libraries doesn't work properly.
--- configure.ac | 129 ++++++++++++++++++++++++++++++++++++++++++++++- lib/Makefile.am | 1 + mkfs/Makefile.am | 3 +- 3 files changed, 131 insertions(+), 2 deletions(-)
configure.ac +128 -1
@@ -24,7 +24,7 @@ esac # OS-specific treatment AM_CONDITIONAL([OS_LINUX], [test "$build_linux" = "yes"]) -AM_INIT_AUTOMAKE([foreign -Wall]) +AM_INIT_AUTOMAKE([foreign subdir-objects -Wall]) # Checks for programs. AM_PROG_AR
@@ -165,6 +165,22 @@ AC_ARG_WITH(xxhash, [AS_HELP_STRING([--with-xxhash], [Enable and build with libxxhash support @<:@default=auto@:>@])]) +AC_ARG_WITH(libcurl, + [AS_HELP_STRING([--with-libcurl], + [Enable and build with libcurl support @<:@default=auto@:>@])]) + +AC_ARG_WITH(openssl, + [AS_HELP_STRING([--with-openssl], + [Enable and build with openssl support @<:@default=auto@:>@])]) + +AC_ARG_WITH(libxml2, + [AS_HELP_STRING([--with-libxml2], + [Enable and build with libxml2 support @<:@default=auto@:>@])]) + +AC_ARG_ENABLE(s3, + [AS_HELP_STRING([--enable-s3], [enable s3 image generation support @<:@default=no@:>@])], + [enable_s3="$enableval"], [enable_s3="no"]) + AC_ARG_ENABLE(fuse, [AS_HELP_STRING([--enable-fuse], [enable erofsfuse @<:@default=no@:>@])], [enable_fuse="$enableval"], [enable_fuse="no"])
@@ -583,6 +599,91 @@ AS_IF([test "x$with_xxhash" != "xno"], [ ]) ]) +# Configure libcurl +have_libcurl="no" +AS_IF([test "x$with_libcurl" != "xno"], [ + PKG_CHECK_MODULES([libcurl], [libcurl], [ + # Paranoia: don't trust the result reported by pkgconfig before trying out + saved_LIBS="$LIBS" + saved_CPPFLAGS=${CPPFLAGS} + CPPFLAGS="${libcurl_CFLAGS} ${CPPFLAGS}" + LIBS="${libcurl_LIBS} $LIBS" + AC_CHECK_HEADERS([curl/curl.h],[ + AC_CHECK_LIB(curl, curl_easy_perform, [], [ + AC_MSG_ERROR([libcurl doesn't work properly])]) + AC_CHECK_DECL(curl_easy_perform, [have_libcurl="yes"], + [AC_MSG_ERROR([libcurl doesn't work properly])], [[ +#include <curl/curl.h> + ]]) + ]) + LIBS="${saved_LIBS}" + CPPFLAGS="${saved_CPPFLAGS}"], [ + AS_IF([test "x$with_libcurl" = "xyes"], [ + AC_MSG_ERROR([Cannot find proper libcurl]) + ]) + ]) +]) + +# Configure openssl +have_openssl="no" +AS_IF([test "x$with_openssl" != "xno"], [ + PKG_CHECK_MODULES([openssl], [openssl], [ + # Paranoia: don't trust the result reported by pkgconfig before trying out + saved_LIBS="$LIBS" + saved_CPPFLAGS=${CPPFLAGS} + CPPFLAGS="${openssl_CFLAGS} ${CPPFLAGS}" + LIBS="${openssl_LIBS} $LIBS" + AC_CHECK_HEADERS([openssl/hmac.h],[ + AC_CHECK_LIB(ssl, EVP_sha1, [], [ + AC_MSG_ERROR([openssl doesn't work properly])]) + AC_CHECK_DECL(EVP_sha1, [have_openssl="yes"], + [AC_MSG_ERROR([openssl doesn't work properly])], [[ +#include <openssl/hmac.h> + ]]) + ]) + LIBS="${saved_LIBS}" + CPPFLAGS="${saved_CPPFLAGS}"], [ + AS_IF([test "x$with_openssl" = "xyes"], [ + AC_MSG_ERROR([Cannot find proper openssl]) + ]) + ]) +]) + +# Configure libxml2 +have_libxml2="no" +AS_IF([test "x$with_libxml2" != "xno"], [ + PKG_CHECK_MODULES([libxml2], [libxml-2.0], [ + # Paranoia: don't trust the result reported by pkgconfig before trying out + saved_LIBS="$LIBS" + saved_CPPFLAGS=${CPPFLAGS} + CPPFLAGS="${libxml2_CFLAGS} ${CPPFLAGS}" + LIBS="${libxml2_LIBS} $LIBS" + AC_CHECK_HEADERS([libxml/parser.h],[ + AC_CHECK_LIB(xml2, xmlReadMemory, [], [ + AC_MSG_ERROR([libxml2 doesn't work properly])]) + AC_CHECK_DECL(xmlReadMemory, [have_libxml2="yes"], + [AC_MSG_ERROR([libxml2 doesn't work properly])], [[ +#include <libxml/parser.h> + ]]) + ]) + LIBS="${saved_LIBS}" + CPPFLAGS="${saved_CPPFLAGS}"], [ + AS_IF([test "x$with_libxml2" = "xyes"], [ + AC_MSG_ERROR([Cannot find proper libxml2]) + ]) + ]) +]) + +AS_IF([test "x$enable_s3" != "xno"], [ + AS_IF( + [test "x$have_libcurl" = "xyes" && \ + test "x$have_openssl" = "xyes" && \ + test "x$have_libxml2" = "xyes"], + [have_s3="yes"], + [have_s3="no" + AC_MSG_ERROR([S3 disabled: missing libcurl, openssl, or libxml2])]) + ], [have_s3="no"]) + # Enable 64-bit off_t CFLAGS+=" -D_LARGEFILE_SOURCE -D_FILE_OFFSET_BITS=64"
@@ -606,6 +707,10 @@ AM_CONDITIONAL([ENABLE_LIBDEFLATE], [test "x${have_libdeflate}" = "xyes"]) AM_CONDITIONAL([ENABLE_LIBZSTD], [test "x${have_libzstd}" = "xyes"]) AM_CONDITIONAL([ENABLE_QPL], [test "x${have_qpl}" = "xyes"]) AM_CONDITIONAL([ENABLE_XXHASH], [test "x${have_xxhash}" = "xyes"]) +AM_CONDITIONAL([ENABLE_LIBCURL], [test "x${have_libcurl}" = "xyes"]) +AM_CONDITIONAL([ENABLE_OPENSSL], [test "x${have_openssl}" = "xyes"]) +AM_CONDITIONAL([ENABLE_LIBXML2], [test "x${have_libxml2}" = "xyes"]) +AM_CONDITIONAL([ENABLE_S3], [test "x${have_s3}" = "xyes"]) AM_CONDITIONAL([ENABLE_STATIC_FUSE], [test "x${enable_static_fuse}" = "xyes"]) if test "x$have_uuid" = "xyes"; then
@@ -657,6 +762,28 @@ if test "x$have_xxhash" = "xyes"; then AC_DEFINE([HAVE_XXHASH], 1, [Define to 1 if xxhash is found]) fi +if test "x$have_libcurl" = "xyes"; then + AC_DEFINE([HAVE_LIBCURL], 1, [Define to 1 if libcurl is found]) + AC_SUBST([libcurl_LIBS]) + AC_SUBST([libcurl_CFLAGS]) +fi + +if test "x$have_openssl" = "xyes"; then + AC_DEFINE([HAVE_OPENSSL], 1, [Define to 1 if openssl is found]) + AC_SUBST([openssl_LIBS]) + AC_SUBST([openssl_CFLAGS]) +fi + +if test "x$have_libxml2" = "xyes"; then + AC_DEFINE([HAVE_LIBXML2], 1, [Define to 1 if libxml2 is found]) + AC_SUBST([libxml2_LIBS]) + AC_SUBST([libxml2_CFLAGS]) +fi + +if test "x$have_s3" = "xyes"; then + AC_DEFINE([S3EROFS_ENABLED], 1, [Define to 1 if s3 is enabled]) +fi + # Dump maximum block size AS_IF([test "x$erofs_cv_max_block_size" = "x"], [$erofs_cv_max_block_size = 4096], [])
lib/Makefile.am +1 -0
@@ -66,6 +66,7 @@ liberofs_la_CFLAGS += ${libxxhash_CFLAGS} else liberofs_la_SOURCES += xxhash.c endif +liberofs_la_CFLAGS += ${libcurl_CFLAGS} ${openssl_CFLAGS} ${libxml2_CFLAGS} if ENABLE_EROFS_MT liberofs_la_LDFLAGS = -lpthread liberofs_la_SOURCES += workqueue.c
mkfs/Makefile.am +2 -1
@@ -7,4 +7,5 @@ mkfs_erofs_SOURCES = main.c mkfs_erofs_CFLAGS = -Wall -I$(top_srcdir)/include mkfs_erofs_LDADD = $(top_builddir)/lib/liberofs.la ${libselinux_LIBS} \ ${libuuid_LIBS} ${liblz4_LIBS} ${liblzma_LIBS} ${zlib_LIBS} \ - ${libdeflate_LIBS} ${libzstd_LIBS} ${libqpl_LIBS} ${libxxhash_LIBS} + ${libdeflate_LIBS} ${libzstd_LIBS} ${libqpl_LIBS} ${libxxhash_LIBS} \ + ${libcurl_LIBS} ${openssl_LIBS} ${libxml2_LIBS}
Signature · 1 line
2.43.5
PATCH v7 3/4 erofs-utils: mkfs: introduce --s3=... option
In reply to Gao XiangFrom: Yifan Zhao <[email protected]>
It introduces configuration options for the upcoming experimental S3 support, including configuration parsing and passwd_file reading logic.
Users can specify the following options:
- S3 service endpoint (required);
- S3 credentials file in the format $ak:%sk (optional);
- S3 API calling style (optional);
- S3 API signature version (optional, only V2 is currently supported).
--- lib/liberofs_s3.h | 40 ++++++++ mkfs/main.c | 226 +++++++++++++++++++++++++++++++++++++++------- 2 files changed, 234 insertions(+), 32 deletions(-) create mode 100644 lib/liberofs_s3.h
lib/liberofs_s3.h new file +40 -0
@@ -0,0 +1,40 @@ +/* SPDX-License-Identifier: GPL-2.0+ OR Apache-2.0 */ +/* +* Copyright (C) 2025 HUAWEI, Inc. +* http://www.huawei.com/ +* Created by Yifan Zhao <[email protected]> +*/ +#ifndef __EROFS_S3_H +#define __EROFS_S3_H + +#ifdef __cplusplus +extern "C" { +#endif + +enum s3erofs_url_style { +S3EROFS_URL_STYLE_PATH ,// Path style: https://s3.amazonaws.com/bucket/object +S3EROFS_URL_STYLE_VIRTUAL_HOST ,// Virtual host style: https://bucket.s3.amazonaws.com/object +}; + +enum s3erofs_signature_version { +S3EROFS_SIGNATURE_VERSION_2 , +S3EROFS_SIGNATURE_VERSION_4 , +}; + +#define S3_ACCESS_KEY_LEN 256 +#define S3_SECRET_KEY_LEN 256 + +struct erofs_s3 { +const char *endpoint; +char access_key[S3_ACCESS_KEY_LEN +1 ]; +char secret_key[S3_SECRET_KEY_LEN +1 ]; + +enum s3erofs_url_style url_style; +enum s3erofs_signature_version sig; +}; + +#ifdef __cplusplus +} +#endif + +#endif
mkfs/main.c +194 -32
301 lines not on this page · raw message
Signature · 1 line
2.43.5
Re: [PATCH v7 3/4] erofs-utils: mkfs: introduce --s3=... option
Hi Xiang,
Quoted 187 lines · Gao Xiang
From: Yifan Zhao <[email protected]>
It introduces configuration options for the upcoming experimental S3 support, including configuration parsing and
passwd_filereading logic.Users can specify the following options:
- S3 service endpoint (required);
- S3 credentials file in the format $ak:%sk (optional);
- S3 API calling style (optional);
- S3 API signature version (optional, only V2 is currently supported).Signed-off-by Yifan Zhao <[email protected]>Signed-off-by Gao Xiang <[email protected]>--- lib/liberofs_s3.h | 40 ++++++++ mkfs/main.c | 226 +++++++++++++++++++++++++++++++++++++++------- 2 files changed, 234 insertions(+), 32 deletions(-)create mode 100644
lib/liberofs_s3.hlib/liberofs_s3.h new file +40
-0@@ -0,0 +1,40 @@ +/* SPDX-License-Identifier: GPL-2.0+ OR Apache-2.0 */ +/* +* Copyright (C) 2025 HUAWEI, Inc. +* http://www.huawei.com/ +* Created by Yifan Zhao <[email protected]> +*/ +#ifndef __EROFS_S3_H +#define __EROFS_S3_H + +#ifdef __cplusplus +extern "C" { +#endif + +enum s3erofs_url_style { +S3EROFS_URL_STYLE_PATH ,// Path style: https://s3.amazonaws.com/bucket/object +S3EROFS_URL_STYLE_VIRTUAL_HOST ,// Virtual host style: https://bucket.s3.amazonaws.com/object +}; + +enum s3erofs_signature_version { +S3EROFS_SIGNATURE_VERSION_2 , +S3EROFS_SIGNATURE_VERSION_4 , +}; + +#define S3_ACCESS_KEY_LEN 256 +#define S3_SECRET_KEY_LEN 256 + +struct erofs_s3 { +const char *endpoint; +char access_key[S3_ACCESS_KEY_LEN +1 ]; +char secret_key[S3_SECRET_KEY_LEN +1 ]; + +enum s3erofs_url_style url_style; +enum s3erofs_signature_version sig; +}; + +#ifdef __cplusplus +} +#endif + +#endif mkfs/main.c +66
-3@@ -31,6 +31,7 @@#include "../lib/liberofs_private.h" #include "../lib/liberofs_uuid.h" #include "../lib/liberofs_metabox.h" +#include "../lib/liberofs_s3.h" #include "../lib/compressor.h" static struct option long_options[] = {@@ -92,6 +93,9 @@ static struct option long_options[] = {#endif {"fsalignblks", required_argument, NULL, 531}, {"vmdk-desc", required_argument, NULL, 532}, +#ifdef S3EROFS_ENABLED + {"s3", required_argument, NULL, 533}, +#endif {0, 0, 0, 0}, };@@ -174,8 +178,8 @@ static void usage(int argc, char **argv)" --chunksize=# generate chunk-based files with #-byte chunks\n" " --clean=X run full clean build (default) or:\n" " --incremental=X run incremental build\n" -" (X = data|rvsp; data=full data, rvsp=space is allocated\n" -" and filled with zeroes)\n" +" X = data|rvsp|0 (data: full data, rvsp: space fallocated\n" +" 0: inodes zeroed)\n" " --compress-hints=X specify a file to configure per-file compression strategy\n" " --dsunit=# align all data block addresses to multiples of #\n" " --exclude-path=X avoid including file X (X = exact literal path)\n" @@ -197,6 +201,12 @@ static void usage(int argc, char **argv) " --root-xattr-isize=# ensure the inline xattr size of the root directory is # bytes at least\n" " --aufs replace aufs special files with overlayfs metadata\n" " --sort=<path,none> data sorting order for tarballs as input (default: path)\n" +#ifdef S3EROFS_ENABLED + " --s3=X generate an image from S3-compatible object store\n" + " [,passwd_file=Y] X=endpoint, Y=s3fs-compatible password file\n" + " [,urlstyle=Z] S3 API calling style (Z = vhost|path) (default: vhost)\n" + " [,sig=<2,4>] S3 API signature version (default: 2)\n" +#endif" --tar=X generate a full or index-only image from a tarball(-ish) source\n" " (X = f|i|headerball; f=full mode, i=index mode,\n" " headerball=file data is omited in the source stream)\n" @@ -247,16 +257,23 @@ static struct erofs_tarfile erofstar = {static bool incremental_mode;static u8 metabox_algorithmid; +#ifdef S3EROFS_ENABLED +static struct erofs_s3 s3cfg; +#endif +enum {EROFS_MKFS_DATA_IMPORT_DEFAULT ,EROFS_MKFS_DATA_IMPORT_FULLDATA ,EROFS_MKFS_DATA_IMPORT_RVSP , -EROFS_MKFS_DATA_IMPORT_SPARSE , +EROFS_MKFS_DATA_IMPORT_ZEROFILL , } dataimport_mode;static enum {EROFS_MKFS_SOURCE_LOCALDIR ,EROFS_MKFS_SOURCE_TAR , +#ifdef S3EROFS_ENABLED +EROFS_MKFS_SOURCE_S3 , +#endif EROFS_MKFS_SOURCE_REBUILD , } source_mode;@@ -522,6 +539,137 @@ static void mkfs_parse_tar_cfg(char *cfg) erofstar.index_mode = true; } +#ifdef S3EROFS_ENABLED +static int mkfs_parse_s3_cfg_passwd (const char *filepath,char *ak,char *sk) +{ +struct stat st; +int fd, n, ret; +char buf[S3_ACCESS_KEY_LEN +S3_SECRET_KEY_LEN +3 ]; +char *colon; + + fd =open (filepath,O_RDONLY ); +if (fd <0 ) { +erofs_err ("failed to open passwd_file %s" , filepath); +return -errno; + } + + ret =fstat (fd, &st); +if (ret) { + ret = -errno; + goto err; + } + +if (!S_ISREG (st.st_mode)) { +erofs_err ("%s is not a regular file" , filepath); + ret = -EINVAL ; + goto err; + } + +if ((st.st_mode &077 ) !=0 ) +erofs_warn ("passwd_file %s should not be accessible by group or others" , + filepath); + +if (st.st_size >S3_ACCESS_KEY_LEN +S3_SECRET_KEY_LEN +3 ) { +erofs_err ("passwd_file %s is too large (size: %llu)" , filepath, + st.st_size |0ULL ); + ret = -EINVAL ; + goto err; + } + + n =read (fd, buf, st.st_size); +if (n <0 ) { + ret = -errno; + goto err; + } + buf[n] ='\0' ; + +while (n >0 && (buf[n -1 ] =='\n' || buf[n -1 ] =='\r' )) + buf[--n] ='\0' ;
mkfs/main.c static void mkfs_parse_tar_cfg(char *cfg) · line 597
Quoted 10 lines
mkfs/main.c +10
-0+ + colon =strchr (buf,':' ); +if (!colon) { + ret = -EINVAL ; + goto err; + } + *colon ='\0' ; + +strcpy (ak, buf); +strcpy (sk, colon +1 );
How about adding memset on buf? And we also need to add cleanup actions for s3cfg to free the endpoint and memset the access_key/secret_key memory.
Therefore, after mkfs.erofs is finished, the ak/sk information will not be recorded in the memory.
Thanks,
Hongbo
Quoted 185 lines
mkfs/main.c +118
-29+ +err: +close (fd); +return ret; +} + +static int mkfs_parse_s3_cfg (char *cfg_str) +{ +char *p, *q, *opt; +int ret =0 ; + +if (source_mode !=EROFS_MKFS_SOURCE_LOCALDIR ) +return -EINVAL ; + source_mode =EROFS_MKFS_SOURCE_S3 ; + +if (!cfg_str) { +erofs_err ("s3: missing parameter" ); +return -EINVAL ; + } + + s3cfg.url_style =S3EROFS_URL_STYLE_VIRTUAL_HOST ; + s3cfg.sig =S3EROFS_SIGNATURE_VERSION_2 ; + + p =strchr (cfg_str,',' ); +if (p) { + s3cfg.endpoint =strndup (cfg_str, p - cfg_str); + }else { + s3cfg.endpoint =strdup (cfg_str); +return 0 ; + } + + opt = p +1 ; +while (opt) { + q =strchr (opt,',' ); +if (q) + *q ='\0' ; + +if ((p =strstr (opt,"passwd_file=" ))) { + p +=sizeof ("passwd_file=" ) -1 ; + ret =mkfs_parse_s3_cfg_passwd (p, s3cfg.access_key, + s3cfg.secret_key); +if (ret) +return ret; + }else if ((p =strstr (opt,"urlstyle=" ))) { + p +=sizeof ("urlstyle=" ) -1 ; +if (strncmp (p,"vhost" ,5 ) ==0 ) { + s3cfg.url_style =S3EROFS_URL_STYLE_VIRTUAL_HOST ; + }else if (strncmp (p,"path" ,4 ) ==0 ) { + s3cfg.url_style =S3EROFS_URL_STYLE_PATH ; + }else { +erofs_err ("invalid S3 addressing style %s" , p); +return -EINVAL ; + } + }else if ((p =strstr (opt,"sig=" ))) { + p +=strlen ("sig=" ); +if (strncmp (p,"4" ,1 ) ==0 ) { +erofs_warn ("AWS Signature Version 4 is not supported yet, using Version 2" ); + }else if (strncmp (p,"2" ,1 ) ==0 ) { + s3cfg.sig =S3EROFS_SIGNATURE_VERSION_2 ; + }else { +erofs_err ("Invalid AWS Signature Version %s" , p); +return -EINVAL ; + } + }else { +erofs_err ("invalid --s3 option %s" , opt); +return -EINVAL ; + } + + opt = q ? q +1 :NULL ; + } + +return 0 ; +} +#endif +static int mkfs_parse_one_compress_alg (char *alg,struct erofs_compr_opts *copts) {@@ -670,6 +818,13 @@ static int mkfs_parse_sources(int argc, char *argv[], int optind) erofstar.ios.dumpfd = fd; }break ; +#ifdef S3EROFS_ENABLED +case EROFS_MKFS_SOURCE_S3 : + cfg.c_src_path =strdup (argv[optind++]); +if (!cfg.c_src_path) +return -ENOMEM ; +break ; +#endif default :erofs_err ("unexpected source_mode: %d" , source_mode);return -EINVAL ;@@ -997,6 +1152,8 @@ static int mkfs_parse_options_cfg(int argc, char *argv[]) dataimport_mode =EROFS_MKFS_DATA_IMPORT_FULLDATA ; }else if (!strcmp (optarg,"rvsp" )) { dataimport_mode =EROFS_MKFS_DATA_IMPORT_RVSP ; + }else if (!strcmp (optarg,"0" )) { + dataimport_mode =EROFS_MKFS_DATA_IMPORT_ZEROFILL ; }else { errno =0 ; dataimport_mode =strtol (optarg, &endptr,0 );@@ -1058,6 +1215,13 @@ static int mkfs_parse_options_cfg(int argc, char *argv[])return -EINVAL ; }break ; +#ifdef S3EROFS_ENABLED +case 533 : + err =mkfs_parse_s3_cfg (optarg); +if (err) +return err; +break ; +#endif case 'V' :version ();exit (0 );@@ -1538,35 +1702,7 @@ int main(int argc, char **argv)erofs_inode_manager_init (); -if (source_mode ==EROFS_MKFS_SOURCE_TAR ) { - root =erofs_rebuild_make_root (&g_sbi); -if (IS_ERR (root)) { - err =PTR_ERR (root); - goto exit; - } - -while (!(err =tarerofs_parse_tar (root, &erofstar))); - -if (err <0 ) - goto exit; - - err =erofs_rebuild_dump_tree (root, incremental_mode); -if (err <0 ) - goto exit; - }else if (source_mode ==EROFS_MKFS_SOURCE_REBUILD ) { - root =erofs_rebuild_make_root (&g_sbi); -if (IS_ERR (root)) { - err =PTR_ERR (root); - goto exit; - } - - err =erofs_mkfs_rebuild_load_trees (root); -if (err) - goto exit; - err =erofs_rebuild_dump_tree (root, incremental_mode); -if (err) - goto exit; - }else { +if (source_mode ==EROFS_MKFS_SOURCE_LOCALDIR ) { err =erofs_build_shared_xattrs_from_path (&g_sbi, cfg.c_src_path);if (err) { erofs_err("failed to build shared xattrs: %s",@@ -1583,6 +1719,32 @@ int main(int argc, char **argv) root =NULL ; goto exit; } + }else { + root =erofs_rebuild_make_root (&g_sbi); +if (IS_ERR (root)) { + err =PTR_ERR (root); + goto exit; + } + +if (source_mode ==EROFS_MKFS_SOURCE_TAR ) { +while (!(err =tarerofs_parse_tar (root, &erofstar))) + ; +if (err <0 ) + goto exit; + }else if (source_mode ==EROFS_MKFS_SOURCE_REBUILD ) { + err =erofs_mkfs_rebuild_load_trees (root); +if (err) + goto exit; +#ifdef S3EROFS_ENABLED + }else if (source_mode ==EROFS_MKFS_SOURCE_S3 ) { + err = -EOPNOTSUPP ; + goto exit; +#endif + } + + err =erofs_rebuild_dump_tree (root, incremental_mode); +if (err) + goto exit; }if (tar_index_512b) {
Re: [PATCH v7 3/4] erofs-utils: mkfs: introduce --s3=... option
Quoted 207 lines · Hongbo Li
Hi Xiang,
Quoted 191 lines · Gao Xiang
From: Yifan Zhao <[email protected]>
It introduces configuration options for the upcoming experimental S3 support, including configuration parsing and
passwd_filereading logic.Users can specify the following options:
- S3 service endpoint (required);
- S3 credentials file in the format $ak:%sk (optional);
- S3 API calling style (optional);
- S3 API signature version (optional, only V2 is currently supported).Signed-off-by Yifan Zhao <[email protected]>Signed-off-by Gao Xiang <[email protected]>---
lib/liberofs_s3.h| 40 ++++++++
mkfs/main.c | 226 +++++++++++++++++++++++++++++++++++++++-------2 files changed, 234 insertions(+), 32 deletions(-)create mode 100644
lib/liberofs_s3.hlib/liberofs_s3.h new file +15
-0@@ -0,0 +1,40 @@ +/* SPDX-License-Identifier: GPL-2.0+ OR Apache-2.0 */ +/* +* Copyright (C) 2025 HUAWEI, Inc. +* http://www.huawei.com/ +* Created by Yifan Zhao <[email protected]> +*/ +#ifndef __EROFS_S3_H +#define __EROFS_S3_H + +#ifdef __cplusplus +extern "C" { +#endif + +enum s3erofs_url_style { + S3EROFS_URL_STYLE_PATH, // Path style:https://s3.amazonaws.com/bucket/object
+ S3EROFS_URL_STYLE_VIRTUAL_HOST, // Virtual host style:
https://bucket.s3.amazonaws.com/objectlib/liberofs_s3.h new file +24
-0+}; + +enum s3erofs_signature_version { + S3EROFS_SIGNATURE_VERSION_2, + S3EROFS_SIGNATURE_VERSION_4, +}; + +#define S3_ACCESS_KEY_LEN 256 +#define S3_SECRET_KEY_LEN 256 + +struct erofs_s3 { + const char *endpoint; + char access_key[S3_ACCESS_KEY_LEN + 1]; + char secret_key[S3_SECRET_KEY_LEN + 1]; + + enum s3erofs_url_style url_style; + enum s3erofs_signature_version sig; +}; + +#ifdef __cplusplus +} +#endif + +#endif mkfs/main.c +4
-0@@ -31,6 +31,7 @@ #include "../lib/liberofs_private.h" #include "../lib/liberofs_uuid.h" #include "../lib/liberofs_metabox.h" +#include "../lib/liberofs_s3.h" #include "../lib/compressor.h" static struct option long_options[] = {@@ -92,6 +93,9 @@ static struct option long_options[] = { #endif {"fsalignblks", required_argument, NULL, 531}, {"vmdk-desc", required_argument, NULL, 532}, +#ifdef S3EROFS_ENABLED + {"s3", required_argument, NULL, 533}, +#endif {0, 0, 0, 0}, };@@ -174,8 +178,8 @@ static void usage(int argc, char **argv)" --chunksize=# generate chunk-based files with #-byte chunks\n" " --clean=X run full clean build (default) or:\n"
" --incremental=X run incremental build\n"
- " (X = data|rvsp; data=full data,
rvsp=space is allocated\n"
- " and filled with
zeroes)\n"
+ " X = data|rvsp|0 (data: full data,
rvsp: space fallocated\n"
+ " 0: inodes zeroed)\n"
" --compress-hints=X specify a file to configure per-file compression strategy\n" " --dsunit=# align all data block addresses to multiples of #\n" " --exclude-path=X avoid including file X (X = exact literal path)\n"mkfs/main.c +0
-0@@ -197,6 +201,12 @@ static void usage(int argc, char **argv)" --root-xattr-isize=# ensure the inline xattr size of the root directory is # bytes at least\n" " --aufs replace aufs special files with overlayfs metadata\n"
" --sort=<path,none> data sorting order for tarballs as input (default: path)\n" +#ifdefS3EROFS_ENABLED
+ " --s3=X generate an image from S3-compatible
object store\n"
+ " [,passwd_file=Y] X=endpoint, Y=s3fs-compatible
password file\n"
+ " [,urlstyle=Z] S3 API calling style (Z = vhost|path)
(default: vhost)\n"
+ " [,sig=<2,4>] S3 API signature version (default: 2)\n"
+#endif
" --tar=X generate a full or index-only image from a tarball(-ish) source\n" " (X = f|i|headerball; f=full mode, i=index mode,\n"
" headerball=file data is omited in the source stream)\n"mkfs/main.c +54
-1@@ -247,16 +257,23 @@ static struct erofs_tarfile erofstar = { static bool incremental_mode; static u8 metabox_algorithmid; +#ifdef S3EROFS_ENABLED +static struct erofs_s3 s3cfg; +#endif + enum { EROFS_MKFS_DATA_IMPORT_DEFAULT, EROFS_MKFS_DATA_IMPORT_FULLDATA, EROFS_MKFS_DATA_IMPORT_RVSP, - EROFS_MKFS_DATA_IMPORT_SPARSE, + EROFS_MKFS_DATA_IMPORT_ZEROFILL, } dataimport_mode; static enum { EROFS_MKFS_SOURCE_LOCALDIR, EROFS_MKFS_SOURCE_TAR, +#ifdef S3EROFS_ENABLED + EROFS_MKFS_SOURCE_S3, +#endif EROFS_MKFS_SOURCE_REBUILD, } source_mode;@@ -522,6 +539,137 @@ static void mkfs_parse_tar_cfg(char *cfg) erofstar.index_mode = true; } +#ifdef S3EROFS_ENABLED +static int mkfs_parse_s3_cfg_passwd (const char *filepath,char *ak,char *sk) +{ + struct stat st; + int fd, n, ret; + char buf[S3_ACCESS_KEY_LEN + S3_SECRET_KEY_LEN + 3]; + char *colon; + + fd = open(filepath, O_RDONLY); + if (fd < 0) { + erofs_err("failed to open passwd_file %s", filepath); + return -errno; + } + + ret = fstat(fd, &st); + if (ret) { + ret = -errno; + goto err; + } + + if (!S_ISREG(st.st_mode)) { + erofs_err("%s is not a regular file", filepath); + ret = -EINVAL; + goto err; + } + + if ((st.st_mode & 077) != 0) + erofs_warn("passwd_file %s should not be accessible by group or others", + filepath); + + if (st.st_size > S3_ACCESS_KEY_LEN + S3_SECRET_KEY_LEN + 3) { + erofs_err("passwd_file %s is too large (size: %llu)", filepath, + st.st_size | 0ULL); + ret = -EINVAL; + goto err; + } + + n = read(fd, buf, st.st_size); + if (n < 0) { + ret = -errno; + goto err; + } + buf[n] = '\0'; + + while (n > 0 && (buf[n - 1] == '\n' || buf[n - 1] == '\r')) + buf[--n] = '\0';mkfs/main.c static void mkfs_parse_tar_cfg(char *cfg) · line 598
Quoted 10 lines
mkfs/main.c +10
-0+ + colon = strchr(buf, ':'); + if (!colon) { + ret = -EINVAL; + goto err; + } + *colon = '\0'; + + strcpy(ak, buf); + strcpy(sk, colon + 1);How about adding memset on buf? And we also need to add cleanup actions for s3cfg to free the endpoint and memset the access_key/secret_key memory.
Quoted 2 lines
Therefore, after mkfs.erofs is finished, the ak/sk information will not be recorded in the memory.
oh, buf is stack variable, memset is unuse. ;)
Quoted 189 lines
Thanks,
HongboQuoted 186 lines
mkfs/main.c +75
-0+ +err: + close(fd); + return ret; +} + +static int mkfs_parse_s3_cfg (char *cfg_str) +{ + char *p, *q, *opt; + int ret = 0; + + if (source_mode != EROFS_MKFS_SOURCE_LOCALDIR) + return -EINVAL; + source_mode = EROFS_MKFS_SOURCE_S3; + + if (!cfg_str) { + erofs_err("s3: missing parameter"); + return -EINVAL; + } + + s3cfg.url_style = S3EROFS_URL_STYLE_VIRTUAL_HOST; + s3cfg.sig = S3EROFS_SIGNATURE_VERSION_2; + + p = strchr(cfg_str, ','); + if (p) { + s3cfg.endpoint = strndup(cfg_str, p - cfg_str); + } else { + s3cfg.endpoint = strdup(cfg_str); + return 0; + } + + opt = p + 1; + while (opt) { + q = strchr(opt, ','); + if (q) + *q = '\0'; + + if ((p = strstr(opt, "passwd_file="))) { + p += sizeof("passwd_file=") - 1; + ret = mkfs_parse_s3_cfg_passwd(p, s3cfg.access_key, + s3cfg.secret_key); + if (ret) + return ret; + } else if ((p = strstr(opt, "urlstyle="))) { + p += sizeof("urlstyle=") - 1; + if (strncmp(p, "vhost", 5) == 0) { + s3cfg.url_style = S3EROFS_URL_STYLE_VIRTUAL_HOST; + } else if (strncmp(p, "path", 4) == 0) { + s3cfg.url_style = S3EROFS_URL_STYLE_PATH; + } else { + erofs_err("invalid S3 addressing style %s", p); + return -EINVAL; + } + } else if ((p = strstr(opt, "sig="))) { + p += strlen("sig="); + if (strncmp(p, "4", 1) == 0) { + erofs_warn("AWS Signature Version 4 is not supported yet, using Version 2"); + } else if (strncmp(p, "2", 1) == 0) { + s3cfg.sig = S3EROFS_SIGNATURE_VERSION_2; + } else { + erofs_err("Invalid AWS Signature Version %s", p); + return -EINVAL; + } + } else { + erofs_err("invalid --s3 option %s", opt); + return -EINVAL; + } + + opt = q ? q + 1 : NULL; + } + + return 0; +} +#endif + static int mkfs_parse_one_compress_alg(char *alg,struct
erofs_compr_opts*copts) {mkfs/main.c +7
-0@@ -670,6 +818,13 @@ static int mkfs_parse_sources(int argc, char *argv[], int optind) erofstar.ios.dumpfd = fd; } break; +#ifdef S3EROFS_ENABLED + case EROFS_MKFS_SOURCE_S3: + cfg.c_src_path = strdup(argv[optind++]); + if (!cfg.c_src_path) + return -ENOMEM; + break; +#endif default:
erofs_err("unexpected source_mode: %d", source_mode); return-EINVAL;mkfs/main.c +2
-0@@ -997,6 +1152,8 @@ static int mkfs_parse_options_cfg(int argc, char *argv[]) dataimport_mode = EROFS_MKFS_DATA_IMPORT_FULLDATA; } else if (!strcmp(optarg, "rvsp")) { dataimport_mode = EROFS_MKFS_DATA_IMPORT_RVSP; + } else if (!strcmp(optarg, "0")) { + dataimport_mode = EROFS_MKFS_DATA_IMPORT_ZEROFILL; } else { errno = 0;
dataimport_mode=strtol(optarg, &endptr, 0);mkfs/main.c +7
-0@@ -1058,6 +1215,13 @@ static int mkfs_parse_options_cfg(int argc, char *argv[]) return -EINVAL; } break; +#ifdef S3EROFS_ENABLED + case 533: + err = mkfs_parse_s3_cfg(optarg); + if (err) + return err; + break; +#endif case 'V':
version();
exit(0);mkfs/main.c +27
-29@@ -1538,35 +1702,7 @@ int main(int argc, char **argv) erofs_inode_manager_init(); - if (source_mode == EROFS_MKFS_SOURCE_TAR) { - root = erofs_rebuild_make_root(&g_sbi); - if (IS_ERR(root)) { - err = PTR_ERR(root); - goto exit; - } - - while (!(err = tarerofs_parse_tar(root, &erofstar))); - - if (err < 0) - goto exit; - - err = erofs_rebuild_dump_tree(root, incremental_mode); - if (err < 0) - goto exit; - } else if (source_mode == EROFS_MKFS_SOURCE_REBUILD) { - root = erofs_rebuild_make_root(&g_sbi); - if (IS_ERR(root)) { - err = PTR_ERR(root); - goto exit; - } - - err = erofs_mkfs_rebuild_load_trees(root); - if (err) - goto exit; - err = erofs_rebuild_dump_tree(root, incremental_mode); - if (err) - goto exit; - } else { + if (source_mode == EROFS_MKFS_SOURCE_LOCALDIR) { err = erofs_build_shared_xattrs_from_path(&g_sbi, cfg.c_src_path); if (err) { erofs_err("failed to build shared xattrs: %s",@@ -1583,6 +1719,32 @@ int main(int argc, char **argv) root = NULL; goto exit; } + } else { + root = erofs_rebuild_make_root(&g_sbi); + if (IS_ERR(root)) { + err = PTR_ERR(root); + goto exit; + } + + if (source_mode == EROFS_MKFS_SOURCE_TAR) { + while (!(err = tarerofs_parse_tar(root, &erofstar))) + ; + if (err < 0) + goto exit; + } else if (source_mode == EROFS_MKFS_SOURCE_REBUILD) { + err = erofs_mkfs_rebuild_load_trees(root); + if (err) + goto exit; +#ifdef S3EROFS_ENABLED + } else if (source_mode == EROFS_MKFS_SOURCE_S3) { + err = -EOPNOTSUPP; + goto exit; +#endif + } + + err = erofs_rebuild_dump_tree(root, incremental_mode); + if (err) + goto exit; } if (tar_index_512b) {
Re: [PATCH v7 3/4] erofs-utils: mkfs: introduce --s3=... option
Hi Hongbo,
Quoted 2 lines · Hongbo Li
...
Quoted 7 lines
Quoted 5 lines
How about adding memset on buf? And we also need to add cleanup actions for s3cfg to free the endpoint and memset the access_key/secret_key memory.
Therefore, after mkfs.erofs is finished, the ak/sk information will not be recorded in the memory.
oh, buf is stack variable, memset is unuse. ;)
I will merge this series to -dev branch today, feel free to submit any following improvement for this work.
Thanks,
Gao Xiang
Re: [PATCH v7 3/4] erofs-utils: mkfs: introduce --s3=... option
In reply to Gao XiangQuoted 6 lines · Gao Xiang
From: Yifan Zhao <[email protected]>
It introduces configuration options for the upcoming experimental S3 support, including configuration parsing and
passwd_filereading logic.
Quoted 8 lines
Users can specify the following options:
- S3 service endpoint (required);
- S3 credentials file in the format $ak:%sk (optional);
- S3 API calling style (optional);
- S3 API signature version (optional, only V2 is currently supported).Signed-off-by Yifan Zhao <[email protected]>Signed-off-by Gao Xiang <[email protected]>
Thanks,
Hongbo
Quoted 368 lines
--- lib/liberofs_s3.h | 40 ++++++++ mkfs/main.c | 226 +++++++++++++++++++++++++++++++++++++++------- 2 files changed, 234 insertions(+), 32 deletions(-)create mode 100644
lib/liberofs_s3.hlib/liberofs_s3.h new file +40
-0@@ -0,0 +1,40 @@ +/* SPDX-License-Identifier: GPL-2.0+ OR Apache-2.0 */ +/* +* Copyright (C) 2025 HUAWEI, Inc. +* http://www.huawei.com/ +* Created by Yifan Zhao <[email protected]> +*/ +#ifndef __EROFS_S3_H +#define __EROFS_S3_H + +#ifdef __cplusplus +extern "C" { +#endif + +enum s3erofs_url_style { +S3EROFS_URL_STYLE_PATH ,// Path style: https://s3.amazonaws.com/bucket/object +S3EROFS_URL_STYLE_VIRTUAL_HOST ,// Virtual host style: https://bucket.s3.amazonaws.com/object +}; + +enum s3erofs_signature_version { +S3EROFS_SIGNATURE_VERSION_2 , +S3EROFS_SIGNATURE_VERSION_4 , +}; + +#define S3_ACCESS_KEY_LEN 256 +#define S3_SECRET_KEY_LEN 256 + +struct erofs_s3 { +const char *endpoint; +char access_key[S3_ACCESS_KEY_LEN +1 ]; +char secret_key[S3_SECRET_KEY_LEN +1 ]; + +enum s3erofs_url_style url_style; +enum s3erofs_signature_version sig; +}; + +#ifdef __cplusplus +} +#endif + +#endif mkfs/main.c +194
-32@@ -31,6 +31,7 @@#include "../lib/liberofs_private.h" #include "../lib/liberofs_uuid.h" #include "../lib/liberofs_metabox.h" +#include "../lib/liberofs_s3.h" #include "../lib/compressor.h" static struct option long_options[] = {@@ -92,6 +93,9 @@ static struct option long_options[] = {#endif {"fsalignblks", required_argument, NULL, 531}, {"vmdk-desc", required_argument, NULL, 532}, +#ifdef S3EROFS_ENABLED + {"s3", required_argument, NULL, 533}, +#endif {0, 0, 0, 0}, };@@ -174,8 +178,8 @@ static void usage(int argc, char **argv)" --chunksize=# generate chunk-based files with #-byte chunks\n" " --clean=X run full clean build (default) or:\n" " --incremental=X run incremental build\n" -" (X = data|rvsp; data=full data, rvsp=space is allocated\n" -" and filled with zeroes)\n" +" X = data|rvsp|0 (data: full data, rvsp: space fallocated\n" +" 0: inodes zeroed)\n" " --compress-hints=X specify a file to configure per-file compression strategy\n" " --dsunit=# align all data block addresses to multiples of #\n" " --exclude-path=X avoid including file X (X = exact literal path)\n" @@ -197,6 +201,12 @@ static void usage(int argc, char **argv) " --root-xattr-isize=# ensure the inline xattr size of the root directory is # bytes at least\n" " --aufs replace aufs special files with overlayfs metadata\n" " --sort=<path,none> data sorting order for tarballs as input (default: path)\n" +#ifdef S3EROFS_ENABLED + " --s3=X generate an image from S3-compatible object store\n" + " [,passwd_file=Y] X=endpoint, Y=s3fs-compatible password file\n" + " [,urlstyle=Z] S3 API calling style (Z = vhost|path) (default: vhost)\n" + " [,sig=<2,4>] S3 API signature version (default: 2)\n" +#endif" --tar=X generate a full or index-only image from a tarball(-ish) source\n" " (X = f|i|headerball; f=full mode, i=index mode,\n" " headerball=file data is omited in the source stream)\n" @@ -247,16 +257,23 @@ static struct erofs_tarfile erofstar = {static bool incremental_mode;static u8 metabox_algorithmid; +#ifdef S3EROFS_ENABLED +static struct erofs_s3 s3cfg; +#endif +enum {EROFS_MKFS_DATA_IMPORT_DEFAULT ,EROFS_MKFS_DATA_IMPORT_FULLDATA ,EROFS_MKFS_DATA_IMPORT_RVSP , -EROFS_MKFS_DATA_IMPORT_SPARSE , +EROFS_MKFS_DATA_IMPORT_ZEROFILL , } dataimport_mode;static enum {EROFS_MKFS_SOURCE_LOCALDIR ,EROFS_MKFS_SOURCE_TAR , +#ifdef S3EROFS_ENABLED +EROFS_MKFS_SOURCE_S3 , +#endif EROFS_MKFS_SOURCE_REBUILD , } source_mode;@@ -522,6 +539,137 @@ static void mkfs_parse_tar_cfg(char *cfg) erofstar.index_mode = true; } +#ifdef S3EROFS_ENABLED +static int mkfs_parse_s3_cfg_passwd (const char *filepath,char *ak,char *sk) +{ +struct stat st; +int fd, n, ret; +char buf[S3_ACCESS_KEY_LEN +S3_SECRET_KEY_LEN +3 ]; +char *colon; + + fd =open (filepath,O_RDONLY ); +if (fd <0 ) { +erofs_err ("failed to open passwd_file %s" , filepath); +return -errno; + } + + ret =fstat (fd, &st); +if (ret) { + ret = -errno; + goto err; + } + +if (!S_ISREG (st.st_mode)) { +erofs_err ("%s is not a regular file" , filepath); + ret = -EINVAL ; + goto err; + } + +if ((st.st_mode &077 ) !=0 ) +erofs_warn ("passwd_file %s should not be accessible by group or others" , + filepath); + +if (st.st_size >S3_ACCESS_KEY_LEN +S3_SECRET_KEY_LEN +3 ) { +erofs_err ("passwd_file %s is too large (size: %llu)" , filepath, + st.st_size |0ULL ); + ret = -EINVAL ; + goto err; + } + + n =read (fd, buf, st.st_size); +if (n <0 ) { + ret = -errno; + goto err; + } + buf[n] ='\0' ; + +while (n >0 && (buf[n -1 ] =='\n' || buf[n -1 ] =='\r' )) + buf[--n] ='\0' ; + + colon =strchr (buf,':' ); +if (!colon) { + ret = -EINVAL ; + goto err; + } + *colon ='\0' ; + +strcpy (ak, buf); +strcpy (sk, colon +1 ); + +err: +close (fd); +return ret; +} + +static int mkfs_parse_s3_cfg (char *cfg_str) +{ +char *p, *q, *opt; +int ret =0 ; + +if (source_mode !=EROFS_MKFS_SOURCE_LOCALDIR ) +return -EINVAL ; + source_mode =EROFS_MKFS_SOURCE_S3 ; + +if (!cfg_str) { +erofs_err ("s3: missing parameter" ); +return -EINVAL ; + } + + s3cfg.url_style =S3EROFS_URL_STYLE_VIRTUAL_HOST ; + s3cfg.sig =S3EROFS_SIGNATURE_VERSION_2 ; + + p =strchr (cfg_str,',' ); +if (p) { + s3cfg.endpoint =strndup (cfg_str, p - cfg_str); + }else { + s3cfg.endpoint =strdup (cfg_str); +return 0 ; + } + + opt = p +1 ; +while (opt) { + q =strchr (opt,',' ); +if (q) + *q ='\0' ; + +if ((p =strstr (opt,"passwd_file=" ))) { + p +=sizeof ("passwd_file=" ) -1 ; + ret =mkfs_parse_s3_cfg_passwd (p, s3cfg.access_key, + s3cfg.secret_key); +if (ret) +return ret; + }else if ((p =strstr (opt,"urlstyle=" ))) { + p +=sizeof ("urlstyle=" ) -1 ; +if (strncmp (p,"vhost" ,5 ) ==0 ) { + s3cfg.url_style =S3EROFS_URL_STYLE_VIRTUAL_HOST ; + }else if (strncmp (p,"path" ,4 ) ==0 ) { + s3cfg.url_style =S3EROFS_URL_STYLE_PATH ; + }else { +erofs_err ("invalid S3 addressing style %s" , p); +return -EINVAL ; + } + }else if ((p =strstr (opt,"sig=" ))) { + p +=strlen ("sig=" ); +if (strncmp (p,"4" ,1 ) ==0 ) { +erofs_warn ("AWS Signature Version 4 is not supported yet, using Version 2" ); + }else if (strncmp (p,"2" ,1 ) ==0 ) { + s3cfg.sig =S3EROFS_SIGNATURE_VERSION_2 ; + }else { +erofs_err ("Invalid AWS Signature Version %s" , p); +return -EINVAL ; + } + }else { +erofs_err ("invalid --s3 option %s" , opt); +return -EINVAL ; + } + + opt = q ? q +1 :NULL ; + } + +return 0 ; +} +#endif +static int mkfs_parse_one_compress_alg (char *alg,struct erofs_compr_opts *copts) {@@ -670,6 +818,13 @@ static int mkfs_parse_sources(int argc, char *argv[], int optind) erofstar.ios.dumpfd = fd; }break ; +#ifdef S3EROFS_ENABLED +case EROFS_MKFS_SOURCE_S3 : + cfg.c_src_path =strdup (argv[optind++]); +if (!cfg.c_src_path) +return -ENOMEM ; +break ; +#endif default :erofs_err ("unexpected source_mode: %d" , source_mode);return -EINVAL ;@@ -997,6 +1152,8 @@ static int mkfs_parse_options_cfg(int argc, char *argv[]) dataimport_mode =EROFS_MKFS_DATA_IMPORT_FULLDATA ; }else if (!strcmp (optarg,"rvsp" )) { dataimport_mode =EROFS_MKFS_DATA_IMPORT_RVSP ; + }else if (!strcmp (optarg,"0" )) { + dataimport_mode =EROFS_MKFS_DATA_IMPORT_ZEROFILL ; }else { errno =0 ; dataimport_mode =strtol (optarg, &endptr,0 );@@ -1058,6 +1215,13 @@ static int mkfs_parse_options_cfg(int argc, char *argv[])return -EINVAL ; }break ; +#ifdef S3EROFS_ENABLED +case 533 : + err =mkfs_parse_s3_cfg (optarg); +if (err) +return err; +break ; +#endif case 'V' :version ();exit (0 );@@ -1538,35 +1702,7 @@ int main(int argc, char **argv)erofs_inode_manager_init (); -if (source_mode ==EROFS_MKFS_SOURCE_TAR ) { - root =erofs_rebuild_make_root (&g_sbi); -if (IS_ERR (root)) { - err =PTR_ERR (root); - goto exit; - } - -while (!(err =tarerofs_parse_tar (root, &erofstar))); - -if (err <0 ) - goto exit; - - err =erofs_rebuild_dump_tree (root, incremental_mode); -if (err <0 ) - goto exit; - }else if (source_mode ==EROFS_MKFS_SOURCE_REBUILD ) { - root =erofs_rebuild_make_root (&g_sbi); -if (IS_ERR (root)) { - err =PTR_ERR (root); - goto exit; - } - - err =erofs_mkfs_rebuild_load_trees (root); -if (err) - goto exit; - err =erofs_rebuild_dump_tree (root, incremental_mode); -if (err) - goto exit; - }else { +if (source_mode ==EROFS_MKFS_SOURCE_LOCALDIR ) { err =erofs_build_shared_xattrs_from_path (&g_sbi, cfg.c_src_path);if (err) { erofs_err("failed to build shared xattrs: %s",@@ -1583,6 +1719,32 @@ int main(int argc, char **argv) root =NULL ; goto exit; } + }else { + root =erofs_rebuild_make_root (&g_sbi); +if (IS_ERR (root)) { + err =PTR_ERR (root); + goto exit; + } + +if (source_mode ==EROFS_MKFS_SOURCE_TAR ) { +while (!(err =tarerofs_parse_tar (root, &erofstar))) + ; +if (err <0 ) + goto exit; + }else if (source_mode ==EROFS_MKFS_SOURCE_REBUILD ) { + err =erofs_mkfs_rebuild_load_trees (root); +if (err) + goto exit; +#ifdef S3EROFS_ENABLED + }else if (source_mode ==EROFS_MKFS_SOURCE_S3 ) { + err = -EOPNOTSUPP ; + goto exit; +#endif + } + + err =erofs_rebuild_dump_tree (root, incremental_mode); +if (err) + goto exit; }if (tar_index_512b) {
PATCH v7 4/4 erofs-utils: mkfs: support EROFS meta-only image generation from S3
In reply to Gao XiangFrom: Yifan Zhao <[email protected]>
This patch introduces experimental S3 support for mkfs.erofs, allowing EROFS images to be generated from AWS S3 and/or other S3 API-compatible services.
Here are the current limitations:
- Only meta-only EROFS image generation is supported;
- Only AWS Signature Version 2 is supported;
- The S3 object names and sizes are strictly respected during image generation.
---
v7:
- Fix NextMarker omission on the standard AWS S3;
- Add prefix support for S3 ListObjects.
lib/Makefile.am | 3 + lib/liberofs_s3.h | 3 + lib/remotes/s3.c | 670 ++++++++++++++++++++++++++++++++++++++++++++++ mkfs/main.c | 10 +- 4 files changed, 684 insertions(+), 2 deletions(-) create mode 100644 lib/remotes/s3.c
lib/Makefile.am +3 -0
@@ -67,6 +67,9 @@ else liberofs_la_SOURCES += xxhash.c endif liberofs_la_CFLAGS += ${libcurl_CFLAGS} ${openssl_CFLAGS} ${libxml2_CFLAGS} +if ENABLE_S3 +liberofs_la_SOURCES += remotes/s3.c +endif if ENABLE_EROFS_MT liberofs_la_LDFLAGS = -lpthread liberofs_la_SOURCES += workqueue.c
lib/liberofs_s3.h +3 -0
@@ -33,6 +33,9 @@ struct erofs_s3 {enum s3erofs_signature_version sig; }; +int s3erofs_build_trees (struct erofs_inode *root,struct erofs_s3 *s3, +const char *path); + #ifdef __cplusplus }#endif
lib/remotes/s3.c new file +670 -0
670 lines not on this page · raw message
mkfs/main.c +8 -2
@@ -1737,8 +1737,14 @@ int main(int argc, char **argv) goto exit; #ifdef S3EROFS_ENABLED }else if (source_mode ==EROFS_MKFS_SOURCE_S3 ) { - err = -EOPNOTSUPP ; - goto exit; +if (incremental_mode || + dataimport_mode !=EROFS_MKFS_DATA_IMPORT_ZEROFILL ) + err = -EOPNOTSUPP ; +else + err =s3erofs_build_trees (root, &s3cfg, + cfg.c_src_path); +if (err) + goto exit;#endif }
Signature · 1 line
2.43.5
Re: [PATCH v7 4/4] erofs-utils: mkfs: support EROFS meta-only image generation from S3
Tested:
- special filename: 'test%^&hidden.txt' 'test space.txt' '测试文件.txt'
- manyfiles (>1000) directory
- path-style obs backend
On 2025/8/8 11:15, Gao Xiang Wrote:
Quoted 753 lines
From: Yifan Zhao <[email protected]>
This patch introduces experimental S3 support for mkfs.erofs, allowing EROFS images to be generated from AWS S3 and/or other S3 API-compatible services.
Here are the current limitations:
- Only meta-only EROFS image generation is supported;
- Only AWS Signature Version 2 is supported;
- The S3 object names and sizes are strictly respected during image generation.Co-developed-by Hongbo Li <[email protected]>Signed-off-by Hongbo Li <[email protected]>Signed-off-by Yifan Zhao <[email protected]>Signed-off-by Gao Xiang <[email protected]>---
v7:
- FixNextMarkeromission on the standard AWS S3;
- Addprefixsupport for S3 ListObjects.lib/Makefile.am | 3 + lib/liberofs_s3.h | 3 + lib/remotes/s3.c | 670 ++++++++++++++++++++++++++++++++++++++++++++++ mkfs/main.c | 10 +- 4 files changed, 684 insertions(+), 2 deletions(-)create mode 100644
lib/remotes/s3.clib/Makefile.am +3
-0@@ -67,6 +67,9 @@ else liberofs_la_SOURCES += xxhash.c endif liberofs_la_CFLAGS += ${libcurl_CFLAGS} ${openssl_CFLAGS} ${libxml2_CFLAGS} +if ENABLE_S3 +liberofs_la_SOURCES += remotes/s3.c +endif if ENABLE_EROFS_MT liberofs_la_LDFLAGS = -lpthread liberofs_la_SOURCES += workqueue.clib/liberofs_s3.h +3
-0@@ -33,6 +33,9 @@ struct erofs_s3 {enum s3erofs_signature_version sig; }; +int s3erofs_build_trees (struct erofs_inode *root,struct erofs_s3 *s3, +const char *path); + #ifdef __cplusplus }#endif lib/remotes/s3.c new file +670
-0@@ -0,0 +1,670 @@ +// SPDX-License-Identifier: GPL-2.0+ OR Apache-2.0 +/* +* Copyright (C) 2025 HUAWEI, Inc. +* http://www.huawei.com/ +* Created by Yifan Zhao <[email protected]> +*/ +#define _GNU_SOURCE +#include <stdio.h> +#include <stdlib.h> +#include <time.h> +#include <curl/curl.h> +#include <libxml/parser.h> +#include <openssl/hmac.h> +#include "erofs/internal.h" +#include "erofs/print.h" +#include "erofs/inode.h" +#include "erofs/blobchunk.h" +#include "erofs/rebuild.h" +#include "liberofs_s3.h" + +#define S3EROFS_PATH_MAX 1024 +#define S3EROFS_MAX_QUERY_PARAMS 16 +#define S3EROFS_URL_LEN 8192 +#define S3EROFS_CANONICAL_QUERY_LEN 2048 + +#define BASE64_ENCODE_LEN (len) (((len + 2) / 3) * 4) + +static CURL *easy_curl; + +struct s3erofs_query_params { +int num; +const char *key[S3EROFS_MAX_QUERY_PARAMS ]; +const char *value[S3EROFS_MAX_QUERY_PARAMS ]; +}; + +struct s3erofs_curl_request { +const char *method; +char url[S3EROFS_URL_LEN ]; +char canonical_query[S3EROFS_CANONICAL_QUERY_LEN ]; +}; + +static int s3erofs_prepare_url (struct s3erofs_curl_request *req, +const char *endpoint, +const char *path, +struct s3erofs_query_params *params, +enum s3erofs_url_style url_style) +{ +static const char https[] ="https://" ; +const char *schema, *host; +bool slash = false; +char *url = req->url; +int pos, i; + +if (!endpoint || !path) +return -EINVAL ; + + schema =strstr (endpoint,"://" ); +if (!schema) { + schema = https; + host = endpoint; + }else { + host = schema +sizeof ("://" ) -1 ; + schema =strndup (endpoint, host - endpoint); +if (!schema) +return -ENOMEM ; + } + +if (url_style ==S3EROFS_URL_STYLE_PATH ) { + pos =snprintf (url,S3EROFS_URL_LEN ,"%s%s/%s" , schema, + host, path); + }else { +const char * split =strchr (path,'/' ); + +if (!split) { + pos =snprintf (url,S3EROFS_URL_LEN ,"%s%s.%s/" , + schema, path, host); + slash = true; + }else { + pos =snprintf (url,S3EROFS_URL_LEN ,"%s%.*s.%s%s" , + schema, (int )(split - path), path, + host, split); + } + } + i =snprintf (req->canonical_query,S3EROFS_CANONICAL_QUERY_LEN , +"/%s%s" , path, slash ?"/" :"" ); + req->canonical_query[i] ='\0' ; + +for (i =0 ; i < params->num; i++) + pos +=snprintf (url + pos,S3EROFS_URL_LEN - pos,"%c%s=%s" , + (!i ?'?' :'&' ), + params->key[i], params->value[i]); +if (schema != https) +free ((void *)schema); +return 0 ; +} + +static char *get_canonical_headers (const struct curl_slist *list) {return "" ; } + +// See: https://docs.aws.amazon.com/AmazonS3/latest/API/RESTAuthentication.html#ConstructingTheAuthenticationHeader +static char *s3erofs_sigv2_header (const struct curl_slist *headers, +const char *method,const char *content_md5, +const char *content_type,const char *date, +const char *canonical_query,const char *ak,const char *sk) +{ +u8 hmac_signature[EVP_MAX_MD_SIZE ]; +char *str, *output =NULL ; +unsigned int len, pos, output_len; +const char *canonical_headers =get_canonical_headers (headers); +const char *prefix ="Authorization: AWS " ; + +if (!method || !date || !ak || !sk) +return ERR_PTR (-EINVAL ); + +if (!content_md5) + content_md5 ="" ; +if (!content_type) + content_type ="" ; +if (!canonical_query) + canonical_query ="/" ; + + pos =asprintf (&str,"%s\n%s\n%s\n%s\n%s%s" , method, content_md5, + content_type, date, canonical_headers, canonical_query); +if (pos <0 ) +return ERR_PTR (-ENOMEM ); + +if (!HMAC (EVP_sha1 (), sk,strlen (sk), (u8 *)str,strlen (str), hmac_signature, &len)) + goto free_string; + + output_len =BASE64_ENCODE_LEN (len); + output_len +=strlen (prefix); + output_len +=strlen (ak); + output_len +=1 ;/* for ':' between ak and signature */ + + output = (char *)malloc (output_len +1 ); +if (!output) + goto free_string; + + pos =snprintf (output, output_len,"%s%s:" , prefix, ak); +if (pos <0 ) + goto free_string; +EVP_EncodeBlock ((u8 *)output + pos, hmac_signature, len); +free_string: +free (str); +return output ?:ERR_PTR (-ENOMEM ); +} + +static void s3erofs_now_rfc1123 (char *buf,size_t maxlen) +{ +time_t now =time (NULL ); +struct tm *ptm =gmtime (&now); + +strftime (buf, maxlen,"%a, %d %b %Y %H:%M:%S GMT" , ptm); +} + +struct s3erofs_curl_response { +char *data; +size_t size; +}; + +static size_t s3erofs_request_write_memory_cb (void *contents,size_t size, +size_t nmemb,void *userp) +{ +size_t realsize = size * nmemb; +struct s3erofs_curl_response *response = userp; +void *tmp; + + tmp =realloc (response->data, response->size + realsize +1 ); +if (tmp ==NULL ) +return 0 ; + + response->data = tmp; + +memcpy (response->data + response->size, contents, realsize); + response->size += realsize; + response->data[response->size] ='\0' ; +return realsize; +} + +static int s3erofs_request_insert_auth (struct curl_slist **request_headers, +const char *method, +const char *canonical_query, +const char *ak,const char *sk) +{ +static const char date_prefix[] ="Date: " ; +char date[64 ], *sigv2; + +memcpy (date, date_prefix,sizeof (date_prefix) -1 ); +s3erofs_now_rfc1123 (date +sizeof (date_prefix) -1 , +sizeof (date) -sizeof (date_prefix) +1 ); + + sigv2 =s3erofs_sigv2_header (*request_headers, method,NULL ,NULL , + date +sizeof (date_prefix) -1 , + canonical_query, ak, sk); +if (IS_ERR (sigv2)) +return PTR_ERR (sigv2); + + *request_headers =curl_slist_append (*request_headers, date); + *request_headers =curl_slist_append (*request_headers, sigv2); + +free (sigv2); +return 0 ; +} + +static int s3erofs_request_perform (struct erofs_s3 *s3, +struct s3erofs_curl_request *req,void *resp) +{ +struct curl_slist *request_headers =NULL ; +long http_code =0 ; +int ret; + +if (s3->access_key[0 ]) { + ret =s3erofs_request_insert_auth (&request_headers, req->method, + req->canonical_query, + s3->access_key, s3->secret_key); +if (ret <0 ) { +erofs_err ("failed to insert auth headers" ); +return ret; + } + } + +curl_easy_setopt (easy_curl,CURLOPT_URL , req->url); +curl_easy_setopt (easy_curl,CURLOPT_WRITEDATA , resp); +curl_easy_setopt (easy_curl,CURLOPT_HTTPHEADER , request_headers); + + ret =curl_easy_perform (easy_curl); +if (ret !=CURLE_OK ) { +erofs_err ("curl_easy_perform() failed: %s" , +curl_easy_strerror (ret)); + ret = -EIO ; + goto err_header; + } + + ret =curl_easy_getinfo (easy_curl,CURLINFO_RESPONSE_CODE , &http_code); +if (ret !=CURLE_OK ) { +erofs_err ("curl_easy_getinfo() failed: %s" , +curl_easy_strerror (ret)); + ret = -EIO ; + goto err_header; + } + +if (!(http_code >=200 && http_code <300 )) { +erofs_err ("request failed with HTTP code %ld" , http_code); + ret = -EIO ; + } + +err_header: +curl_slist_free_all (request_headers); +return ret; +} + +struct s3erofs_object_info { +char *key; +u64 size; +time_t mtime; +u32 mtime_ns; +}; + +struct s3erofs_object_iterator { +struct erofs_s3 *s3; +struct s3erofs_object_info *objects; +int cur; + +char *bucket, *prefix; +const char *delimiter; + +char *next_marker; +bool is_truncated; +}; + +static int s3erofs_parse_list_objects_one (xmlNodePtr node, +struct s3erofs_object_info *info) +{ +xmlNodePtr child; +xmlChar *str; + +for (child = node->children; child; child = child->next) { +if (child->type ==XML_ELEMENT_NODE ) { + str =xmlNodeGetContent (child); +if (!str) +return -ENOMEM ; + +if (xmlStrEqual (child->name, (const xmlChar *)"LastModified" )) { +struct tm tm; +char *end; + + end =strptime ((char *)str,"%Y-%m-%dT%H:%M:%S" , &tm); +if (!end || (*end !='.' && *end !='Z' && *end !='\0' )) { +xmlFree (str); +return -EIO ; + } +if (*end =='.' ) { + info->mtime_ns =strtoul (end +1 , &end,10 ); +if (*end !='Z' && *end !='\0' ) { +xmlFree (str); +return -EIO ; + } + } + info->mtime =mktime (&tm); + } +if (xmlStrEqual (child->name, (const xmlChar *)"Key" )) + info->key =strdup ((char *)str); +else if (xmlStrEqual (child->name, (const xmlChar *)"Size" )) + info->size =atoll ((char *)str); +xmlFree (str); + } + } +return 0 ; +} + +static int s3erofs_parse_list_objects_result (const char *data,int len, +struct s3erofs_object_iterator *it) +{ +xmlNodePtr root =NULL , node, next; +int ret, i, contents_count; +xmlDocPtr doc =NULL ; +xmlChar *str; +void *tmp; + + doc =xmlReadMemory (data, len,NULL ,NULL ,0 ); +if (!doc) { +erofs_err ("failed to parse XML data" ); +return -EINVAL ; + } + + root =xmlDocGetRootElement (doc); +if (!root) { +erofs_err ("failed to get root element" ); + ret = -EINVAL ; + goto out; + } + +if (!xmlStrEqual (root->name, (const xmlChar *)"ListBucketResult" )) { +erofs_err ("invalid root element: expected ListBucketResult, got %s" , root->name); + ret = -EINVAL ; + goto out; + } + +// https://docs.aws.amazon.com/AmazonS3/latest/API/API_ListObjects.html#AmazonS3-ListObjects-response-NextMarker +free (it->next_marker); + it->next_marker =NULL ; + + contents_count =1 ; +for (node = root->children; node; node = next) { + next = node->next; +if (node->type ==XML_ELEMENT_NODE ) { +if (xmlStrEqual (node->name, (const xmlChar *)"Contents" )) { + ++contents_count; +continue ; + } +if (xmlStrEqual (node->name, (const xmlChar *)"IsTruncated" )) { + str =xmlNodeGetContent (node); +if (str) { + it->is_truncated = + !!xmlStrEqual (str, (const xmlChar *)"true" ); +xmlFree (str); + } + }else if (xmlStrEqual (node->name, (const xmlChar *)"NextMarker" )) { + str =xmlNodeGetContent (node); +if (str) { + it->next_marker =strdup ((char *)str); +xmlFree (str); +if (!it->next_marker) { + ret = -ENOMEM ; + goto out; + } + } + } +xmlUnlinkNode (node); + } +xmlUnlinkNode (node); +xmlFreeNode (node); + } + + i =0 ; +if (it->objects) { +for (; it->objects[i].key; ++i) { +free (it->objects[i].key); + it->objects[i].key =NULL ; + } + } + +if (i +1 < contents_count) { + tmp =malloc (contents_count *sizeof (*it->objects)); +if (!tmp) { + ret = -ENOMEM ; + goto out; + } +free (it->objects); + it->objects = tmp; + it->objects[0 ].key =NULL ; + } + it->cur =0 ; + + ret =0 ; +for (i =0 , node = root->children; node; node = node->next) { +if (__erofs_unlikely (i >= contents_count -1 )) { +DBG_BUGON (1 ); +continue ; + } + ret =s3erofs_parse_list_objects_one (node, &it->objects[i]); +if (ret <0 ) { +erofs_err ("failed to parse contents node %s: %s" , + (const char *)node->name,erofs_strerror (ret)); +break ; + } + it->objects[++i].key =NULL ; + } + +/* +* `NextMarker` is returned only if the `delimiter` request parameter +* is specified. +* +* If the response is truncated and does not include `NextMarker`, use +* the value of the last `Key` element in the response as the `marker` +* parameter in the next request. +*/ +if (!ret && i && it->is_truncated && !it->next_marker) { + it->next_marker =strdup (it->objects[i -1 ].key); +if (!it->next_marker) + ret = -ENOMEM ; + } + +if (!ret) + ret = i; +out: +xmlFreeDoc (doc); +return ret; +} + +static int s3erofs_list_objects (struct s3erofs_object_iterator *it) +{ +struct s3erofs_curl_request req = {}; +struct s3erofs_curl_response resp = {}; +struct s3erofs_query_params params; +struct erofs_s3 *s3 = it->s3; +int ret =0 ; + +if (it->delimiter &&strlen (it->delimiter) >S3EROFS_PATH_MAX ) { +erofs_err ("delimiter is too long" ); +return -EINVAL ; + } + + params.num =0 ; +if (it->prefix) { + params.key[params.num] ="prefix" ; + params.value[params.num] = it->prefix; + ++params.num; + } + +if (it->delimiter) { + params.key[params.num] ="delimiter" ; + params.value[params.num] = it->delimiter; + ++params.num; + } + +if (it->next_marker) { + params.key[params.num] ="marker" ; + params.value[params.num] = it->next_marker; + ++params.num; + } + + req.method ="GET" ; + ret =s3erofs_prepare_url (&req, s3->endpoint, it->bucket, + ¶ms, s3->url_style); +if (ret <0 ) +return ret; + + ret =s3erofs_request_perform (s3, &req, &resp); +if (ret <0 ) +return ret; + + ret =s3erofs_parse_list_objects_result (resp.data, resp.size, it); +if (ret <0 ) +return ret; +free (resp.data); +return 0 ; +} + +static struct s3erofs_object_iterator * +s3erofs_create_object_iterator (struct erofs_s3 *s3,const char *path, +const char *delimiter) +{ +struct s3erofs_object_iterator *iter; +char *prefix; + + iter =calloc (1 ,sizeof (struct s3erofs_object_iterator )); +if (!iter) +return ERR_PTR (-ENOMEM ); + iter->s3 = s3; + prefix =strchr (path,'/' ); +if (prefix) { +if (++prefix - path >S3EROFS_PATH_MAX ) +return ERR_PTR (-EINVAL ); + iter->bucket =strndup (path, prefix - path); + iter->prefix =strdup (prefix); + }else { + iter->bucket =strdup (path); + iter->prefix =NULL ; + } + iter->delimiter = delimiter; + iter->is_truncated = true; +return iter; +} + +static void s3erofs_destroy_object_iterator (struct s3erofs_object_iterator *it) +{ +int i; + +if (it->next_marker) +free (it->next_marker); +if (it->objects) { +for (i =0 ; it->objects[i].key; ++i) +free (it->objects[i].key); +free (it->objects); + } +free (it->prefix); +free (it->bucket); +free (it); +} + +static struct s3erofs_object_info * +s3erofs_get_next_object (struct s3erofs_object_iterator *it) +{ +int ret; + +if (it->objects && it->objects[it->cur].key) +return &it->objects[it->cur++]; + +if (it->is_truncated) { + ret =s3erofs_list_objects (it); +if (ret <0 ) +return ERR_PTR (ret); +return &it->objects[it->cur++]; + } +return NULL ; +} + +static int s3erofs_global_init (void ) +{ +if (curl_global_init (CURL_GLOBAL_DEFAULT ) !=CURLE_OK ) +return -EIO ; + + easy_curl =curl_easy_init (); +if (!easy_curl) + goto out_err; + +if (curl_easy_setopt (easy_curl,CURLOPT_WRITEFUNCTION , + s3erofs_request_write_memory_cb) !=CURLE_OK ) + goto out_err; + +if (curl_easy_setopt (easy_curl,CURLOPT_FOLLOWLOCATION ,1L ) !=CURLE_OK ) + goto out_err; + +if (curl_easy_setopt (easy_curl,CURLOPT_TIMEOUT ,30L ) !=CURLE_OK ) + goto out_err; + +if (curl_easy_setopt (easy_curl,CURLOPT_USERAGENT , +"s3erofs/" PACKAGE_VERSION ) !=CURLE_OK ) + goto out_err; + +xmlInitParser (); +return 0 ; +out_err: +curl_global_cleanup (); +return -EIO ; +} + +static void s3erofs_global_exit (void ) +{ +if (!easy_curl) +return ; + +xmlCleanupParser (); + +curl_easy_cleanup (easy_curl); + easy_curl =NULL ; + +curl_global_cleanup (); +} + +int s3erofs_build_trees (struct erofs_inode *root,struct erofs_s3 *s3, +const char *path) +{ +struct erofs_sb_info *sbi = root->sbi; +struct s3erofs_object_iterator *iter; +struct s3erofs_object_info *obj; +struct erofs_dentry *d; +struct erofs_inode *inode; +struct stat st; +char *trimmed; +bool dumb; +int ret; + + st.st_uid = root->i_uid; + st.st_gid = root->i_gid; + + ret =s3erofs_global_init (); +if (ret) { +erofs_err ("failed to initialize s3erofs: %s" ,erofs_strerror (ret)); +return ret; + } + + iter =s3erofs_create_object_iterator (s3, path,NULL ); +if (IS_ERR (iter)) { +erofs_err ("failed to create object iterator" ); + ret =PTR_ERR (iter); + goto err_global; + } + +while (1 ) { + obj =s3erofs_get_next_object (iter); +if (!obj) { +break ; + }else if (IS_ERR (obj)) { +erofs_err ("failed to get next object" ); + ret =PTR_ERR (obj); + goto err_iter; + } + + d =erofs_rebuild_get_dentry (root, obj->key, false, + &dumb, &dumb, false); +if (IS_ERR (d)) { + ret =PTR_ERR (d); + goto err_iter; + } +if (d->type ==EROFS_FT_DIR ) { + inode = d->inode; + inode->i_mode =S_IFDIR |0755 ; + }else { + inode =erofs_new_inode (sbi); +if (IS_ERR (inode)) { + ret =PTR_ERR (inode); + goto err_iter; + } + + inode->i_mode =S_IFREG |0644 ; + inode->i_parent = d->inode; + inode->i_nlink =1 ; + + d->inode = inode; + d->type =EROFS_FT_REG_FILE ; + } + inode->i_srcpath =strdup (obj->key); +if (!inode->i_srcpath) { + ret = -ENOMEM ; + goto err_iter; + } + + trimmed =erofs_trim_for_progressinfo (inode->i_srcpath, +sizeof ("Importing ..." ) -1 ); +erofs_update_progressinfo ("Importing %s ..." , trimmed); +free (trimmed); + + st.st_mtime = obj->mtime; +ST_MTIM_NSEC_SET (&st, obj->mtime_ns); + ret =__erofs_fill_inode (inode, &st, obj->key); +if (!ret &&S_ISREG (inode->i_mode)) { + inode->i_size = obj->size; + ret =erofs_write_zero_inode (inode); + } +if (ret) + goto err_iter; + } + +err_iter: +s3erofs_destroy_object_iterator (iter); +err_global: +s3erofs_global_exit (); +return ret; +}mkfs/main.c +8
-2@@ -1737,8 +1737,14 @@ int main(int argc, char **argv) goto exit; #ifdef S3EROFS_ENABLED }else if (source_mode ==EROFS_MKFS_SOURCE_S3 ) { - err = -EOPNOTSUPP ; - goto exit; +if (incremental_mode || + dataimport_mode !=EROFS_MKFS_DATA_IMPORT_ZEROFILL ) + err = -EOPNOTSUPP ; +else + err =s3erofs_build_trees (root, &s3cfg, + cfg.c_src_path); +if (err) + goto exit;#endif }
Re: [PATCH v7 4/4] erofs-utils: mkfs: support EROFS meta-only image generation from S3
Quoted 9 lines · zhaoyifan (H)
Tested:
- special filename: 'test%^&hidden.txt' 'test space.txt' '测试文件.txt'
- manyfiles (>1000) directory
- path-style obs backend
Tested-by Yifan Zhao <[email protected]>
Thanks, let me merge into -dev now.
Thanks,
Gao Xiang
Re: [PATCH v7 4/4] erofs-utils: mkfs: support EROFS meta-only image generation from S3
In reply to Gao XiangHi Xiang,
Quoted 737 lines · Gao Xiang
From: Yifan Zhao <[email protected]>
This patch introduces experimental S3 support for mkfs.erofs, allowing EROFS images to be generated from AWS S3 and/or other S3 API-compatible services.
Here are the current limitations:
- Only meta-only EROFS image generation is supported;
- Only AWS Signature Version 2 is supported;
- The S3 object names and sizes are strictly respected during image generation.Co-developed-by Hongbo Li <[email protected]>Signed-off-by Hongbo Li <[email protected]>Signed-off-by Yifan Zhao <[email protected]>Signed-off-by Gao Xiang <[email protected]>---
v7:
- FixNextMarkeromission on the standard AWS S3;
- Addprefixsupport for S3 ListObjects.lib/Makefile.am | 3 + lib/liberofs_s3.h | 3 + lib/remotes/s3.c | 670 ++++++++++++++++++++++++++++++++++++++++++++++ mkfs/main.c | 10 +- 4 files changed, 684 insertions(+), 2 deletions(-)create mode 100644
lib/remotes/s3.clib/Makefile.am +3
-0@@ -67,6 +67,9 @@ else liberofs_la_SOURCES += xxhash.c endif liberofs_la_CFLAGS += ${libcurl_CFLAGS} ${openssl_CFLAGS} ${libxml2_CFLAGS} +if ENABLE_S3 +liberofs_la_SOURCES += remotes/s3.c +endif if ENABLE_EROFS_MT liberofs_la_LDFLAGS = -lpthread liberofs_la_SOURCES += workqueue.clib/liberofs_s3.h +3
-0@@ -33,6 +33,9 @@ struct erofs_s3 {enum s3erofs_signature_version sig; }; +int s3erofs_build_trees (struct erofs_inode *root,struct erofs_s3 *s3, +const char *path); + #ifdef __cplusplus }#endif lib/remotes/s3.c new file +670
-0@@ -0,0 +1,670 @@ +// SPDX-License-Identifier: GPL-2.0+ OR Apache-2.0 +/* +* Copyright (C) 2025 HUAWEI, Inc. +* http://www.huawei.com/ +* Created by Yifan Zhao <[email protected]> +*/ +#define _GNU_SOURCE +#include <stdio.h> +#include <stdlib.h> +#include <time.h> +#include <curl/curl.h> +#include <libxml/parser.h> +#include <openssl/hmac.h> +#include "erofs/internal.h" +#include "erofs/print.h" +#include "erofs/inode.h" +#include "erofs/blobchunk.h" +#include "erofs/rebuild.h" +#include "liberofs_s3.h" + +#define S3EROFS_PATH_MAX 1024 +#define S3EROFS_MAX_QUERY_PARAMS 16 +#define S3EROFS_URL_LEN 8192 +#define S3EROFS_CANONICAL_QUERY_LEN 2048 + +#define BASE64_ENCODE_LEN (len) (((len + 2) / 3) * 4) + +static CURL *easy_curl; + +struct s3erofs_query_params { +int num; +const char *key[S3EROFS_MAX_QUERY_PARAMS ]; +const char *value[S3EROFS_MAX_QUERY_PARAMS ]; +}; + +struct s3erofs_curl_request { +const char *method; +char url[S3EROFS_URL_LEN ]; +char canonical_query[S3EROFS_CANONICAL_QUERY_LEN ]; +}; + +static int s3erofs_prepare_url (struct s3erofs_curl_request *req, +const char *endpoint, +const char *path, +struct s3erofs_query_params *params, +enum s3erofs_url_style url_style) +{ +static const char https[] ="https://" ; +const char *schema, *host; +bool slash = false; +char *url = req->url; +int pos, i; + +if (!endpoint || !path) +return -EINVAL ; + + schema =strstr (endpoint,"://" ); +if (!schema) { + schema = https; + host = endpoint; + }else { + host = schema +sizeof ("://" ) -1 ; + schema =strndup (endpoint, host - endpoint); +if (!schema) +return -ENOMEM ; + } + +if (url_style ==S3EROFS_URL_STYLE_PATH ) { + pos =snprintf (url,S3EROFS_URL_LEN ,"%s%s/%s" , schema, + host, path); + }else { +const char * split =strchr (path,'/' ); + +if (!split) { + pos =snprintf (url,S3EROFS_URL_LEN ,"%s%s.%s/" , + schema, path, host); + slash = true; + }else { + pos =snprintf (url,S3EROFS_URL_LEN ,"%s%.*s.%s%s" , + schema, (int )(split - path), path, + host, split); + } + } + i =snprintf (req->canonical_query,S3EROFS_CANONICAL_QUERY_LEN , +"/%s%s" , path, slash ?"/" :"" ); + req->canonical_query[i] ='\0' ; + +for (i =0 ; i < params->num; i++) + pos +=snprintf (url + pos,S3EROFS_URL_LEN - pos,"%c%s=%s" , + (!i ?'?' :'&' ), + params->key[i], params->value[i]); +if (schema != https) +free ((void *)schema); +return 0 ; +} + +static char *get_canonical_headers (const struct curl_slist *list) {return "" ; } + +// See: https://docs.aws.amazon.com/AmazonS3/latest/API/RESTAuthentication.html#ConstructingTheAuthenticationHeader +static char *s3erofs_sigv2_header (const struct curl_slist *headers, +const char *method,const char *content_md5, +const char *content_type,const char *date, +const char *canonical_query,const char *ak,const char *sk) +{ +u8 hmac_signature[EVP_MAX_MD_SIZE ]; +char *str, *output =NULL ; +unsigned int len, pos, output_len; +const char *canonical_headers =get_canonical_headers (headers); +const char *prefix ="Authorization: AWS " ; + +if (!method || !date || !ak || !sk) +return ERR_PTR (-EINVAL ); + +if (!content_md5) + content_md5 ="" ; +if (!content_type) + content_type ="" ; +if (!canonical_query) + canonical_query ="/" ; + + pos =asprintf (&str,"%s\n%s\n%s\n%s\n%s%s" , method, content_md5, + content_type, date, canonical_headers, canonical_query); +if (pos <0 ) +return ERR_PTR (-ENOMEM ); + +if (!HMAC (EVP_sha1 (), sk,strlen (sk), (u8 *)str,strlen (str), hmac_signature, &len)) + goto free_string; + + output_len =BASE64_ENCODE_LEN (len); + output_len +=strlen (prefix); + output_len +=strlen (ak); + output_len +=1 ;/* for ':' between ak and signature */ + + output = (char *)malloc (output_len +1 ); +if (!output) + goto free_string; + + pos =snprintf (output, output_len,"%s%s:" , prefix, ak); +if (pos <0 ) + goto free_string; +EVP_EncodeBlock ((u8 *)output + pos, hmac_signature, len); +free_string: +free (str); +return output ?:ERR_PTR (-ENOMEM ); +} + +static void s3erofs_now_rfc1123 (char *buf,size_t maxlen) +{ +time_t now =time (NULL ); +struct tm *ptm =gmtime (&now); + +strftime (buf, maxlen,"%a, %d %b %Y %H:%M:%S GMT" , ptm); +} + +struct s3erofs_curl_response { +char *data; +size_t size; +}; + +static size_t s3erofs_request_write_memory_cb (void *contents,size_t size, +size_t nmemb,void *userp) +{ +size_t realsize = size * nmemb; +struct s3erofs_curl_response *response = userp; +void *tmp; + + tmp =realloc (response->data, response->size + realsize +1 ); +if (tmp ==NULL ) +return 0 ; + + response->data = tmp; + +memcpy (response->data + response->size, contents, realsize); + response->size += realsize; + response->data[response->size] ='\0' ; +return realsize; +} + +static int s3erofs_request_insert_auth (struct curl_slist **request_headers, +const char *method, +const char *canonical_query, +const char *ak,const char *sk) +{ +static const char date_prefix[] ="Date: " ; +char date[64 ], *sigv2; + +memcpy (date, date_prefix,sizeof (date_prefix) -1 ); +s3erofs_now_rfc1123 (date +sizeof (date_prefix) -1 , +sizeof (date) -sizeof (date_prefix) +1 ); + + sigv2 =s3erofs_sigv2_header (*request_headers, method,NULL ,NULL , + date +sizeof (date_prefix) -1 , + canonical_query, ak, sk); +if (IS_ERR (sigv2)) +return PTR_ERR (sigv2); + + *request_headers =curl_slist_append (*request_headers, date); + *request_headers =curl_slist_append (*request_headers, sigv2); + +free (sigv2); +return 0 ; +} + +static int s3erofs_request_perform (struct erofs_s3 *s3, +struct s3erofs_curl_request *req,void *resp) +{ +struct curl_slist *request_headers =NULL ; +long http_code =0 ; +int ret; + +if (s3->access_key[0 ]) { + ret =s3erofs_request_insert_auth (&request_headers, req->method, + req->canonical_query, + s3->access_key, s3->secret_key); +if (ret <0 ) { +erofs_err ("failed to insert auth headers" ); +return ret; + } + } + +curl_easy_setopt (easy_curl,CURLOPT_URL , req->url); +curl_easy_setopt (easy_curl,CURLOPT_WRITEDATA , resp); +curl_easy_setopt (easy_curl,CURLOPT_HTTPHEADER , request_headers); + + ret =curl_easy_perform (easy_curl); +if (ret !=CURLE_OK ) { +erofs_err ("curl_easy_perform() failed: %s" , +curl_easy_strerror (ret)); + ret = -EIO ; + goto err_header; + } + + ret =curl_easy_getinfo (easy_curl,CURLINFO_RESPONSE_CODE , &http_code); +if (ret !=CURLE_OK ) { +erofs_err ("curl_easy_getinfo() failed: %s" , +curl_easy_strerror (ret)); + ret = -EIO ; + goto err_header; + } + +if (!(http_code >=200 && http_code <300 )) { +erofs_err ("request failed with HTTP code %ld" , http_code); + ret = -EIO ; + } + +err_header: +curl_slist_free_all (request_headers); +return ret; +} + +struct s3erofs_object_info { +char *key; +u64 size; +time_t mtime; +u32 mtime_ns; +}; + +struct s3erofs_object_iterator { +struct erofs_s3 *s3; +struct s3erofs_object_info *objects; +int cur; + +char *bucket, *prefix; +const char *delimiter; + +char *next_marker; +bool is_truncated; +}; + +static int s3erofs_parse_list_objects_one (xmlNodePtr node, +struct s3erofs_object_info *info) +{ +xmlNodePtr child; +xmlChar *str; + +for (child = node->children; child; child = child->next) { +if (child->type ==XML_ELEMENT_NODE ) { + str =xmlNodeGetContent (child); +if (!str) +return -ENOMEM ; + +if (xmlStrEqual (child->name, (const xmlChar *)"LastModified" )) { +struct tm tm; +char *end; + + end =strptime ((char *)str,"%Y-%m-%dT%H:%M:%S" , &tm); +if (!end || (*end !='.' && *end !='Z' && *end !='\0' )) { +xmlFree (str); +return -EIO ; + } +if (*end =='.' ) { + info->mtime_ns =strtoul (end +1 , &end,10 ); +if (*end !='Z' && *end !='\0' ) { +xmlFree (str); +return -EIO ; + } + } + info->mtime =mktime (&tm); + } +if (xmlStrEqual (child->name, (const xmlChar *)"Key" )) + info->key =strdup ((char *)str); +else if (xmlStrEqual (child->name, (const xmlChar *)"Size" )) + info->size =atoll ((char *)str); +xmlFree (str); + } + } +return 0 ; +} + +static int s3erofs_parse_list_objects_result (const char *data,int len, +struct s3erofs_object_iterator *it) +{ +xmlNodePtr root =NULL , node, next; +int ret, i, contents_count; +xmlDocPtr doc =NULL ; +xmlChar *str; +void *tmp; + + doc =xmlReadMemory (data, len,NULL ,NULL ,0 ); +if (!doc) { +erofs_err ("failed to parse XML data" ); +return -EINVAL ; + } + + root =xmlDocGetRootElement (doc); +if (!root) { +erofs_err ("failed to get root element" ); + ret = -EINVAL ; + goto out; + } + +if (!xmlStrEqual (root->name, (const xmlChar *)"ListBucketResult" )) { +erofs_err ("invalid root element: expected ListBucketResult, got %s" , root->name); + ret = -EINVAL ; + goto out; + } + +// https://docs.aws.amazon.com/AmazonS3/latest/API/API_ListObjects.html#AmazonS3-ListObjects-response-NextMarker +free (it->next_marker); + it->next_marker =NULL ; + + contents_count =1 ; +for (node = root->children; node; node = next) { + next = node->next; +if (node->type ==XML_ELEMENT_NODE ) { +if (xmlStrEqual (node->name, (const xmlChar *)"Contents" )) { + ++contents_count; +continue ; + } +if (xmlStrEqual (node->name, (const xmlChar *)"IsTruncated" )) { + str =xmlNodeGetContent (node); +if (str) { + it->is_truncated = + !!xmlStrEqual (str, (const xmlChar *)"true" ); +xmlFree (str); + } + }else if (xmlStrEqual (node->name, (const xmlChar *)"NextMarker" )) { + str =xmlNodeGetContent (node); +if (str) { + it->next_marker =strdup ((char *)str); +xmlFree (str); +if (!it->next_marker) { + ret = -ENOMEM ; + goto out; + } + } + } +xmlUnlinkNode (node); + } +xmlUnlinkNode (node); +xmlFreeNode (node); + } + + i =0 ; +if (it->objects) { +for (; it->objects[i].key; ++i) { +free (it->objects[i].key); + it->objects[i].key =NULL ; + } + } + +if (i +1 < contents_count) { + tmp =malloc (contents_count *sizeof (*it->objects)); +if (!tmp) { + ret = -ENOMEM ; + goto out; + } +free (it->objects); + it->objects = tmp; + it->objects[0 ].key =NULL ; + } + it->cur =0 ; + + ret =0 ; +for (i =0 , node = root->children; node; node = node->next) { +if (__erofs_unlikely (i >= contents_count -1 )) { +DBG_BUGON (1 ); +continue ; + } + ret =s3erofs_parse_list_objects_one (node, &it->objects[i]); +if (ret <0 ) { +erofs_err ("failed to parse contents node %s: %s" , + (const char *)node->name,erofs_strerror (ret)); +break ; + } + it->objects[++i].key =NULL ; + } + +/* +* `NextMarker` is returned only if the `delimiter` request parameter +* is specified. +* +* If the response is truncated and does not include `NextMarker`, use +* the value of the last `Key` element in the response as the `marker` +* parameter in the next request. +*/ +if (!ret && i && it->is_truncated && !it->next_marker) { + it->next_marker =strdup (it->objects[i -1 ].key); +if (!it->next_marker) + ret = -ENOMEM ; + } + +if (!ret) + ret = i; +out: +xmlFreeDoc (doc); +return ret; +} + +static int s3erofs_list_objects (struct s3erofs_object_iterator *it) +{ +struct s3erofs_curl_request req = {}; +struct s3erofs_curl_response resp = {}; +struct s3erofs_query_params params; +struct erofs_s3 *s3 = it->s3; +int ret =0 ; + +if (it->delimiter &&strlen (it->delimiter) >S3EROFS_PATH_MAX ) { +erofs_err ("delimiter is too long" ); +return -EINVAL ; + } + + params.num =0 ; +if (it->prefix) { + params.key[params.num] ="prefix" ; + params.value[params.num] = it->prefix; + ++params.num; + } + +if (it->delimiter) { + params.key[params.num] ="delimiter" ; + params.value[params.num] = it->delimiter; + ++params.num; + } + +if (it->next_marker) { + params.key[params.num] ="marker" ; + params.value[params.num] = it->next_marker; + ++params.num; + } + + req.method ="GET" ; + ret =s3erofs_prepare_url (&req, s3->endpoint, it->bucket, + ¶ms, s3->url_style); +if (ret <0 ) +return ret; + + ret =s3erofs_request_perform (s3, &req, &resp); +if (ret <0 ) +return ret; + + ret =s3erofs_parse_list_objects_result (resp.data, resp.size, it); +if (ret <0 ) +return ret; +free (resp.data); +return 0 ; +} + +static struct s3erofs_object_iterator * +s3erofs_create_object_iterator (struct erofs_s3 *s3,const char *path, +const char *delimiter) +{ +struct s3erofs_object_iterator *iter; +char *prefix; + + iter =calloc (1 ,sizeof (struct s3erofs_object_iterator )); +if (!iter) +return ERR_PTR (-ENOMEM ); + iter->s3 = s3; + prefix =strchr (path,'/' ); +if (prefix) { +if (++prefix - path >S3EROFS_PATH_MAX ) +return ERR_PTR (-EINVAL ); + iter->bucket =strndup (path, prefix - path); + iter->prefix =strdup (prefix); + }else { + iter->bucket =strdup (path); + iter->prefix =NULL ; + } + iter->delimiter = delimiter; + iter->is_truncated = true; +return iter; +} + +static void s3erofs_destroy_object_iterator (struct s3erofs_object_iterator *it) +{ +int i; + +if (it->next_marker) +free (it->next_marker); +if (it->objects) { +for (i =0 ; it->objects[i].key; ++i) +free (it->objects[i].key); +free (it->objects); + } +free (it->prefix); +free (it->bucket); +free (it); +} + +static struct s3erofs_object_info * +s3erofs_get_next_object (struct s3erofs_object_iterator *it) +{ +int ret; + +if (it->objects && it->objects[it->cur].key) +return &it->objects[it->cur++]; + +if (it->is_truncated) { + ret =s3erofs_list_objects (it); +if (ret <0 ) +return ERR_PTR (ret); +return &it->objects[it->cur++]; + } +return NULL ; +} + +static int s3erofs_global_init (void ) +{ +if (curl_global_init (CURL_GLOBAL_DEFAULT ) !=CURLE_OK ) +return -EIO ; + + easy_curl =curl_easy_init (); +if (!easy_curl) + goto out_err; + +if (curl_easy_setopt (easy_curl,CURLOPT_WRITEFUNCTION , + s3erofs_request_write_memory_cb) !=CURLE_OK ) + goto out_err; + +if (curl_easy_setopt (easy_curl,CURLOPT_FOLLOWLOCATION ,1L ) !=CURLE_OK ) + goto out_err; + +if (curl_easy_setopt (easy_curl,CURLOPT_TIMEOUT ,30L ) !=CURLE_OK ) + goto out_err; + +if (curl_easy_setopt (easy_curl,CURLOPT_USERAGENT , +"s3erofs/" PACKAGE_VERSION ) !=CURLE_OK ) + goto out_err; + +xmlInitParser (); +return 0 ; +out_err: +curl_global_cleanup (); +return -EIO ; +} + +static void s3erofs_global_exit (void ) +{ +if (!easy_curl) +return ; + +xmlCleanupParser (); + +curl_easy_cleanup (easy_curl); + easy_curl =NULL ; + +curl_global_cleanup (); +} + +int s3erofs_build_trees (struct erofs_inode *root,struct erofs_s3 *s3, +const char *path) +{ +struct erofs_sb_info *sbi = root->sbi; +struct s3erofs_object_iterator *iter; +struct s3erofs_object_info *obj; +struct erofs_dentry *d; +struct erofs_inode *inode; +struct stat st; +char *trimmed; +bool dumb; +int ret; + + st.st_uid = root->i_uid; + st.st_gid = root->i_gid; + + ret =s3erofs_global_init (); +if (ret) { +erofs_err ("failed to initialize s3erofs: %s" ,erofs_strerror (ret)); +return ret; + } + + iter =s3erofs_create_object_iterator (s3, path,NULL ); +if (IS_ERR (iter)) { +erofs_err ("failed to create object iterator" ); + ret =PTR_ERR (iter); + goto err_global; + } + +while (1 ) { + obj =s3erofs_get_next_object (iter); +if (!obj) { +break ; + }else if (IS_ERR (obj)) { +erofs_err ("failed to get next object" ); + ret =PTR_ERR (obj); + goto err_iter; + } + + d =erofs_rebuild_get_dentry (root, obj->key, false, + &dumb, &dumb, false); +if (IS_ERR (d)) { + ret =PTR_ERR (d); + goto err_iter; + } +if (d->type ==EROFS_FT_DIR ) { + inode = d->inode; + inode->i_mode =S_IFDIR |0755 ; + }else { + inode =erofs_new_inode (sbi); +if (IS_ERR (inode)) { + ret =PTR_ERR (inode); + goto err_iter; + } + + inode->i_mode =S_IFREG |0644 ; + inode->i_parent = d->inode; + inode->i_nlink =1 ; + + d->inode = inode; + d->type =EROFS_FT_REG_FILE ; + } + inode->i_srcpath =strdup (obj->key); +if (!inode->i_srcpath) { + ret = -ENOMEM ; + goto err_iter; + } + + trimmed =erofs_trim_for_progressinfo (inode->i_srcpath, +sizeof ("Importing ..." ) -1 ); +erofs_update_progressinfo ("Importing %s ..." , trimmed); +free (trimmed); + + st.st_mtime = obj->mtime; +ST_MTIM_NSEC_SET (&st, obj->mtime_ns); + ret =__erofs_fill_inode (inode, &st, obj->key); +if (!ret &&S_ISREG (inode->i_mode)) { + inode->i_size = obj->size; + ret =erofs_write_zero_inode (inode); + } +if (ret) + goto err_iter; + } + +err_iter: +s3erofs_destroy_object_iterator (iter); +err_global: +s3erofs_global_exit (); +return ret; +}mkfs/main.c +0
-0
mkfs/main.c int main(int argc, char **argv) · line 1741
Quoted 7 lines
mkfs/main.c +2
-2@@ -1737,8 +1737,14 @@ int main(int argc, char **argv) goto exit; #ifdef S3EROFS_ENABLED }else if (source_mode ==EROFS_MKFS_SOURCE_S3 ) { - err = -EOPNOTSUPP ; - goto exit; +if (incremental_mode || + dataimport_mode !=EROFS_MKFS_DATA_IMPORT_ZEROFILL )
Did you forget setting dataimport_mode as EROFS_MKFS_DATA_IMPORT_ZEROFILL?
mkfs/main.c +1 -0
@@ -1220,6 +1220,7 @@ static int mkfs_parse_options_cfg(int argc, char *argv[]) err =mkfs_parse_s3_cfg (optarg);if (err)return err; + dataimport_mode =EROFS_MKFS_DATA_IMPORT_ZEROFILL ;break ;#endif case 'V' :
Thanks,
Hongbo
Quoted 9 lines
mkfs/main.c +6
-0+ err = -EOPNOTSUPP ; + else + err =s3erofs_build_trees (root, &s3cfg, + cfg.c_src_path); +if (err) + goto exit;#endif }
Re: [PATCH v7 4/4] erofs-utils: mkfs: support EROFS meta-only image generation from S3
On 2025/8/8 15:16, Hongbo Li wrote:
...
Quoted 9 lines
Quoted 1 line
+
dataimport_mode!=EROFS_MKFS_DATA_IMPORT_ZEROFILL)Did you forget setting
dataimport_modeasEROFS_MKFS_DATA_IMPORT_ZEROFILL?mkfs/main.c +0
-0
mkfs/main.c static int mkfs_parse_options_cfg(int argc, char *argv[]) · line 1226
Quoted 7 lines
mkfs/main.c +1
-0@@ -1220,6 +1220,7 @@ static int mkfs_parse_options_cfg(int argc, char *argv[]) err = mkfs_parse_s3_cfg(optarg); if (err) return err; + dataimport_mode = EROFS_MKFS_DATA_IMPORT_ZEROFILL; break; #endif case 'V':
No, users need to specify --clean=0 to make it work.
The default mode is still -EOPNOTSUPP.
Thanks,
Gao Xiang
Quoted 13 lines
Thanks,
HongboQuoted 8 lines
+ err =
-EOPNOTSUPP;
+ else
+ err =s3erofs_build_trees(root, &s3cfg, + cfg.c_src_path); + if (err)
+ goto exit;
#endif
}
Re: [PATCH v7 4/4] erofs-utils: mkfs: support EROFS meta-only image generation from S3
Quoted 27 lines · Gao Xiang
On 2025/8/8 15:16, Hongbo Li wrote:
...
Quoted 16 lines
Quoted 1 line
+
dataimport_mode!=EROFS_MKFS_DATA_IMPORT_ZEROFILL)Did you forget setting
dataimport_modeasEROFS_MKFS_DATA_IMPORT_ZEROFILL?mkfs/main.c +1
-0@@ -1220,6 +1220,7 @@ static int mkfs_parse_options_cfg(int argc, char *argv[]) err = mkfs_parse_s3_cfg(optarg); if (err) return err; + dataimport_mode = EROFS_MKFS_DATA_IMPORT_ZEROFILL; break;Quoted 2 lines
#endif
case 'V':No, users need to specify
--clean=0to make it work.
Quoted 2 lines
The default mode is still
-EOPNOTSUPP.
Ok.
Thanks,
Hongbo
Quoted 17 lines
Thanks,
Gao XiangQuoted 13 lines
Thanks,
HongboQuoted 8 lines
+ err =
-EOPNOTSUPP;
+ else
+ err =s3erofs_build_trees(root, &s3cfg, + cfg.c_src_path); + if (err)
+ goto exit;
#endif
}
Re: [PATCH v7 4/4] erofs-utils: mkfs: support EROFS meta-only image generation from S3
In reply to Hongbo LiQuoted 781 lines · Hongbo Li
Hi Xiang,
Quoted 756 lines · Gao Xiang
From: Yifan Zhao <[email protected]>
This patch introduces experimental S3 support for mkfs.erofs, allowing EROFS images to be generated from AWS S3 and/or other S3 API-compatible services.
Here are the current limitations:
- Only meta-only EROFS image generation is supported;
- Only AWS Signature Version 2 is supported;
- The S3 object names and sizes are strictly respected during image generation.Co-developed-by Hongbo Li <[email protected]>Signed-off-by Hongbo Li <[email protected]>Signed-off-by Yifan Zhao <[email protected]>Signed-off-by Gao Xiang <[email protected]>---
v7:
- FixNextMarkeromission on the standard AWS S3;
- Addprefixsupport for S3 ListObjects.lib/Makefile.am | 3 +
lib/liberofs_s3.h| 3 +
lib/remotes/s3.c | 670 ++++++++++++++++++++++++++++++++++++++++++++++ mkfs/main.c | 10 +-4 files changed, 684 insertions(+), 2 deletions(-)create mode 100644
lib/remotes/s3.clib/Makefile.am +0
-0@@ -67,6 +67,9 @@ else
liberofs_la_SOURCES+=xxhash.c
endif
liberofs_la_CFLAGS+= ${libcurl_CFLAGS} ${openssl_CFLAGS} ${libxml2_CFLAGS}lib/Makefile.am +3
-0+if ENABLE_S3 +liberofs_la_SOURCES += remotes/s3.c +endifif
ENABLE_EROFS_MT
liberofs_la_LDFLAGS= -lpthread
liberofs_la_SOURCES+=workqueue.clib/liberofs_s3.h +2
-0@@ -33,6 +33,9 @@ struct erofs_s3 { enum s3erofs_signature_version sig; }; +int s3erofs_build_trees(struct erofs_inode *root, struct erofs_s3 *s3, + const char *path); + #ifdef __cplusplus } #endiflib/remotes/s3.c new file +97
-0@@ -0,0 +1,670 @@ +// SPDX-License-Identifier: GPL-2.0+ OR Apache-2.0 +/* +* Copyright (C) 2025 HUAWEI, Inc. +* http://www.huawei.com/ +* Created by Yifan Zhao <[email protected]> +*/ +#define _GNU_SOURCE +#include <stdio.h> +#include <stdlib.h> +#include <time.h> +#include <curl/curl.h> +#include <libxml/parser.h> +#include <openssl/hmac.h> +#include "erofs/internal.h" +#include "erofs/print.h" +#include "erofs/inode.h" +#include "erofs/blobchunk.h" +#include "erofs/rebuild.h" +#include "liberofs_s3.h" + +#define S3EROFS_PATH_MAX 1024 +#define S3EROFS_MAX_QUERY_PARAMS 16 +#define S3EROFS_URL_LEN 8192 +#define S3EROFS_CANONICAL_QUERY_LEN 2048 + +#define BASE64_ENCODE_LEN (len) (((len + 2) / 3) * 4) + +static CURL *easy_curl; + +struct s3erofs_query_params { + int num; + const char *key[S3EROFS_MAX_QUERY_PARAMS]; + const char *value[S3EROFS_MAX_QUERY_PARAMS]; +}; + +struct s3erofs_curl_request { + const char *method; + char url[S3EROFS_URL_LEN]; + char canonical_query[S3EROFS_CANONICAL_QUERY_LEN]; +}; + +static int s3erofs_prepare_url (struct s3erofs_curl_request *req, + const char *endpoint, + const char *path, + struct s3erofs_query_params *params, + enum s3erofs_url_style url_style) +{ + static const char https[] = "https://"; + const char *schema, *host; + bool slash = false; + char *url = req->url; + int pos, i; + + if (!endpoint || !path) + return -EINVAL; + + schema = strstr(endpoint, "://"); + if (!schema) { + schema = https; + host = endpoint; + } else { + host = schema + sizeof("://") - 1; + schema = strndup(endpoint, host - endpoint); + if (!schema) + return -ENOMEM; + } + + if (url_style == S3EROFS_URL_STYLE_PATH) { + pos = snprintf(url, S3EROFS_URL_LEN, "%s%s/%s", schema, + host, path); + } else { + const char * split = strchr(path, '/'); + + if (!split) { + pos = snprintf(url, S3EROFS_URL_LEN, "%s%s.%s/", + schema, path, host); + slash = true; + } else { + pos = snprintf(url, S3EROFS_URL_LEN, "%s%.*s.%s%s", + schema, (int)(split - path), path, + host, split); + } + } + i = snprintf(req->canonical_query, S3EROFS_CANONICAL_QUERY_LEN, + "/%s%s", path, slash ? "/" : ""); + req->canonical_query[i] = '\0'; + + for (i = 0; i < params->num; i++) + pos += snprintf(url + pos, S3EROFS_URL_LEN - pos, "%c%s=%s", + (!i ? '?' : '&'), + params->key[i], params->value[i]); + if (schema != https) + free((void *)schema); + return 0; +} + +static char *get_canonical_headers (const struct curl_slist *list) {return ""; }
lib/remotes/s3.c new file +2
-0+ +// See: lib/remotes/s3.c new file +27
-0+static char *s3erofs_sigv2_header(const struct curl_slist *headers, + const char *method, const char *content_md5, + const char *content_type, const char *date, + const char *canonical_query, const char *ak, const char *sk) +{ + u8 hmac_signature[EVP_MAX_MD_SIZE]; + char *str, *output = NULL; + unsigned int len, pos, output_len; + const char *canonical_headers = get_canonical_headers(headers); + const char *prefix = "Authorization: AWS "; + + if (!method || !date || !ak || !sk) + return ERR_PTR(-EINVAL); + + if (!content_md5) + content_md5 = ""; + if (!content_type) + content_type = ""; + if (!canonical_query) + canonical_query = "/"; + + pos = asprintf(&str, "%s\n%s\n%s\n%s\n%s%s", method, content_md5, + content_type, date, canonical_headers, canonical_query); + if (pos < 0) + return ERR_PTR(-ENOMEM); + + if (!HMAC(EVP_sha1(), sk, strlen(sk), (u8 *)str, strlen(str),
hmac_signature, &len))
+ gotofree_string;lib/remotes/s3.c new file +33
-0+ + output_len = BASE64_ENCODE_LEN(len); + output_len += strlen(prefix); + output_len += strlen(ak); + output_len += 1; /* for ':' between ak and signature */ + + output = (char *)malloc(output_len + 1); + if (!output) + goto free_string; + + pos = snprintf(output, output_len, "%s%s:", prefix, ak); + if (pos < 0) + goto free_string; + EVP_EncodeBlock((u8 *)output + pos, hmac_signature, len); +free_string: + free(str); + return output ?: ERR_PTR(-ENOMEM); +} + +static void s3erofs_now_rfc1123 (char *buf,size_t maxlen) +{ + time_t now = time(NULL); + struct tm *ptm = gmtime(&now); + + strftime(buf, maxlen, "%a, %d %b %Y %H:%M:%S GMT", ptm); +} + +struct s3erofs_curl_response { + char *data; + size_t size; +}; + +static size_t s3erofs_request_write_memory_cb(void *contents, size_tsize,
+size_tnmemb, void *userp)lib/remotes/s3.c new file +18
-0+{ + size_t realsize = size * nmemb; + struct s3erofs_curl_response *response = userp; + void *tmp; + + tmp = realloc(response->data, response->size + realsize + 1); + if (tmp == NULL) + return 0; + + response->data = tmp; + + memcpy(response->data + response->size, contents, realsize); + response->size += realsize; + response->data[response->size] = '\0'; + return realsize; +} + +static int s3erofs_request_insert_auth(struct curl_slist**request_headers,
+ const char *method,
+ const char*canonical_query, + const char *ak, const char *sk)lib/remotes/s3.c new file +30
-0+{ + static const char date_prefix[] = "Date: "; + char date[64], *sigv2; + + memcpy(date, date_prefix, sizeof(date_prefix) - 1); + s3erofs_now_rfc1123(date + sizeof(date_prefix) - 1, + sizeof(date) - sizeof(date_prefix) + 1); + + sigv2 = s3erofs_sigv2_header(*request_headers, method, NULL, NULL, + date + sizeof(date_prefix) - 1, + canonical_query, ak, sk); + if (IS_ERR(sigv2)) + return PTR_ERR(sigv2); + + *request_headers = curl_slist_append(*request_headers, date); + *request_headers = curl_slist_append(*request_headers, sigv2); + + free(sigv2); + return 0; +} + +static int s3erofs_request_perform (struct erofs_s3 *s3, + struct s3erofs_curl_request *req, void *resp) +{ + struct curl_slist *request_headers = NULL; + long http_code = 0; + int ret; + + if (s3->access_key[0]) { + ret = s3erofs_request_insert_auth(&request_headers,
req->method,
+req->canonical_query,
+s3->access_key,s3->secret_key); + if (ret < 0) {
+erofs_err("failed to insert auth headers"); + return ret;
+ }
+ }lib/remotes/s3.c new file +14
-0+ + curl_easy_setopt(easy_curl, CURLOPT_URL, req->url); + curl_easy_setopt(easy_curl, CURLOPT_WRITEDATA, resp); + curl_easy_setopt(easy_curl, CURLOPT_HTTPHEADER, request_headers); + + ret = curl_easy_perform(easy_curl); + if (ret != CURLE_OK) { + erofs_err("curl_easy_perform() failed: %s", + curl_easy_strerror(ret)); + ret = -EIO; + goto err_header; + } + + ret = curl_easy_getinfo(easy_curl, CURLINFO_RESPONSE_CODE,
&http_code);
+ if (ret !=CURLE_OK) {
+erofs_err("curl_easy_getinfo() failed: %s", + curl_easy_strerror(ret));
+ ret =-EIO;
+ gotoerr_header;
+ }lib/remotes/s3.c new file +43
-0+ + if (!(http_code >= 200 && http_code < 300)) { + erofs_err("request failed with HTTP code %ld", http_code); + ret = -EIO; + } + +err_header: + curl_slist_free_all(request_headers); + return ret; +} + +struct s3erofs_object_info { + char *key; + u64 size; + time_t mtime; + u32 mtime_ns; +}; + +struct s3erofs_object_iterator { + struct erofs_s3 *s3; + struct s3erofs_object_info *objects; + int cur; + + char *bucket, *prefix; + const char *delimiter; + + char *next_marker; + bool is_truncated; +}; + +static int s3erofs_parse_list_objects_one (xmlNodePtr node, + struct s3erofs_object_info *info) +{ + xmlNodePtr child; + xmlChar *str; + + for (child = node->children; child; child = child->next) { + if (child->type == XML_ELEMENT_NODE) { + str = xmlNodeGetContent(child); + if (!str) + return -ENOMEM; + + if (xmlStrEqual(child->name, (const xmlChar*)"LastModified")) {
+ struct tm tm;
+ char *end;
+
+ end =strptime((char *)str, "%Y-%m-%dT%H:%M:%S", &tm); + if (!end || (*end != '.' && *end != 'Z' && *end != '\0')) {
+ xmlFree(str);
+ return-EIO;
+ }
+ if (*end == '.') {
+info->mtime_ns=strtoul(end + 1, &end, 10); + if (*end != 'Z' && *end != '\0') {
+ xmlFree(str);
+ return-EIO;
+ }
+ }
+info->mtime=mktime(&tm);
+ }
+ if (xmlStrEqual(child->name, (const xmlChar *)"Key")) +info->key=strdup((char *)str);
+ else if (xmlStrEqual(child->name, (const xmlChar *)"Size")) +info->size=atoll((char *)str);
+ xmlFree(str);
+ }
+ }
+ return 0;lib/remotes/s3.c new file +25
-0+} + +static int s3erofs_parse_list_objects_result(const char *data, int len, + struct s3erofs_object_iterator *it) +{ + xmlNodePtr root = NULL, node, next; + int ret, i, contents_count; + xmlDocPtr doc = NULL; + xmlChar *str; + void *tmp; + + doc = xmlReadMemory(data, len, NULL, NULL, 0); + if (!doc) { + erofs_err("failed to parse XML data"); + return -EINVAL; + } + + root = xmlDocGetRootElement(doc); + if (!root) { + erofs_err("failed to get root element"); + ret = -EINVAL; + goto out; + } + + if (!xmlStrEqual(root->name, (const xmlChar*)"ListBucketResult")) {
+erofs_err("invalid root element: expected ListBucketResult, got %s", root->name);
+ ret =-EINVAL;
+ goto out;
+ }
+
+ //
https://docs.aws.amazon.com/AmazonS3/latest/API/API_ListObjects.html#AmazonS3-ListObjects-response-NextMarker
+free(it->next_marker);
+it->next_marker= NULL;
+
+contents_count= 1;
+ for (node =root->children; node; node = next) {
+ next =node->next;
+ if (node->type==XML_ELEMENT_NODE) {
+ if (xmlStrEqual(node->name, (const xmlChar *)"Contents")) { + ++contents_count;
+ continue;
+ }
+ if (xmlStrEqual(node->name, (const xmlChar *)"IsTruncated")) {
+ str = xmlNodeGetContent(node);
+ if (str) {
+it->is_truncated=
+ !!xmlStrEqual(str, (const xmlChar *)"true"); + xmlFree(str);
+ }
+ } else if (xmlStrEqual(node->name, (const xmlChar *)"NextMarker")) {
+ str = xmlNodeGetContent(node);
+ if (str) {
+it->next_marker=strdup((char *)str); + xmlFree(str);
+ if (!it->next_marker) {
+ ret =-ENOMEM;
+ goto out;
+ }
+ }
+ }
+ xmlUnlinkNode(node);
+ }
+ xmlUnlinkNode(node);
+ xmlFreeNode(node);
+ }lib/remotes/s3.c new file +38
-0+ + i = 0; + if (it->objects) { + for (; it->objects[i].key; ++i) { + free(it->objects[i].key); + it->objects[i].key = NULL; + } + } + + if (i + 1 < contents_count) { + tmp = malloc(contents_count * sizeof(*it->objects)); + if (!tmp) { + ret = -ENOMEM; + goto out; + } + free(it->objects); + it->objects = tmp; + it->objects[0].key = NULL; + } + it->cur = 0; + + ret = 0; + for (i = 0, node = root->children; node; node = node->next) { + if (__erofs_unlikely(i >= contents_count - 1)) { + DBG_BUGON(1); + continue; + } + ret = s3erofs_parse_list_objects_one(node, &it->objects[i]); + if (ret < 0) { + erofs_err("failed to parse contents node %s: %s", + (const char *)node->name, erofs_strerror(ret)); + break; + } + it->objects[++i].key = NULL; + } + + /* +* `NextMarker` is returned only if the `delimiter` request parameter
+ * is specified.
+ *
+ * If the response is truncated and does not includeNextMarker, use
+ * the value of the lastKeyelement in the response as themarker
+ * parameter in the next request.
+ */
+ if (!ret && i &&it->is_truncated&& !it->next_marker) { +it->next_marker=strdup(it->objects[i - 1].key); + if (!it->next_marker)
+ ret =-ENOMEM;
+ }lib/remotes/s3.c new file +84
-0+ + if (!ret) + ret = i; +out: + xmlFreeDoc(doc); + return ret; +} + +static int s3erofs_list_objects (struct s3erofs_object_iterator *it) +{ + struct s3erofs_curl_request req = {}; + struct s3erofs_curl_response resp = {}; + struct s3erofs_query_params params; + struct erofs_s3 *s3 = it->s3; + int ret = 0; + + if (it->delimiter && strlen(it->delimiter) > S3EROFS_PATH_MAX) { + erofs_err("delimiter is too long"); + return -EINVAL; + } + + params.num = 0; + if (it->prefix) { + params.key[params.num] = "prefix"; + params.value[params.num] = it->prefix; + ++params.num; + } + + if (it->delimiter) { + params.key[params.num] = "delimiter"; + params.value[params.num] = it->delimiter; + ++params.num; + } + + if (it->next_marker) { + params.key[params.num] = "marker"; + params.value[params.num] = it->next_marker; + ++params.num; + } + + req.method = "GET"; + ret = s3erofs_prepare_url(&req, s3->endpoint, it->bucket, + ¶ms, s3->url_style); + if (ret < 0) + return ret; + + ret = s3erofs_request_perform(s3, &req, &resp); + if (ret < 0) + return ret; + + ret = s3erofs_parse_list_objects_result(resp.data, resp.size, it); + if (ret < 0) + return ret; + free(resp.data); + return 0; +} + +static struct s3erofs_object_iterator * +s3erofs_create_object_iterator (struct erofs_s3 *s3,const char *path, + const char *delimiter) +{ + struct s3erofs_object_iterator *iter; + char *prefix; + + iter = calloc(1, sizeof(struct s3erofs_object_iterator)); + if (!iter) + return ERR_PTR(-ENOMEM); + iter->s3 = s3; + prefix = strchr(path, '/'); + if (prefix) { + if (++prefix - path > S3EROFS_PATH_MAX) + return ERR_PTR(-EINVAL); + iter->bucket = strndup(path, prefix - path); + iter->prefix = strdup(prefix); + } else { + iter->bucket = strdup(path); + iter->prefix = NULL; + } + iter->delimiter = delimiter; + iter->is_truncated = true; + return iter; +} + +static void s3erofs_destroy_object_iterator(struct
s3erofs_object_iterator*it)lib/remotes/s3.c new file +46
-0+{ + int i; + + if (it->next_marker) + free(it->next_marker); + if (it->objects) { + for (i = 0; it->objects[i].key; ++i) + free(it->objects[i].key); + free(it->objects); + } + free(it->prefix); + free(it->bucket); + free(it); +} + +static struct s3erofs_object_info * +s3erofs_get_next_object (struct s3erofs_object_iterator *it) +{ + int ret; + + if (it->objects && it->objects[it->cur].key) + return &it->objects[it->cur++]; + + if (it->is_truncated) { + ret = s3erofs_list_objects(it); + if (ret < 0) + return ERR_PTR(ret); + return &it->objects[it->cur++]; + } + return NULL; +} + +static int s3erofs_global_init (void ) +{ + if (curl_global_init(CURL_GLOBAL_DEFAULT) != CURLE_OK) + return -EIO; + + easy_curl = curl_easy_init(); + if (!easy_curl) + goto out_err; + + if (curl_easy_setopt(easy_curl, CURLOPT_WRITEFUNCTION, + s3erofs_request_write_memory_cb) != CURLE_OK) + goto out_err; + + if (curl_easy_setopt(easy_curl, CURLOPT_FOLLOWLOCATION, 1L) !=
CURLE_OK)
+ gotoout_err;lib/remotes/s3.c new file +47
-0+ + if (curl_easy_setopt(easy_curl, CURLOPT_TIMEOUT, 30L) != CURLE_OK) + goto out_err; + + if (curl_easy_setopt(easy_curl, CURLOPT_USERAGENT, + "s3erofs/" PACKAGE_VERSION) != CURLE_OK) + goto out_err; + + xmlInitParser(); + return 0; +out_err: + curl_global_cleanup(); + return -EIO; +} + +static void s3erofs_global_exit (void ) +{ + if (!easy_curl) + return; + + xmlCleanupParser(); + + curl_easy_cleanup(easy_curl); + easy_curl = NULL; + + curl_global_cleanup(); +} + +int s3erofs_build_trees (struct erofs_inode *root,struct erofs_s3 *s3, + const char *path) +{ + struct erofs_sb_info *sbi = root->sbi; + struct s3erofs_object_iterator *iter; + struct s3erofs_object_info *obj; + struct erofs_dentry *d; + struct erofs_inode *inode; + struct stat st; + char *trimmed; + bool dumb; + int ret; + + st.st_uid = root->i_uid; + st.st_gid = root->i_gid; + + ret = s3erofs_global_init(); + if (ret) { + erofs_err("failed to initialize s3erofs: %s",
erofs_strerror(ret));
+ return ret;
+ }lib/remotes/s3.c new file +69
-0+ + iter = s3erofs_create_object_iterator(s3, path, NULL); + if (IS_ERR(iter)) { + erofs_err("failed to create object iterator"); + ret = PTR_ERR(iter); + goto err_global; + } + + while (1) { + obj = s3erofs_get_next_object(iter); + if (!obj) { + break; + } else if (IS_ERR(obj)) { + erofs_err("failed to get next object"); + ret = PTR_ERR(obj); + goto err_iter; + } + + d = erofs_rebuild_get_dentry(root, obj->key, false, + &dumb, &dumb, false); + if (IS_ERR(d)) { + ret = PTR_ERR(d); + goto err_iter; + } + if (d->type == EROFS_FT_DIR) { + inode = d->inode; + inode->i_mode = S_IFDIR | 0755; + } else { + inode = erofs_new_inode(sbi); + if (IS_ERR(inode)) { + ret = PTR_ERR(inode); + goto err_iter; + } + + inode->i_mode = S_IFREG | 0644; + inode->i_parent = d->inode; + inode->i_nlink = 1; + + d->inode = inode; + d->type = EROFS_FT_REG_FILE; + } + inode->i_srcpath = strdup(obj->key); + if (!inode->i_srcpath) { + ret = -ENOMEM; + goto err_iter; + } + + trimmed = erofs_trim_for_progressinfo(inode->i_srcpath, + sizeof("Importing ...") - 1); + erofs_update_progressinfo("Importing %s ...", trimmed); + free(trimmed); + + st.st_mtime = obj->mtime; + ST_MTIM_NSEC_SET(&st, obj->mtime_ns); + ret = __erofs_fill_inode(inode, &st, obj->key); + if (!ret && S_ISREG(inode->i_mode)) { + inode->i_size = obj->size; + ret = erofs_write_zero_inode(inode); + } + if (ret) + goto err_iter; + } + +err_iter: + s3erofs_destroy_object_iterator(iter); +err_global: + s3erofs_global_exit(); + return ret; +}mkfs/main.c +0
-0mkfs/main.c int main(int argc, char **argv) · line 1741
Quoted 7 lines
mkfs/main.c +2
-2@@ -1737,8 +1737,14 @@ int main(int argc, char **argv) goto exit; #ifdef S3EROFS_ENABLED } else if (source_mode == EROFS_MKFS_SOURCE_S3) { - err = -EOPNOTSUPP; - goto exit; + if (incremental_mode || + dataimport_mode != EROFS_MKFS_DATA_IMPORT_ZEROFILL)Did you forget setting
dataimport_modeasEROFS_MKFS_DATA_IMPORT_ZEROFILL?mkfs/main.c +1
-0@@ -1220,6 +1220,7 @@ static int mkfs_parse_options_cfg(int argc, char *argv[]) err = mkfs_parse_s3_cfg(optarg); if (err) return err; + dataimport_mode = EROFS_MKFS_DATA_IMPORT_ZEROFILL; break;
Quoted 3 lines
#endif
case 'V':
We'd better use the current s3 support with --clean=0 parameter, because
meta-only image building support is not a default choice.
Thanks,
Yifan
Quoted 12 lines
Thanks,
HongboQuoted 8 lines
mkfs/main.c +6
-0+ err = -EOPNOTSUPP; + else + err = s3erofs_build_trees(root, &s3cfg, + cfg.c_src_path); + if (err) + goto exit; #endif }
Re: [PATCH v7 4/4] erofs-utils: mkfs: support EROFS meta-only image generation from S3
In reply to Gao XiangQuoted 12 lines · Gao Xiang
From: Yifan Zhao <[email protected]>
This patch introduces experimental S3 support for mkfs.erofs, allowing EROFS images to be generated from AWS S3 and/or other S3 API-compatible services.
Here are the current limitations:
- Only meta-only EROFS image generation is supported;
- Only AWS Signature Version 2 is supported;
- The S3 object names and sizes are strictly respected during image generation.
Quoted 4 lines
Co-developed-by Hongbo Li <[email protected]>Signed-off-by Hongbo Li <[email protected]>Signed-off-by Yifan Zhao <[email protected]>Signed-off-by Gao Xiang <[email protected]>
Thanks,
Hongbo
Quoted 737 lines
---
v7:
- FixNextMarkeromission on the standard AWS S3;
- Addprefixsupport for S3 ListObjects.lib/Makefile.am | 3 + lib/liberofs_s3.h | 3 + lib/remotes/s3.c | 670 ++++++++++++++++++++++++++++++++++++++++++++++ mkfs/main.c | 10 +- 4 files changed, 684 insertions(+), 2 deletions(-)create mode 100644
lib/remotes/s3.clib/Makefile.am +3
-0@@ -67,6 +67,9 @@ else liberofs_la_SOURCES += xxhash.c endif liberofs_la_CFLAGS += ${libcurl_CFLAGS} ${openssl_CFLAGS} ${libxml2_CFLAGS} +if ENABLE_S3 +liberofs_la_SOURCES += remotes/s3.c +endif if ENABLE_EROFS_MT liberofs_la_LDFLAGS = -lpthread liberofs_la_SOURCES += workqueue.clib/liberofs_s3.h +3
-0@@ -33,6 +33,9 @@ struct erofs_s3 {enum s3erofs_signature_version sig; }; +int s3erofs_build_trees (struct erofs_inode *root,struct erofs_s3 *s3, +const char *path); + #ifdef __cplusplus }#endif lib/remotes/s3.c new file +670
-0@@ -0,0 +1,670 @@ +// SPDX-License-Identifier: GPL-2.0+ OR Apache-2.0 +/* +* Copyright (C) 2025 HUAWEI, Inc. +* http://www.huawei.com/ +* Created by Yifan Zhao <[email protected]> +*/ +#define _GNU_SOURCE +#include <stdio.h> +#include <stdlib.h> +#include <time.h> +#include <curl/curl.h> +#include <libxml/parser.h> +#include <openssl/hmac.h> +#include "erofs/internal.h" +#include "erofs/print.h" +#include "erofs/inode.h" +#include "erofs/blobchunk.h" +#include "erofs/rebuild.h" +#include "liberofs_s3.h" + +#define S3EROFS_PATH_MAX 1024 +#define S3EROFS_MAX_QUERY_PARAMS 16 +#define S3EROFS_URL_LEN 8192 +#define S3EROFS_CANONICAL_QUERY_LEN 2048 + +#define BASE64_ENCODE_LEN (len) (((len + 2) / 3) * 4) + +static CURL *easy_curl; + +struct s3erofs_query_params { +int num; +const char *key[S3EROFS_MAX_QUERY_PARAMS ]; +const char *value[S3EROFS_MAX_QUERY_PARAMS ]; +}; + +struct s3erofs_curl_request { +const char *method; +char url[S3EROFS_URL_LEN ]; +char canonical_query[S3EROFS_CANONICAL_QUERY_LEN ]; +}; + +static int s3erofs_prepare_url (struct s3erofs_curl_request *req, +const char *endpoint, +const char *path, +struct s3erofs_query_params *params, +enum s3erofs_url_style url_style) +{ +static const char https[] ="https://" ; +const char *schema, *host; +bool slash = false; +char *url = req->url; +int pos, i; + +if (!endpoint || !path) +return -EINVAL ; + + schema =strstr (endpoint,"://" ); +if (!schema) { + schema = https; + host = endpoint; + }else { + host = schema +sizeof ("://" ) -1 ; + schema =strndup (endpoint, host - endpoint); +if (!schema) +return -ENOMEM ; + } + +if (url_style ==S3EROFS_URL_STYLE_PATH ) { + pos =snprintf (url,S3EROFS_URL_LEN ,"%s%s/%s" , schema, + host, path); + }else { +const char * split =strchr (path,'/' ); + +if (!split) { + pos =snprintf (url,S3EROFS_URL_LEN ,"%s%s.%s/" , + schema, path, host); + slash = true; + }else { + pos =snprintf (url,S3EROFS_URL_LEN ,"%s%.*s.%s%s" , + schema, (int )(split - path), path, + host, split); + } + } + i =snprintf (req->canonical_query,S3EROFS_CANONICAL_QUERY_LEN , +"/%s%s" , path, slash ?"/" :"" ); + req->canonical_query[i] ='\0' ; + +for (i =0 ; i < params->num; i++) + pos +=snprintf (url + pos,S3EROFS_URL_LEN - pos,"%c%s=%s" , + (!i ?'?' :'&' ), + params->key[i], params->value[i]); +if (schema != https) +free ((void *)schema); +return 0 ; +} + +static char *get_canonical_headers (const struct curl_slist *list) {return "" ; } + +// See: https://docs.aws.amazon.com/AmazonS3/latest/API/RESTAuthentication.html#ConstructingTheAuthenticationHeader +static char *s3erofs_sigv2_header (const struct curl_slist *headers, +const char *method,const char *content_md5, +const char *content_type,const char *date, +const char *canonical_query,const char *ak,const char *sk) +{ +u8 hmac_signature[EVP_MAX_MD_SIZE ]; +char *str, *output =NULL ; +unsigned int len, pos, output_len; +const char *canonical_headers =get_canonical_headers (headers); +const char *prefix ="Authorization: AWS " ; + +if (!method || !date || !ak || !sk) +return ERR_PTR (-EINVAL ); + +if (!content_md5) + content_md5 ="" ; +if (!content_type) + content_type ="" ; +if (!canonical_query) + canonical_query ="/" ; + + pos =asprintf (&str,"%s\n%s\n%s\n%s\n%s%s" , method, content_md5, + content_type, date, canonical_headers, canonical_query); +if (pos <0 ) +return ERR_PTR (-ENOMEM ); + +if (!HMAC (EVP_sha1 (), sk,strlen (sk), (u8 *)str,strlen (str), hmac_signature, &len)) + goto free_string; + + output_len =BASE64_ENCODE_LEN (len); + output_len +=strlen (prefix); + output_len +=strlen (ak); + output_len +=1 ;/* for ':' between ak and signature */ + + output = (char *)malloc (output_len +1 ); +if (!output) + goto free_string; + + pos =snprintf (output, output_len,"%s%s:" , prefix, ak); +if (pos <0 ) + goto free_string; +EVP_EncodeBlock ((u8 *)output + pos, hmac_signature, len); +free_string: +free (str); +return output ?:ERR_PTR (-ENOMEM ); +} + +static void s3erofs_now_rfc1123 (char *buf,size_t maxlen) +{ +time_t now =time (NULL ); +struct tm *ptm =gmtime (&now); + +strftime (buf, maxlen,"%a, %d %b %Y %H:%M:%S GMT" , ptm); +} + +struct s3erofs_curl_response { +char *data; +size_t size; +}; + +static size_t s3erofs_request_write_memory_cb (void *contents,size_t size, +size_t nmemb,void *userp) +{ +size_t realsize = size * nmemb; +struct s3erofs_curl_response *response = userp; +void *tmp; + + tmp =realloc (response->data, response->size + realsize +1 ); +if (tmp ==NULL ) +return 0 ; + + response->data = tmp; + +memcpy (response->data + response->size, contents, realsize); + response->size += realsize; + response->data[response->size] ='\0' ; +return realsize; +} + +static int s3erofs_request_insert_auth (struct curl_slist **request_headers, +const char *method, +const char *canonical_query, +const char *ak,const char *sk) +{ +static const char date_prefix[] ="Date: " ; +char date[64 ], *sigv2; + +memcpy (date, date_prefix,sizeof (date_prefix) -1 ); +s3erofs_now_rfc1123 (date +sizeof (date_prefix) -1 , +sizeof (date) -sizeof (date_prefix) +1 ); + + sigv2 =s3erofs_sigv2_header (*request_headers, method,NULL ,NULL , + date +sizeof (date_prefix) -1 , + canonical_query, ak, sk); +if (IS_ERR (sigv2)) +return PTR_ERR (sigv2); + + *request_headers =curl_slist_append (*request_headers, date); + *request_headers =curl_slist_append (*request_headers, sigv2); + +free (sigv2); +return 0 ; +} + +static int s3erofs_request_perform (struct erofs_s3 *s3, +struct s3erofs_curl_request *req,void *resp) +{ +struct curl_slist *request_headers =NULL ; +long http_code =0 ; +int ret; + +if (s3->access_key[0 ]) { + ret =s3erofs_request_insert_auth (&request_headers, req->method, + req->canonical_query, + s3->access_key, s3->secret_key); +if (ret <0 ) { +erofs_err ("failed to insert auth headers" ); +return ret; + } + } + +curl_easy_setopt (easy_curl,CURLOPT_URL , req->url); +curl_easy_setopt (easy_curl,CURLOPT_WRITEDATA , resp); +curl_easy_setopt (easy_curl,CURLOPT_HTTPHEADER , request_headers); + + ret =curl_easy_perform (easy_curl); +if (ret !=CURLE_OK ) { +erofs_err ("curl_easy_perform() failed: %s" , +curl_easy_strerror (ret)); + ret = -EIO ; + goto err_header; + } + + ret =curl_easy_getinfo (easy_curl,CURLINFO_RESPONSE_CODE , &http_code); +if (ret !=CURLE_OK ) { +erofs_err ("curl_easy_getinfo() failed: %s" , +curl_easy_strerror (ret)); + ret = -EIO ; + goto err_header; + } + +if (!(http_code >=200 && http_code <300 )) { +erofs_err ("request failed with HTTP code %ld" , http_code); + ret = -EIO ; + } + +err_header: +curl_slist_free_all (request_headers); +return ret; +} + +struct s3erofs_object_info { +char *key; +u64 size; +time_t mtime; +u32 mtime_ns; +}; + +struct s3erofs_object_iterator { +struct erofs_s3 *s3; +struct s3erofs_object_info *objects; +int cur; + +char *bucket, *prefix; +const char *delimiter; + +char *next_marker; +bool is_truncated; +}; + +static int s3erofs_parse_list_objects_one (xmlNodePtr node, +struct s3erofs_object_info *info) +{ +xmlNodePtr child; +xmlChar *str; + +for (child = node->children; child; child = child->next) { +if (child->type ==XML_ELEMENT_NODE ) { + str =xmlNodeGetContent (child); +if (!str) +return -ENOMEM ; + +if (xmlStrEqual (child->name, (const xmlChar *)"LastModified" )) { +struct tm tm; +char *end; + + end =strptime ((char *)str,"%Y-%m-%dT%H:%M:%S" , &tm); +if (!end || (*end !='.' && *end !='Z' && *end !='\0' )) { +xmlFree (str); +return -EIO ; + } +if (*end =='.' ) { + info->mtime_ns =strtoul (end +1 , &end,10 ); +if (*end !='Z' && *end !='\0' ) { +xmlFree (str); +return -EIO ; + } + } + info->mtime =mktime (&tm); + } +if (xmlStrEqual (child->name, (const xmlChar *)"Key" )) + info->key =strdup ((char *)str); +else if (xmlStrEqual (child->name, (const xmlChar *)"Size" )) + info->size =atoll ((char *)str); +xmlFree (str); + } + } +return 0 ; +} + +static int s3erofs_parse_list_objects_result (const char *data,int len, +struct s3erofs_object_iterator *it) +{ +xmlNodePtr root =NULL , node, next; +int ret, i, contents_count; +xmlDocPtr doc =NULL ; +xmlChar *str; +void *tmp; + + doc =xmlReadMemory (data, len,NULL ,NULL ,0 ); +if (!doc) { +erofs_err ("failed to parse XML data" ); +return -EINVAL ; + } + + root =xmlDocGetRootElement (doc); +if (!root) { +erofs_err ("failed to get root element" ); + ret = -EINVAL ; + goto out; + } + +if (!xmlStrEqual (root->name, (const xmlChar *)"ListBucketResult" )) { +erofs_err ("invalid root element: expected ListBucketResult, got %s" , root->name); + ret = -EINVAL ; + goto out; + } + +// https://docs.aws.amazon.com/AmazonS3/latest/API/API_ListObjects.html#AmazonS3-ListObjects-response-NextMarker +free (it->next_marker); + it->next_marker =NULL ; + + contents_count =1 ; +for (node = root->children; node; node = next) { + next = node->next; +if (node->type ==XML_ELEMENT_NODE ) { +if (xmlStrEqual (node->name, (const xmlChar *)"Contents" )) { + ++contents_count; +continue ; + } +if (xmlStrEqual (node->name, (const xmlChar *)"IsTruncated" )) { + str =xmlNodeGetContent (node); +if (str) { + it->is_truncated = + !!xmlStrEqual (str, (const xmlChar *)"true" ); +xmlFree (str); + } + }else if (xmlStrEqual (node->name, (const xmlChar *)"NextMarker" )) { + str =xmlNodeGetContent (node); +if (str) { + it->next_marker =strdup ((char *)str); +xmlFree (str); +if (!it->next_marker) { + ret = -ENOMEM ; + goto out; + } + } + } +xmlUnlinkNode (node); + } +xmlUnlinkNode (node); +xmlFreeNode (node); + } + + i =0 ; +if (it->objects) { +for (; it->objects[i].key; ++i) { +free (it->objects[i].key); + it->objects[i].key =NULL ; + } + } + +if (i +1 < contents_count) { + tmp =malloc (contents_count *sizeof (*it->objects)); +if (!tmp) { + ret = -ENOMEM ; + goto out; + } +free (it->objects); + it->objects = tmp; + it->objects[0 ].key =NULL ; + } + it->cur =0 ; + + ret =0 ; +for (i =0 , node = root->children; node; node = node->next) { +if (__erofs_unlikely (i >= contents_count -1 )) { +DBG_BUGON (1 ); +continue ; + } + ret =s3erofs_parse_list_objects_one (node, &it->objects[i]); +if (ret <0 ) { +erofs_err ("failed to parse contents node %s: %s" , + (const char *)node->name,erofs_strerror (ret)); +break ; + } + it->objects[++i].key =NULL ; + } + +/* +* `NextMarker` is returned only if the `delimiter` request parameter +* is specified. +* +* If the response is truncated and does not include `NextMarker`, use +* the value of the last `Key` element in the response as the `marker` +* parameter in the next request. +*/ +if (!ret && i && it->is_truncated && !it->next_marker) { + it->next_marker =strdup (it->objects[i -1 ].key); +if (!it->next_marker) + ret = -ENOMEM ; + } + +if (!ret) + ret = i; +out: +xmlFreeDoc (doc); +return ret; +} + +static int s3erofs_list_objects (struct s3erofs_object_iterator *it) +{ +struct s3erofs_curl_request req = {}; +struct s3erofs_curl_response resp = {}; +struct s3erofs_query_params params; +struct erofs_s3 *s3 = it->s3; +int ret =0 ; + +if (it->delimiter &&strlen (it->delimiter) >S3EROFS_PATH_MAX ) { +erofs_err ("delimiter is too long" ); +return -EINVAL ; + } + + params.num =0 ; +if (it->prefix) { + params.key[params.num] ="prefix" ; + params.value[params.num] = it->prefix; + ++params.num; + } + +if (it->delimiter) { + params.key[params.num] ="delimiter" ; + params.value[params.num] = it->delimiter; + ++params.num; + } + +if (it->next_marker) { + params.key[params.num] ="marker" ; + params.value[params.num] = it->next_marker; + ++params.num; + } + + req.method ="GET" ; + ret =s3erofs_prepare_url (&req, s3->endpoint, it->bucket, + ¶ms, s3->url_style); +if (ret <0 ) +return ret; + + ret =s3erofs_request_perform (s3, &req, &resp); +if (ret <0 ) +return ret; + + ret =s3erofs_parse_list_objects_result (resp.data, resp.size, it); +if (ret <0 ) +return ret; +free (resp.data); +return 0 ; +} + +static struct s3erofs_object_iterator * +s3erofs_create_object_iterator (struct erofs_s3 *s3,const char *path, +const char *delimiter) +{ +struct s3erofs_object_iterator *iter; +char *prefix; + + iter =calloc (1 ,sizeof (struct s3erofs_object_iterator )); +if (!iter) +return ERR_PTR (-ENOMEM ); + iter->s3 = s3; + prefix =strchr (path,'/' ); +if (prefix) { +if (++prefix - path >S3EROFS_PATH_MAX ) +return ERR_PTR (-EINVAL ); + iter->bucket =strndup (path, prefix - path); + iter->prefix =strdup (prefix); + }else { + iter->bucket =strdup (path); + iter->prefix =NULL ; + } + iter->delimiter = delimiter; + iter->is_truncated = true; +return iter; +} + +static void s3erofs_destroy_object_iterator (struct s3erofs_object_iterator *it) +{ +int i; + +if (it->next_marker) +free (it->next_marker); +if (it->objects) { +for (i =0 ; it->objects[i].key; ++i) +free (it->objects[i].key); +free (it->objects); + } +free (it->prefix); +free (it->bucket); +free (it); +} + +static struct s3erofs_object_info * +s3erofs_get_next_object (struct s3erofs_object_iterator *it) +{ +int ret; + +if (it->objects && it->objects[it->cur].key) +return &it->objects[it->cur++]; + +if (it->is_truncated) { + ret =s3erofs_list_objects (it); +if (ret <0 ) +return ERR_PTR (ret); +return &it->objects[it->cur++]; + } +return NULL ; +} + +static int s3erofs_global_init (void ) +{ +if (curl_global_init (CURL_GLOBAL_DEFAULT ) !=CURLE_OK ) +return -EIO ; + + easy_curl =curl_easy_init (); +if (!easy_curl) + goto out_err; + +if (curl_easy_setopt (easy_curl,CURLOPT_WRITEFUNCTION , + s3erofs_request_write_memory_cb) !=CURLE_OK ) + goto out_err; + +if (curl_easy_setopt (easy_curl,CURLOPT_FOLLOWLOCATION ,1L ) !=CURLE_OK ) + goto out_err; + +if (curl_easy_setopt (easy_curl,CURLOPT_TIMEOUT ,30L ) !=CURLE_OK ) + goto out_err; + +if (curl_easy_setopt (easy_curl,CURLOPT_USERAGENT , +"s3erofs/" PACKAGE_VERSION ) !=CURLE_OK ) + goto out_err; + +xmlInitParser (); +return 0 ; +out_err: +curl_global_cleanup (); +return -EIO ; +} + +static void s3erofs_global_exit (void ) +{ +if (!easy_curl) +return ; + +xmlCleanupParser (); + +curl_easy_cleanup (easy_curl); + easy_curl =NULL ; + +curl_global_cleanup (); +} + +int s3erofs_build_trees (struct erofs_inode *root,struct erofs_s3 *s3, +const char *path) +{ +struct erofs_sb_info *sbi = root->sbi; +struct s3erofs_object_iterator *iter; +struct s3erofs_object_info *obj; +struct erofs_dentry *d; +struct erofs_inode *inode; +struct stat st; +char *trimmed; +bool dumb; +int ret; + + st.st_uid = root->i_uid; + st.st_gid = root->i_gid; + + ret =s3erofs_global_init (); +if (ret) { +erofs_err ("failed to initialize s3erofs: %s" ,erofs_strerror (ret)); +return ret; + } + + iter =s3erofs_create_object_iterator (s3, path,NULL ); +if (IS_ERR (iter)) { +erofs_err ("failed to create object iterator" ); + ret =PTR_ERR (iter); + goto err_global; + } + +while (1 ) { + obj =s3erofs_get_next_object (iter); +if (!obj) { +break ; + }else if (IS_ERR (obj)) { +erofs_err ("failed to get next object" ); + ret =PTR_ERR (obj); + goto err_iter; + } + + d =erofs_rebuild_get_dentry (root, obj->key, false, + &dumb, &dumb, false); +if (IS_ERR (d)) { + ret =PTR_ERR (d); + goto err_iter; + } +if (d->type ==EROFS_FT_DIR ) { + inode = d->inode; + inode->i_mode =S_IFDIR |0755 ; + }else { + inode =erofs_new_inode (sbi); +if (IS_ERR (inode)) { + ret =PTR_ERR (inode); + goto err_iter; + } + + inode->i_mode =S_IFREG |0644 ; + inode->i_parent = d->inode; + inode->i_nlink =1 ; + + d->inode = inode; + d->type =EROFS_FT_REG_FILE ; + } + inode->i_srcpath =strdup (obj->key); +if (!inode->i_srcpath) { + ret = -ENOMEM ; + goto err_iter; + } + + trimmed =erofs_trim_for_progressinfo (inode->i_srcpath, +sizeof ("Importing ..." ) -1 ); +erofs_update_progressinfo ("Importing %s ..." , trimmed); +free (trimmed); + + st.st_mtime = obj->mtime; +ST_MTIM_NSEC_SET (&st, obj->mtime_ns); + ret =__erofs_fill_inode (inode, &st, obj->key); +if (!ret &&S_ISREG (inode->i_mode)) { + inode->i_size = obj->size; + ret =erofs_write_zero_inode (inode); + } +if (ret) + goto err_iter; + } + +err_iter: +s3erofs_destroy_object_iterator (iter); +err_global: +s3erofs_global_exit (); +return ret; +}mkfs/main.c +8
-2@@ -1737,8 +1737,14 @@ int main(int argc, char **argv) goto exit; #ifdef S3EROFS_ENABLED }else if (source_mode ==EROFS_MKFS_SOURCE_S3 ) { - err = -EOPNOTSUPP ; - goto exit; +if (incremental_mode || + dataimport_mode !=EROFS_MKFS_DATA_IMPORT_ZEROFILL ) + err = -EOPNOTSUPP ; +else + err =s3erofs_build_trees (root, &s3cfg, + cfg.c_src_path); +if (err) + goto exit;#endif }