]> www.infradead.org Git - mtd-utils.git/commitdiff
ubifs-utils: Decouple mkfs.ubifs.h out of other modules
authorZhihao Cheng <chengzhihao1@huawei.com>
Mon, 11 Nov 2024 08:36:33 +0000 (16:36 +0800)
committerDavid Oberhollenzer <david.oberhollenzer@sigma-star.at>
Mon, 11 Nov 2024 09:32:45 +0000 (10:32 +0100)
Header file mkfs.ubifs.h is included in other modules(eg. compr.c, lpt.c,
fscrypt.h, sign.c), decouple it out of other modules.

There are two parts in mkfs.ubifs.h:
1. common functions, for example dbg_msg, err_msg and write_leb, move
   these functions into common/defs.h and common/ubifs.h.
2. devtable related definations, move them into a new header file
   common/devtable.h.

Splitting common functions from mkfs.ubifs.h is also a preparation for
importing libubifs(from linux kernel) to replace current UBIFS libs.

Signed-off-by: Zhihao Cheng <chengzhihao1@huawei.com>
Signed-off-by: David Oberhollenzer <david.oberhollenzer@sigma-star.at>
14 files changed:
ubifs-utils/Makemodule.am
ubifs-utils/common/compr.c
ubifs-utils/common/compr.h
ubifs-utils/common/crypto.c
ubifs-utils/common/defs.h
ubifs-utils/common/devtable.c
ubifs-utils/common/devtable.h [moved from ubifs-utils/mkfs.ubifs/mkfs.ubifs.h with 53% similarity]
ubifs-utils/common/fscrypt.c
ubifs-utils/common/fscrypt.h
ubifs-utils/common/lpt.c
ubifs-utils/common/lpt.h
ubifs-utils/common/sign.c
ubifs-utils/common/ubifs.h
ubifs-utils/mkfs.ubifs/mkfs.ubifs.c

index 4a617c19cf196e64fdf4790c50ab687f7a22f7ba..8af65be759f65071cc26eae88289202649a995eb 100644 (file)
@@ -9,6 +9,7 @@ common_SOURCES = \
        ubifs-utils/common/hashtable/hashtable_private.h \
        ubifs-utils/common/hashtable/hashtable.c \
        ubifs-utils/common/hashtable/hashtable_itr.c \
+       ubifs-utils/common/devtable.h \
        ubifs-utils/common/devtable.c \
        ubifs-utils/common/ubifs.h \
        ubifs-utils/common/key.h \
@@ -27,12 +28,11 @@ endif
 
 mkfs_ubifs_SOURCES = \
        $(common_SOURCES) \
-       ubifs-utils/mkfs.ubifs/mkfs.ubifs.h \
        ubifs-utils/mkfs.ubifs/mkfs.ubifs.c
 
 mkfs_ubifs_LDADD = libmtd.a libubi.a $(ZLIB_LIBS) $(LZO_LIBS) $(ZSTD_LIBS) $(UUID_LIBS) $(LIBSELINUX_LIBS) $(OPENSSL_LIBS) -lm
 mkfs_ubifs_CPPFLAGS = $(AM_CPPFLAGS) $(ZLIB_CFLAGS) $(LZO_CFLAGS) $(ZSTD_CFLAGS) $(UUID_CFLAGS) $(LIBSELINUX_CFLAGS) \
-       -I$(top_srcdir)/ubi-utils/include -I$(top_srcdir)/ubifs-utils/mkfs.ubifs/ -I$(top_srcdir)/ubifs-utils/common
+       -I$(top_srcdir)/ubi-utils/include -I$(top_srcdir)/ubifs-utils/common
 
 EXTRA_DIST += ubifs-utils/common/README
 
index e4324f34aba031f5f4bd66c86f5d5664a92e8e96..6f901511831265172f97866b5e7115bc629bd11c 100644 (file)
 #endif
 
 #include "compr.h"
-#include "mkfs.ubifs.h"
+#include "ubifs.h"
 
 static void *lzo_mem;
 static unsigned long long errcnt = 0;
 #ifdef WITH_LZO
+extern struct ubifs_info info_;
 static struct ubifs_info *c = &info_;
 #endif
 
@@ -181,12 +182,12 @@ static int favor_lzo_compress(void *in_buf, size_t in_len, void *out_buf,
 
 select_lzo:
        *out_len = lzo_len;
-       *type = MKFS_UBIFS_COMPR_LZO;
+       *type = UBIFS_COMPR_LZO;
        return 0;
 
 select_zlib:
        *out_len = zlib_len;
-       *type = MKFS_UBIFS_COMPR_ZLIB;
+       *type = UBIFS_COMPR_ZLIB;
        memcpy(out_buf, zlib_buf, zlib_len);
        return 0;
 }
@@ -199,7 +200,7 @@ int compress_data(void *in_buf, size_t in_len, void *out_buf, size_t *out_len,
 
        if (in_len < UBIFS_MIN_COMPR_LEN) {
                no_compress(in_buf, in_len, out_buf, out_len);
-               return MKFS_UBIFS_COMPR_NONE;
+               return UBIFS_COMPR_NONE;
        }
 
 #if defined(WITH_LZO) && defined(WITH_ZLIB)
@@ -211,21 +212,21 @@ int compress_data(void *in_buf, size_t in_len, void *out_buf, size_t *out_len,
 #endif
                switch (type) {
 #ifdef WITH_LZO
-               case MKFS_UBIFS_COMPR_LZO:
+               case UBIFS_COMPR_LZO:
                        ret = lzo_compress(in_buf, in_len, out_buf, out_len);
                        break;
 #endif
 #ifdef WITH_ZLIB
-               case MKFS_UBIFS_COMPR_ZLIB:
+               case UBIFS_COMPR_ZLIB:
                        ret = zlib_deflate(in_buf, in_len, out_buf, out_len);
                        break;
 #endif
 #ifdef WITH_ZSTD
-               case MKFS_UBIFS_COMPR_ZSTD:
+               case UBIFS_COMPR_ZSTD:
                        ret = zstd_compress(in_buf, in_len, out_buf, out_len);
                        break;
 #endif
-               case MKFS_UBIFS_COMPR_NONE:
+               case UBIFS_COMPR_NONE:
                        ret = 1;
                        break;
                default:
@@ -236,7 +237,7 @@ int compress_data(void *in_buf, size_t in_len, void *out_buf, size_t *out_len,
        }
        if (ret || *out_len >= in_len) {
                no_compress(in_buf, in_len, out_buf, out_len);
-               return MKFS_UBIFS_COMPR_NONE;
+               return UBIFS_COMPR_NONE;
        }
        return type;
 }
index d58c7c7bd313f227b05777adae31981a20a2acfa..3e8e9b6116b227c52502013299e53740c4fbaa10 100644 (file)
  */
 #define WORST_COMPR_FACTOR 4
 
-enum compression_type
-{
-       MKFS_UBIFS_COMPR_NONE,
-       MKFS_UBIFS_COMPR_LZO,
-       MKFS_UBIFS_COMPR_ZLIB,
-       MKFS_UBIFS_COMPR_ZSTD,
-};
-
 int compress_data(void *in_buf, size_t in_len, void *out_buf, size_t *out_len,
                  int type);
 int init_compression(void);
index 19c445e82b8db930f6ed9f83ef2cf25056e05e20..60a67a4eaef629d04f234214964a1e77dbcac8ad 100644 (file)
  * Authors: David Oberhollenzer <david.oberhollenzer@sigma-star.at>
  */
 
-#define PROGRAM_NAME "mkfs.ubifs"
 #include <openssl/evp.h>
 #include <openssl/err.h>
+#include <openssl/rand.h>
 #include <string.h>
 #include <assert.h>
 
-#include "fscrypt.h"
+#define PROGRAM_NAME "mkfs.ubifs"
 #include "common.h"
+#include "defs.h"
+#include "fscrypt.h"
 
 static int do_hash(const EVP_MD *md, const unsigned char *in, size_t len, unsigned char *out)
 {
index 8db52776cd85f241c6dd26efa98b7ffead407f7c..e1aded0326f95c7d36dbf64c3f6955236d43e8b9 100644 (file)
@@ -6,6 +6,30 @@
 #ifndef __UBIFS_DEFS_H__
 #define __UBIFS_DEFS_H__
 
+#include <stdlib.h>
+#include <stdio.h>
+#include <limits.h>
+#include <byteswap.h>
+#include <errno.h>
+
+extern int debug_level;
+
+#define dbg_msg(lvl, fmt, ...) do {if (debug_level >= lvl)                \
+       printf("mkfs.ubifs: %s: " fmt "\n", __FUNCTION__, ##__VA_ARGS__); \
+} while(0)
+
+#define err_msg(fmt, ...) ({                                \
+       fprintf(stderr, "Error: " fmt "\n", ##__VA_ARGS__); \
+       -1;                                                 \
+})
+
+#define sys_err_msg(fmt, ...) ({                                         \
+       int err_ = errno;                                                \
+       fprintf(stderr, "Error: " fmt "\n", ##__VA_ARGS__);              \
+       fprintf(stderr, "       %s (error %d)\n", strerror(err_), err_); \
+       -1;                                                              \
+})
+
 #define t16(x) ({ \
        uint16_t __b = (x); \
        (__LITTLE_ENDIAN==__BYTE_ORDER) ? __b : bswap_16(__b); \
index aa815fbb094e17c71cc1243cf5ac419543f4a1e4..2b9c6ef20ccacede7874a0005d7f533339fa1181 100644 (file)
  * for more information about what the device table is.
  */
 
-#include "mkfs.ubifs.h"
+#include <string.h>
+#include <ctype.h>
+#include <sys/stat.h>
+#include <sys/types.h>
+#include <sys/sysmacros.h>
+
+#include "devtable.h"
+#include "defs.h"
 #include "hashtable/hashtable.h"
 #include "hashtable/hashtable_itr.h"
 
similarity index 53%
rename from ubifs-utils/mkfs.ubifs/mkfs.ubifs.h
rename to ubifs-utils/common/devtable.h
index 569098425f136341730995d6ee0c2243a2db2386..97585f2b20d50a5f3ad8774103c1396a81cbb640 100644 (file)
  *          Zoltan Sogor
  */
 
-#ifndef __MKFS_UBIFS_H__
-#define __MKFS_UBIFS_H__
-
-#include <unistd.h>
-#include <stdlib.h>
-#include <stdio.h>
-#include <limits.h>
-#include <string.h>
-#include <stdint.h>
-#include <endian.h>
-#include <byteswap.h>
-#include <linux/types.h>
-#include <linux/fs.h>
-
-#include <getopt.h>
-#include <sys/types.h>
-#include <sys/stat.h>
-#include <sys/ioctl.h>
-#include <fcntl.h>
-#include <dirent.h>
-#include <errno.h>
-#include <libgen.h>
-#include <ctype.h>
-#include <uuid.h>
-#include <sys/file.h>
-
-#ifdef WITH_CRYPTO
-#include <openssl/rand.h>
-#endif
-
-#include <mtd/ubifs-media.h>
-
-/* common.h requires the PROGRAM_NAME macro */
-#define PROGRAM_NAME "mkfs.ubifs"
-#include "common.h"
-
-#include "libubi.h"
-#include "defs.h"
-#include "crc16.h"
-#include "ubifs.h"
-#include "key.h"
-#include "lpt.h"
-#include "compr.h"
-#include "sign.h"
-
-/*
- * Compression flags are duplicated so that compr.c can compile without ubifs.h.
- * Here we make sure they are the same.
- */
-#if MKFS_UBIFS_COMPR_NONE != UBIFS_COMPR_NONE
-#error MKFS_UBIFS_COMPR_NONE != UBIFS_COMPR_NONE
-#endif
-#if MKFS_UBIFS_COMPR_LZO != UBIFS_COMPR_LZO
-#error MKFS_UBIFS_COMPR_LZO != UBIFS_COMPR_LZO
-#endif
-#if MKFS_UBIFS_COMPR_ZLIB != UBIFS_COMPR_ZLIB
-#error MKFS_UBIFS_COMPR_ZLIB != UBIFS_COMPR_ZLIB
-#endif
-#if MKFS_UBIFS_COMPR_ZSTD != UBIFS_COMPR_ZSTD
-#error MKFS_UBIFS_COMPR_ZSTD != UBIFS_COMPR_ZSTD
-#endif
-
-extern int verbose;
-extern int debug_level;
-
-#define dbg_msg(lvl, fmt, ...) do {if (debug_level >= lvl)                \
-       printf("mkfs.ubifs: %s: " fmt "\n", __FUNCTION__, ##__VA_ARGS__); \
-} while(0)
-
-#define err_msg(fmt, ...) ({                                \
-       fprintf(stderr, "Error: " fmt "\n", ##__VA_ARGS__); \
-       -1;                                                 \
-})
-
-#define sys_err_msg(fmt, ...) ({                                         \
-       int err_ = errno;                                                \
-       fprintf(stderr, "Error: " fmt "\n", ##__VA_ARGS__);              \
-       fprintf(stderr, "       %s (error %d)\n", strerror(err_), err_); \
-       -1;                                                              \
-})
+#ifndef __DEVTABLE_H__
+#define __DEVTABLE_H__
 
 /**
  * struct path_htbl_element - an element of the path hash table.
@@ -136,11 +58,8 @@ struct name_htbl_element {
        dev_t dev;
 };
 
-extern struct ubifs_info info_;
-
 struct hashtable_itr;
 
-int write_leb(int lnum, int len, void *buf);
 int parse_devtable(const char *tbl_file);
 struct path_htbl_element *devtbl_find_path(const char *path);
 struct name_htbl_element *devtbl_find_name(struct path_htbl_element *ph_elt,
index b75bdf761c5bcf57169f67880ba63807fa3a940c..94c6c37757200c67289ae72607550d432163562b 100644 (file)
  *          David Oberhollenzer <david.oberhollenzer@sigma-star.at>
  */
 
+#include <endian.h>
+
 #define PROGRAM_NAME "mkfs.ubifs"
+#include "common.h"
 #include "fscrypt.h"
+#include "defs.h"
 
 
 static __u8 fscrypt_masterkey[FS_MAX_KEY_SIZE];
index ff3d326bb1acbbcee63ea001d90d59cc6404df6c..908a5041950e5fcab346afa48c3ad0a4cc3fabe4 100644 (file)
 #ifndef FSCRYPT_H
 #define FSCRYPT_H
 
-
-#include "mkfs.ubifs.h"
-#include <sys/types.h>
+#ifdef WITH_CRYPTO
+#include <openssl/rand.h>
+#endif
+#include <assert.h>
+#include "ubifs.h"
 #include "crypto.h"
 
 #ifndef FS_KEY_DESCRIPTOR_SIZE
index 7ee739a96fafb9361f9bde600c6b624478612b58..23ffe7f700776d4d5c1eb82b5815ed7bf13df0e9 100644 (file)
  *          Artem Bityutskiy
  */
 
-#include "mkfs.ubifs.h"
-
 #ifdef WITH_CRYPTO
 #include <openssl/evp.h>
 #endif
 
+#define PROGRAM_NAME "mkfs.ubifs"
+#include "common.h"
+#include "lpt.h"
+#include "defs.h"
+#include "ubifs.h"
+#include "crc16.h"
+#include "sign.h"
+
 /**
  * do_calc_lpt_geom - calculate sizes for the LPT area.
  * @c: the UBIFS file-system description object
index 4cde59d9ed49d0a5b7399d1b02f77ae121af3092..86148a2a3364a68830ae1f4d5375998877ec2a96 100644 (file)
@@ -22,6 +22,8 @@
 #ifndef __UBIFS_LPT_H__
 #define __UBIFS_LPT_H__
 
+#include "ubifs.h"
+
 int calc_dflt_lpt_geom(struct ubifs_info *c, int *main_lebs, int *big_lpt);
 int create_lpt(struct ubifs_info *c);
 
index 7f284f8e2c3aaac2b8d4e3c585977f6acfd5d72a..93399ff794a74c13092ec30a0b1425ff4567c41d 100644 (file)
@@ -17,9 +17,7 @@
  * Author: Sascha Hauer
  */
 
-#include "mkfs.ubifs.h"
-#include "common.h"
-
+#include <string.h>
 #include <openssl/evp.h>
 #include <openssl/opensslv.h>
 #include <openssl/bio.h>
 #include <openssl/conf.h>
 #include <err.h>
 
+#include "sign.h"
+#include "defs.h"
+#include "ubifs.h"
+
+extern struct ubifs_info info_;
 static struct ubifs_info *c = &info_;
 
 EVP_MD_CTX *hash_md;
index 55937cee783d60c0bbb5ea99624a04f88ad4bc39..0eef31acf8d479806226f6c9f56269d78d2063e5 100644 (file)
@@ -25,6 +25,9 @@
 #ifndef __UBIFS_H__
 #define __UBIFS_H__
 
+#include <mtd/ubifs-media.h>
+#include "libubi.h"
+
 /* Maximum logical eraseblock size in bytes */
 #define UBIFS_MAX_LEB_SZ (2*1024*1024)
 
@@ -468,4 +471,6 @@ struct ubifs_branch *ubifs_idx_branch(const struct ubifs_info *c,
                                       (UBIFS_BRANCH_SZ + c->key_len + c->hash_len) * bnum);
 }
 
+int write_leb(int lnum, int len, void *buf);
+
 #endif /* __UBIFS_H__ */
index f4cb51252e72e933f1dbe81e2a67ce086a61d1c8..0304ae70b69a3436cbe76e3112a6d928b70d0a45 100644 (file)
 
 #define _XOPEN_SOURCE 500 /* For realpath() */
 
-#include "mkfs.ubifs.h"
+#include <stdio.h>
+#include <stdlib.h>
+#include <libgen.h>
+#include <getopt.h>
+#include <dirent.h>
 #include <crc32.h>
-#include "common.h"
+#include <uuid.h>
+#include <linux/fs.h>
 #include <sys/types.h>
+#include <sys/stat.h>
+#include <sys/ioctl.h>
 #ifdef WITH_XATTR
 #include <sys/xattr.h>
 #endif
 #include <zstd.h>
 #endif
 
+/* common.h requires the PROGRAM_NAME macro */
+#define PROGRAM_NAME "mkfs.ubifs"
+#include "common.h"
+#include "defs.h"
 #include "crypto.h"
 #include "fscrypt.h"
+#include "ubifs.h"
+#include "lpt.h"
+#include "compr.h"
+#include "key.h"
+#include "sign.h"
+#include "devtable.h"
 
 /* Size (prime number) of hash table for link counting */
 #define HASH_TABLE_SIZE 10099