Skip to content

Commit

Permalink
erofs-utils: fix macOS build & functionality
Browse files Browse the repository at this point in the history
Tested on macOS Big Sur 11.4.

Link: https://lore.kernel.org/r/[email protected]
Signed-off-by: Gao Xiang <[email protected]>
  • Loading branch information
hsiangkao committed Jul 24, 2021
1 parent 60549d5 commit 95801d4
Show file tree
Hide file tree
Showing 13 changed files with 115 additions and 22 deletions.
3 changes: 2 additions & 1 deletion autogen.sh
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
aclocal && \
autoheader && \
autoconf && \
libtoolize && \
case `uname` in Darwin*) glibtoolize --copy ;; \
*) libtoolize --copy ;; esac && \
automake -a -c

17 changes: 16 additions & 1 deletion configure.ac
Original file line number Diff line number Diff line change
Expand Up @@ -128,6 +128,8 @@ AC_TYPE_INT64_T
AC_TYPE_SIZE_T
AC_TYPE_SSIZE_T
AC_CHECK_MEMBERS([struct stat.st_rdev])
AC_CHECK_MEMBERS([struct stat.st_atim])
AC_CHECK_MEMBERS([struct stat.st_atimensec])
AC_TYPE_UINT64_T

#
Expand All @@ -154,7 +156,20 @@ AC_CHECK_DECL(lseek64,[AC_DEFINE(HAVE_LSEEK64_PROTOTYPE, 1,
#include <unistd.h>])

# Checks for library functions.
AC_CHECK_FUNCS([backtrace fallocate gettimeofday memset realpath strdup strerror strrchr strtoull])
AC_CHECK_FUNCS(m4_flatten([
backtrace
fallocate
gettimeofday
lgetxattr
llistxattr
memset
realpath
pread64
pwrite64
strdup
strerror
strrchr
strtoull]))

# Configure debug mode
AS_IF([test "x$enable_debug" != "xno"], [], [
Expand Down
2 changes: 1 addition & 1 deletion fuse/dir.c
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
*/
#include <fuse.h>
#include <fuse_opt.h>

#include "macosx.h"
#include "erofs/internal.h"
#include "erofs/print.h"

Expand Down
3 changes: 3 additions & 0 deletions fuse/macosx.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
#ifdef __APPLE__
#undef LIST_HEAD
#endif
6 changes: 3 additions & 3 deletions fuse/main.c
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@
#include <libgen.h>
#include <fuse.h>
#include <fuse_opt.h>

#include "macosx.h"
#include "erofs/config.h"
#include "erofs/print.h"
#include "erofs/io.h"
Expand Down Expand Up @@ -74,10 +74,10 @@ static int erofsfuse_read(const char *path, char *buffer,
ret = erofs_pread(&vi, buffer, size, offset);
if (ret)
return ret;
if (offset + size > vi.i_size)
return vi.i_size - offset;
if (offset >= vi.i_size)
return 0;
if (offset + size > vi.i_size)
return vi.i_size - offset;
return size;
}

Expand Down
25 changes: 25 additions & 0 deletions include/erofs/defs.h
Original file line number Diff line number Diff line change
Expand Up @@ -96,13 +96,15 @@ typedef int64_t s64;
#define round_up(x, y) ((((x)-1) | __round_mask(x, y))+1)
#define round_down(x, y) ((x) & ~__round_mask(x, y))

#ifndef roundup
/* The `const' in roundup() prevents gcc-3.3 from calling __divdi3 */
#define roundup(x, y) ( \
{ \
const typeof(y) __y = y; \
(((x) + (__y - 1)) / __y) * __y; \
} \
)
#endif
#define rounddown(x, y) ( \
{ \
typeof(x) __x = (x); \
Expand Down Expand Up @@ -175,5 +177,28 @@ static inline u32 get_unaligned_le32(const u8 *p)
return p[0] | p[1] << 8 | p[2] << 16 | p[3] << 24;
}

#ifndef __always_inline
#define __always_inline inline
#endif

#ifdef HAVE_STRUCT_STAT_ST_ATIM
/* Linux */
#define ST_ATIM_NSEC(stbuf) ((stbuf)->st_atim.tv_nsec)
#define ST_CTIM_NSEC(stbuf) ((stbuf)->st_ctim.tv_nsec)
#define ST_MTIM_NSEC(stbuf) ((stbuf)->st_mtim.tv_nsec)
#elif defined(HAVE_STRUCT_STAT_ST_ATIMENSEC)
/* macOS */
#define ST_ATIM_NSEC(stbuf) ((stbuf)->st_atimensec)
#define ST_CTIM_NSEC(stbuf) ((stbuf)->st_ctimensec)
#define ST_MTIM_NSEC(stbuf) ((stbuf)->st_mtimensec)
#else
#define ST_ATIM_NSEC(stbuf) 0
#define ST_CTIM_NSEC(stbuf) 0
#define ST_MTIM_NSEC(stbuf) 0
#endif

#ifdef __APPLE__
#define stat64 stat
#define lstat64 lstat
#endif
#endif
6 changes: 4 additions & 2 deletions include/erofs/internal.h
Original file line number Diff line number Diff line change
Expand Up @@ -266,7 +266,9 @@ int z_erofs_fill_inode(struct erofs_inode *vi);
int z_erofs_map_blocks_iter(struct erofs_inode *vi,
struct erofs_map_blocks *map);

#ifdef EUCLEAN
#define EFSCORRUPTED EUCLEAN /* Filesystem is corrupted */

#else
#define EFSCORRUPTED EIO
#endif
#endif

26 changes: 19 additions & 7 deletions lib/data.c
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
* Copyright (C) 2020 Gao Xiang <[email protected]>
* Compression support by Huang Jianan <[email protected]>
*/
#include <stdlib.h>
#include "erofs/print.h"
#include "erofs/internal.h"
#include "erofs/io.h"
Expand Down Expand Up @@ -123,22 +124,23 @@ static int erofs_read_raw_data(struct erofs_inode *inode, char *buffer,
static int z_erofs_read_data(struct erofs_inode *inode, char *buffer,
erofs_off_t size, erofs_off_t offset)
{
int ret;
erofs_off_t end, length, skip;
struct erofs_map_blocks map = {
.index = UINT_MAX,
};
bool partial;
unsigned int algorithmformat;
char raw[Z_EROFS_PCLUSTER_MAX_SIZE];
unsigned int algorithmformat, bufsize;
char *raw = NULL;
int ret = 0;

end = offset + size;
bufsize = 0;
while (end > offset) {
map.m_la = end - 1;

ret = z_erofs_map_blocks_iter(inode, &map);
if (ret)
return ret;
break;

/*
* trim to the needed size if the returned extent is quite
Expand Down Expand Up @@ -167,9 +169,17 @@ static int z_erofs_read_data(struct erofs_inode *inode, char *buffer,
continue;
}

if (map.m_plen > bufsize) {
bufsize = map.m_plen;
raw = realloc(raw, bufsize);
if (!raw) {
ret = -ENOMEM;
break;
}
}
ret = dev_read(raw, map.m_pa, map.m_plen);
if (ret < 0)
return -EIO;
break;

algorithmformat = map.m_flags & EROFS_MAP_ZIPPED ?
Z_EROFS_COMPRESSION_LZ4 :
Expand All @@ -185,9 +195,11 @@ static int z_erofs_read_data(struct erofs_inode *inode, char *buffer,
.partial_decoding = partial
});
if (ret < 0)
return ret;
break;
}
return 0;
if (raw)
free(raw);
return ret < 0 ? ret : 0;
}

int erofs_pread(struct erofs_inode *inode, char *buf,
Expand Down
5 changes: 4 additions & 1 deletion lib/inode.c
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,10 @@
#include <stdlib.h>
#include <stdio.h>
#include <sys/stat.h>
#include <config.h>
#if defined(HAVE_SYS_SYSMACROS_H)
#include <sys/sysmacros.h>
#endif
#include <dirent.h>
#include "erofs/print.h"
#include "erofs/inode.h"
Expand Down Expand Up @@ -776,7 +779,7 @@ static int erofs_fill_inode(struct erofs_inode *inode,
inode->i_uid = cfg.c_uid == -1 ? st->st_uid : cfg.c_uid;
inode->i_gid = cfg.c_gid == -1 ? st->st_gid : cfg.c_gid;
inode->i_ctime = st->st_ctime;
inode->i_ctime_nsec = st->st_ctim.tv_nsec;
inode->i_ctime_nsec = ST_CTIM_NSEC(st);

switch (cfg.c_timeinherit) {
case TIMESTAMP_CLAMPING:
Expand Down
9 changes: 8 additions & 1 deletion lib/io.c
Original file line number Diff line number Diff line change
Expand Up @@ -148,7 +148,11 @@ int dev_write(const void *buf, u64 offset, size_t len)
return -EINVAL;
}

#ifdef HAVE_PWRITE64
ret = pwrite64(erofs_devfd, buf, len, (off64_t)offset);
#else
ret = pwrite(erofs_devfd, buf, len, (off_t)offset);
#endif
if (ret != (int)len) {
if (ret < 0) {
erofs_err("Failed to write data into device - %s:[%" PRIu64 ", %zd].",
Expand Down Expand Up @@ -245,8 +249,11 @@ int dev_read(void *buf, u64 offset, size_t len)
offset, len, erofs_devsz);
return -EINVAL;
}

#ifdef HAVE_PREAD64
ret = pread64(erofs_devfd, buf, len, (off64_t)offset);
#else
ret = pread(erofs_devfd, buf, len, (off_t)offset);
#endif
if (ret != (int)len) {
erofs_err("Failed to read data from device - %s:[%" PRIu64 ", %zd].",
erofs_devname, offset, len);
Expand Down
5 changes: 3 additions & 2 deletions lib/namei.c
Original file line number Diff line number Diff line change
Expand Up @@ -4,14 +4,15 @@
*
* Created by Li Guifu <[email protected]>
*/
#include <linux/kdev_t.h>
#include <sys/types.h>
#include <unistd.h>
#include <stdio.h>
#include <errno.h>
#include <sys/stat.h>
#include <config.h>
#if defined(HAVE_SYS_SYSMACROS_H)
#include <sys/sysmacros.h>

#endif
#include "erofs/print.h"
#include "erofs/io.h"

Expand Down
2 changes: 0 additions & 2 deletions lib/super.c
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,6 @@
*/
#include <string.h>
#include <stdlib.h>
#include <asm-generic/errno-base.h>

#include "erofs/io.h"
#include "erofs/print.h"

Expand Down
28 changes: 27 additions & 1 deletion lib/xattr.c
Original file line number Diff line number Diff line change
Expand Up @@ -159,7 +159,13 @@ static struct xattr_item *parse_one_xattr(const char *path, const char *key,
DBG_BUGON(keylen < prefixlen);

/* determine length of the value */
#ifdef HAVE_LGETXATTR
ret = lgetxattr(path, key, NULL, 0);
#elif defined(__APPLE__)
ret = getxattr(path, key, NULL, 0, 0, XATTR_NOFOLLOW);
#else
return ERR_PTR(-EOPNOTSUPP);
#endif
if (ret < 0)
return ERR_PTR(-errno);
len[1] = ret;
Expand All @@ -173,7 +179,15 @@ static struct xattr_item *parse_one_xattr(const char *path, const char *key,
memcpy(kvbuf, key + prefixlen, len[0]);
if (len[1]) {
/* copy value to buffer */
#ifdef HAVE_LGETXATTR
ret = lgetxattr(path, key, kvbuf + len[0], len[1]);
#elif defined(__APPLE__)
ret = getxattr(path, key, kvbuf + len[0], len[1], 0,
XATTR_NOFOLLOW);
#else
free(kvbuf);
return ERR_PTR(-EOPNOTSUPP);
#endif
if (ret < 0) {
free(kvbuf);
return ERR_PTR(-errno);
Expand Down Expand Up @@ -292,7 +306,13 @@ static bool erofs_is_skipped_xattr(const char *key)
static int read_xattrs_from_file(const char *path, mode_t mode,
struct list_head *ixattrs)
{
#ifdef HAVE_LLISTXATTR
ssize_t kllen = llistxattr(path, NULL, 0);
#elif defined(__APPLE__)
ssize_t kllen = listxattr(path, NULL, 0, XATTR_NOFOLLOW);
#else
ssize_t kllen = 0;
#endif
int ret;
char *keylst, *key, *klend;
unsigned int keylen;
Expand All @@ -313,13 +333,19 @@ static int read_xattrs_from_file(const char *path, mode_t mode,
return -ENOMEM;

/* copy the list of attribute keys to the buffer.*/
#ifdef HAVE_LLISTXATTR
kllen = llistxattr(path, keylst, kllen);
#elif defined(__APPLE__)
kllen = listxattr(path, keylst, kllen, XATTR_NOFOLLOW);
if (kllen < 0) {
erofs_err("llistxattr to get names for %s failed", path);
ret = -errno;
goto err;
}

#else
ret = -EOPNOTSUPP;
goto err;
#endif
/*
* loop over the list of zero terminated strings with the
* attribute keys. Use the remaining buffer length to determine
Expand Down

0 comments on commit 95801d4

Please sign in to comment.