code: add clang-format

add a clang-format definition file and apply it to the code.

Signed-off-by: Giuseppe Scrivano <gscrivan@redhat.com>
This commit is contained in:
Giuseppe Scrivano 2023-10-02 15:12:36 +02:00
parent ff6b14d0a8
commit ba472b58bc
No known key found for this signature in database
GPG Key ID: 67E38F7A8BA21772
12 changed files with 564 additions and 454 deletions

117
.clang-format Normal file
View File

@ -0,0 +1,117 @@
---
BasedOnStyle: GNU
AccessModifierOffset: -2
AlignAfterOpenBracket: Align
AlignConsecutiveMacros: false
AlignConsecutiveAssignments: false
AlignConsecutiveDeclarations: false
AlignEscapedNewlines: Left
AlignOperands: true
AlignTrailingComments: true
AllowAllArgumentsOnNextLine: true
AllowAllConstructorInitializersOnNextLine: true
AllowAllParametersOfDeclarationOnNextLine: true
AllowShortBlocksOnASingleLine: Never
AllowShortCaseLabelsOnASingleLine: false
AllowShortFunctionsOnASingleLine: All
AllowShortLambdasOnASingleLine: All
AllowShortIfStatementsOnASingleLine: Never
AllowShortLoopsOnASingleLine: false
AlwaysBreakAfterDefinitionReturnType: All
AlwaysBreakAfterReturnType: AllDefinitions
AlwaysBreakBeforeMultilineStrings: false
AlwaysBreakTemplateDeclarations: MultiLine
BinPackArguments: true
BinPackParameters: true
AllowShortIfStatementsOnASingleLine: false
AllowShortLoopsOnASingleLine: false
AllowShortCaseLabelsOnASingleLine: false
AllowShortFunctionsOnASingleLine: false
AllowShortBlocksOnASingleLine: false
BreakBeforeBraces: Allman
BraceWrapping:
AfterCaseLabel: true
AfterClass: true
AfterControlStatement: true
AfterEnum: true
AfterFunction: true
AfterNamespace: true
AfterObjCDeclaration: true
AfterStruct: true
AfterUnion: true
BeforeCatch: true
BeforeElse: true
IndentBraces: true
SplitEmptyFunction: true
SplitEmptyRecord: true
SplitEmptyNamespace: true
BreakBeforeBinaryOperators: All
BreakBeforeBraces: Custom
BreakBeforeInheritanceComma: false
BreakInheritanceList: BeforeColon
BreakBeforeTernaryOperators: true
BreakConstructorInitializersBeforeComma: false
BreakConstructorInitializers: BeforeColon
BreakAfterJavaFieldAnnotations: false
BreakStringLiterals: true
ColumnLimit: 0
ContinuationIndentWidth: 4
DeriveLineEnding: true
DerivePointerAlignment: false
DisableFormat: false
ExperimentalAutoDetectBinPacking: false
FixNamespaceComments: false
ForEachMacros:
- foreach
- Q_FOREACH
- BOOST_FOREACH
IncludeBlocks: Preserve
IncludeIsMainRegex: '(Test)?$'
IncludeIsMainSourceRegex: ''
IndentCaseLabels: false
IndentGotoLabels: true
IndentPPDirectives: AfterHash
IndentWidth: 2
IndentWrappedFunctionNames: false
JavaScriptQuotes: Leave
JavaScriptWrapImports: true
KeepEmptyLinesAtTheStartOfBlocks: true
MacroBlockBegin: ''
MacroBlockEnd: ''
MaxEmptyLinesToKeep: 1
NamespaceIndentation: None
ObjCBinPackProtocolList: Auto
ObjCBlockIndentWidth: 2
ObjCSpaceAfterProperty: false
ObjCSpaceBeforeProtocolList: true
PenaltyBreakAssignment: 2
PenaltyBreakBeforeFirstCallParameter: 19
PenaltyBreakComment: 300
PenaltyBreakFirstLessLess: 120
PenaltyBreakString: 1000
PenaltyBreakTemplateDeclaration: 10
PenaltyExcessCharacter: 1000000
PenaltyReturnTypeOnItsOwnLine: 60
PointerAlignment: Right
ReflowComments: true
SortIncludes: false
SortUsingDeclarations: true
SpaceAfterCStyleCast: true
SpaceAfterLogicalNot: true
SpaceAfterTemplateKeyword: true
SpaceBeforeAssignmentOperators: true
SpaceBeforeParens: Always
SpaceInEmptyBlock: false
SpaceInEmptyParentheses: false
SpacesBeforeTrailingComments: 1
SpacesInAngles: false
SpacesInConditionalStatement: false
SpacesInContainerLiterals: true
SpacesInCStyleCastParentheses: false
SpacesInParentheses: false
SpacesInSquareBrackets: false
SpaceBeforeSquareBrackets: false
TabWidth: 8
UseCRLF: false
UseTab: Never
...

View File

@ -36,3 +36,6 @@ srpm: dist-gzip fuse-overlayfs.spec
echo $(VERSION)
$(MAKE) -C $(WD) dist-xz
rpmbuild -bs --define "_sourcedir $(WD)" --define "_specdir $(WD)" --define "_builddir $(WD)" --define "_srcrpmdir $(WD)" --define "_rpmdir $(WD)" --define "_buildrootdir $(WD)/.build" fuse-overlayfs.spec
clang-format:
git ls-files | grep -E "\\.[hc]$$" | grep -v "^lib/" | xargs clang-format -style=file -i

View File

@ -76,7 +76,7 @@ direct_fstat (struct ovl_layer *l, int fd, const char *path, unsigned int mask,
#ifdef HAVE_STATX
struct statx stx;
ret = statx (fd, "", AT_STATX_DONT_SYNC|AT_EMPTY_PATH, mask, &stx);
ret = statx (fd, "", AT_STATX_DONT_SYNC | AT_EMPTY_PATH, mask, &stx);
if (ret < 0 && (errno == ENOSYS || errno == EINVAL))
goto fallback;
if (ret == 0)
@ -88,7 +88,7 @@ direct_fstat (struct ovl_layer *l, int fd, const char *path, unsigned int mask,
return ret;
#endif
fallback:
fallback:
ret = fstat (fd, st);
if (ret != 0)
return ret;
@ -103,7 +103,7 @@ direct_statat (struct ovl_layer *l, const char *path, struct stat *st, int flags
#ifdef HAVE_STATX
struct statx stx;
ret = statx (l->fd, path, AT_STATX_DONT_SYNC|flags, mask, &stx);
ret = statx (l->fd, path, AT_STATX_DONT_SYNC | flags, mask, &stx);
if (ret < 0 && (errno == ENOSYS || errno == EINVAL))
goto fallback;
if (ret == 0)
@ -114,7 +114,7 @@ direct_statat (struct ovl_layer *l, const char *path, struct stat *st, int flags
return ret;
#endif
fallback:
fallback:
ret = fstatat (l->fd, path, st, flags);
if (ret != 0)
return ret;
@ -212,24 +212,22 @@ direct_support_acls (struct ovl_layer *l)
char value[32];
return fgetxattr (l->fd, ACL_XATTR, value, sizeof (value)) >= 0
|| errno != ENOTSUP;
|| errno != ENOTSUP;
}
struct data_source direct_access_ds =
{
.num_of_layers = direct_num_of_layers,
.load_data_source = direct_load_data_source,
.cleanup = direct_cleanup,
.file_exists = direct_file_exists,
.statat = direct_statat,
.fstat = direct_fstat,
.opendir = direct_opendir,
.readdir = direct_readdir,
.closedir = direct_closedir,
.openat = direct_openat,
.getxattr = direct_getxattr,
.listxattr = direct_listxattr,
.readlinkat = direct_readlinkat,
.support_acls = direct_support_acls,
};
struct data_source direct_access_ds = {
.num_of_layers = direct_num_of_layers,
.load_data_source = direct_load_data_source,
.cleanup = direct_cleanup,
.file_exists = direct_file_exists,
.statat = direct_statat,
.fstat = direct_fstat,
.opendir = direct_opendir,
.readdir = direct_readdir,
.closedir = direct_closedir,
.openat = direct_openat,
.getxattr = direct_getxattr,
.listxattr = direct_listxattr,
.readlinkat = direct_readlinkat,
.support_acls = direct_support_acls,
};

View File

@ -16,15 +16,15 @@
along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
#ifndef FUSE_OVERLAYFS_H
# define FUSE_OVERLAYFS_H
# define _GNU_SOURCE
#define FUSE_OVERLAYFS_H
#define _GNU_SOURCE
# include <sys/stat.h>
# include <plugin-manager.h>
# include <stdbool.h>
# include <sys/types.h>
#include <sys/stat.h>
#include <plugin-manager.h>
#include <stdbool.h>
#include <sys/types.h>
# define ACL_XATTR "system.posix_acl_default"
#define ACL_XATTR "system.posix_acl_default"
typedef struct hash_table Hash_table;
@ -143,39 +143,39 @@ struct ovl_layer
struct data_source
{
int (*num_of_layers) (const char *opaque, const char *path);
int (*load_data_source)(struct ovl_layer *l, const char *opaque, const char *path, int n_layer);
int (*cleanup)(struct ovl_layer *l);
int (*file_exists)(struct ovl_layer *l, const char *pathname);
int (*statat)(struct ovl_layer *l, const char *path, struct stat *st, int flags, unsigned int mask);
int (*fstat)(struct ovl_layer *l, int fd, const char *path, unsigned int mask, struct stat *st);
void *(*opendir)(struct ovl_layer *l, const char *path);
struct dirent *(*readdir)(void *dirp);
int (*closedir)(void *dirp);
int (*openat)(struct ovl_layer *l, const char *path, int flags, mode_t mode);
int (*listxattr)(struct ovl_layer *l, const char *path, char *buf, size_t size);
int (*getxattr)(struct ovl_layer *l, const char *path, const char *name, char *buf, size_t size);
ssize_t (*readlinkat)(struct ovl_layer *l, const char *path, char *buf, size_t bufsiz);
bool (*support_acls)(struct ovl_layer *l);
int (*load_data_source) (struct ovl_layer *l, const char *opaque, const char *path, int n_layer);
int (*cleanup) (struct ovl_layer *l);
int (*file_exists) (struct ovl_layer *l, const char *pathname);
int (*statat) (struct ovl_layer *l, const char *path, struct stat *st, int flags, unsigned int mask);
int (*fstat) (struct ovl_layer *l, int fd, const char *path, unsigned int mask, struct stat *st);
void *(*opendir) (struct ovl_layer *l, const char *path);
struct dirent *(*readdir) (void *dirp);
int (*closedir) (void *dirp);
int (*openat) (struct ovl_layer *l, const char *path, int flags, mode_t mode);
int (*listxattr) (struct ovl_layer *l, const char *path, char *buf, size_t size);
int (*getxattr) (struct ovl_layer *l, const char *path, const char *name, char *buf, size_t size);
ssize_t (*readlinkat) (struct ovl_layer *l, const char *path, char *buf, size_t bufsiz);
bool (*support_acls) (struct ovl_layer *l);
};
/* passthrough to the file system. */
extern struct data_source direct_access_ds;
# ifndef HAVE_STATX
# define STATX_TYPE 0x00000001U /* Want/got stx_mode & S_IFMT */
# define STATX_MODE 0x00000002U /* Want/got stx_mode & ~S_IFMT */
# define STATX_NLINK 0x00000004U /* Want/got stx_nlink */
# define STATX_UID 0x00000008U /* Want/got stx_uid */
# define STATX_GID 0x00000010U /* Want/got stx_gid */
# define STATX_ATIME 0x00000020U /* Want/got stx_atime */
# define STATX_MTIME 0x00000040U /* Want/got stx_mtime */
# define STATX_CTIME 0x00000080U /* Want/got stx_ctime */
# define STATX_INO 0x00000100U /* Want/got stx_ino */
# define STATX_SIZE 0x00000200U /* Want/got stx_size */
# define STATX_BLOCKS 0x00000400U /* Want/got stx_blocks */
# define STATX_BASIC_STATS 0x000007ffU /* The stuff in the normal stat struct */
# define STATX_BTIME 0x00000800U /* Want/got stx_btime */
# define STATX_ALL 0x00000fffU /* All currently supported flags */
# endif
#ifndef HAVE_STATX
# define STATX_TYPE 0x00000001U /* Want/got stx_mode & S_IFMT */
# define STATX_MODE 0x00000002U /* Want/got stx_mode & ~S_IFMT */
# define STATX_NLINK 0x00000004U /* Want/got stx_nlink */
# define STATX_UID 0x00000008U /* Want/got stx_uid */
# define STATX_GID 0x00000010U /* Want/got stx_gid */
# define STATX_ATIME 0x00000020U /* Want/got stx_atime */
# define STATX_MTIME 0x00000040U /* Want/got stx_mtime */
# define STATX_CTIME 0x00000080U /* Want/got stx_ctime */
# define STATX_INO 0x00000100U /* Want/got stx_ino */
# define STATX_SIZE 0x00000200U /* Want/got stx_size */
# define STATX_BLOCKS 0x00000400U /* Want/got stx_blocks */
# define STATX_BASIC_STATS 0x000007ffU /* The stuff in the normal stat struct */
# define STATX_BTIME 0x00000800U /* Want/got stx_btime */
# define STATX_ALL 0x00000fffU /* All currently supported flags */
#endif
#endif

View File

@ -17,24 +17,26 @@
*/
#ifndef FUSE_OVERLAYFS_ERROR_H
# define FUSE_OVERLAYFS_ERROR_H
#define FUSE_OVERLAYFS_ERROR_H
# include <config.h>
#include <config.h>
# ifdef HAVE_ERROR_H
#ifdef HAVE_ERROR_H
# include <error.h>
# else
# define error(status, errno, fmt, ...) do { \
if (errno == 0) \
fprintf (stderr, "fuse-overlayfs: " fmt "\n", ##__VA_ARGS__); \
else \
#else
# define error(status, errno, fmt, ...) \
do \
{ \
fprintf (stderr, "fuse-overlayfs: " fmt, ##__VA_ARGS__); \
fprintf (stderr, ": %s\n", strerror (errno)); \
} \
if (status) \
exit (status); \
} while(0)
# endif
if (errno == 0) \
fprintf (stderr, "fuse-overlayfs: " fmt "\n", ##__VA_ARGS__); \
else \
{ \
fprintf (stderr, "fuse-overlayfs: " fmt, ##__VA_ARGS__); \
fprintf (stderr, ": %s\n", strerror (errno)); \
} \
if (status) \
exit (status); \
} while (0)
#endif
#endif

622
main.c

File diff suppressed because it is too large Load Diff

View File

@ -52,9 +52,9 @@ plugin_load_one (struct ovl_plugin_context *context, const char *path)
plugin_name name;
struct ovl_plugin *p;
plugin_version version;
void *handle = dlopen (path, RTLD_NOW|RTLD_LOCAL);
void *handle = dlopen (path, RTLD_NOW | RTLD_LOCAL);
if (! handle)
error (EXIT_FAILURE, 0, "cannot load plugin %s: %s", path, dlerror());
error (EXIT_FAILURE, 0, "cannot load plugin %s: %s", path, dlerror ());
p = calloc (1, sizeof (*p));
if (p == NULL)

View File

@ -17,10 +17,10 @@
*/
#ifndef PLUGIN_MANAGER_H
# define PLUGIN_MANAGER_H
# include <config.h>
#define PLUGIN_MANAGER_H
#include <config.h>
# include <dlfcn.h>
#include <dlfcn.h>
struct ovl_plugin_context
{
@ -37,8 +37,7 @@ struct ovl_plugin_context *load_plugins (const char *plugins);
/* taken from glibc unistd.h and fixes musl */
#ifndef TEMP_FAILURE_RETRY
#define TEMP_FAILURE_RETRY(expression) \
(__extension__ \
({ long int __result; \
(__extension__ ({ long int __result; \
do __result = (long int) (expression); \
while (__result == -1L && errno == EINTR); \
__result; }))

View File

@ -17,16 +17,16 @@
*/
#ifndef PLUGIN_H
# define PLUGIN_H
# include <config.h>
#define PLUGIN_H
#include <config.h>
# include <utils.h>
# include <fuse-overlayfs.h>
#include <utils.h>
#include <fuse-overlayfs.h>
typedef struct data_source *(*plugin_load_data_source)(const char *opaque, const char *path);
typedef int (*plugin_release)();
typedef const char *(*plugin_name)();
typedef int (*plugin_version)();
typedef struct data_source *(*plugin_load_data_source) (const char *opaque, const char *path);
typedef int (*plugin_release) ();
typedef const char *(*plugin_name) ();
typedef int (*plugin_version) ();
struct ovl_plugin
{

View File

@ -14,13 +14,13 @@ main ()
unlink ("suid");
unlink ("nosuid");
fd = open ("suid", O_WRONLY|O_CREAT|O_EXCL);
fd = open ("suid", O_WRONLY | O_CREAT | O_EXCL);
write (fd, "1", 1);
fchown (fd, 0, 0);
fchmod (fd, S_ISUID | 0755);
close (fd);
fd = open ("nosuid", O_WRONLY|O_CREAT|O_EXCL);
fd = open ("nosuid", O_WRONLY | O_CREAT | O_EXCL);
write (fd, "1", 1);
fchown (fd, 0, 0);
fchmod (fd, S_ISUID | 0755);

51
utils.c
View File

@ -32,48 +32,42 @@
#include <sys/xattr.h>
#ifndef TEMP_FAILURE_RETRY
#define TEMP_FAILURE_RETRY(expression) \
(__extension__ \
({ long int __result; \
# define TEMP_FAILURE_RETRY(expression) \
(__extension__ ({ long int __result; \
do __result = (long int) (expression); \
while (__result == -1L && errno == EINTR); \
__result; }))
#endif
#ifndef RESOLVE_IN_ROOT
# define RESOLVE_IN_ROOT 0x10
# define RESOLVE_IN_ROOT 0x10
#endif
#ifndef __NR_openat2
# define __NR_openat2 437
# define __NR_openat2 437
#endif
/* uClibc and uClibc-ng don't provide O_TMPFILE */
#ifndef O_TMPFILE
# define O_TMPFILE (020000000 | O_DIRECTORY)
# define O_TMPFILE (020000000 | O_DIRECTORY)
#endif
/* List of all valid flags for the open/openat flags argument: */
#define VALID_OPEN_FLAGS \
(O_RDONLY | O_WRONLY | O_RDWR | O_CREAT | O_EXCL | O_NOCTTY | O_TRUNC | \
O_APPEND | O_NDELAY | O_NONBLOCK | O_NDELAY | O_SYNC | O_DSYNC | \
FASYNC | O_DIRECT | O_LARGEFILE | O_DIRECTORY | O_NOFOLLOW | \
O_NOATIME | O_CLOEXEC | O_PATH | O_TMPFILE)
(O_RDONLY | O_WRONLY | O_RDWR | O_CREAT | O_EXCL | O_NOCTTY | O_TRUNC | O_APPEND | O_NDELAY | O_NONBLOCK | O_NDELAY | O_SYNC | O_DSYNC | FASYNC | O_DIRECT | O_LARGEFILE | O_DIRECTORY | O_NOFOLLOW | O_NOATIME | O_CLOEXEC | O_PATH | O_TMPFILE)
static int
syscall_openat2 (int dirfd, const char *path, uint64_t flags, uint64_t mode, uint64_t resolve)
{
struct openat2_open_how
{
uint64_t flags;
uint64_t mode;
uint64_t resolve;
}
how =
{
.flags = flags & VALID_OPEN_FLAGS,
.mode = (flags & O_CREAT) ? (mode & 07777) : 0,
.resolve = resolve,
};
{
uint64_t flags;
uint64_t mode;
uint64_t resolve;
} how = {
.flags = flags & VALID_OPEN_FLAGS,
.mode = (flags & O_CREAT) ? (mode & 07777) : 0,
.resolve = resolve,
};
return (int) syscall (__NR_openat2, dirfd, path, &how, sizeof (how), 0);
}
@ -97,18 +91,19 @@ safe_openat (int dirfd, const char *pathname, int flags, mode_t mode)
}
return ret;
}
fallback:
fallback:
return openat (dirfd, pathname, flags, mode);
}
int
file_exists_at (int dirfd, const char *pathname)
{
int ret = faccessat (dirfd, pathname, F_OK, AT_SYMLINK_NOFOLLOW|AT_EACCESS);
if (ret < 0 && errno == EINVAL) {
struct stat buf;
return fstatat (dirfd, pathname, &buf, AT_SYMLINK_NOFOLLOW);
}
int ret = faccessat (dirfd, pathname, F_OK, AT_SYMLINK_NOFOLLOW | AT_EACCESS);
if (ret < 0 && errno == EINVAL)
{
struct stat buf;
return fstatat (dirfd, pathname, &buf, AT_SYMLINK_NOFOLLOW);
}
return ret;
}
@ -219,7 +214,7 @@ open_fd_or_get_path (struct ovl_layer *l, const char *path, char *out, int *fd,
{
out[0] = '\0';
*fd = l->ds->openat (l, path, O_NONBLOCK|O_NOFOLLOW|flags, 0);
*fd = l->ds->openat (l, path, O_NONBLOCK | O_NOFOLLOW | flags, 0);
if (*fd < 0 && (errno == ELOOP || errno == EISDIR || errno == ENXIO))
{
strconcat3 (out, PATH_MAX, l->path, "/", path);

44
utils.h
View File

@ -16,25 +16,25 @@
along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
#ifndef UTILS_H
# define UTILS_H
#define UTILS_H
#ifndef _GNU_SOURCE
# define _GNU_SOURCE
# define _GNU_SOURCE
#endif
# include <config.h>
# include <dirent.h>
# include <fcntl.h>
# include <limits.h>
# include <stdio.h>
# include <stdlib.h>
# include <sys/types.h>
# include <unistd.h>
# include "fuse-overlayfs.h"
#include <config.h>
#include <dirent.h>
#include <fcntl.h>
#include <limits.h>
#include <stdio.h>
#include <stdlib.h>
#include <sys/types.h>
#include <unistd.h>
#include "fuse-overlayfs.h"
# define XATTR_OVERRIDE_STAT "user.fuseoverlayfs.override_stat"
# define XATTR_PRIVILEGED_OVERRIDE_STAT "security.fuseoverlayfs.override_stat"
# define XATTR_OVERRIDE_CONTAINERS_STAT "user.containers.override_stat"
#define XATTR_OVERRIDE_STAT "user.fuseoverlayfs.override_stat"
#define XATTR_PRIVILEGED_OVERRIDE_STAT "security.fuseoverlayfs.override_stat"
#define XATTR_OVERRIDE_CONTAINERS_STAT "user.containers.override_stat"
void cleanup_freep (void *p);
void cleanup_filep (FILE **f);
@ -46,17 +46,17 @@ int file_exists_at (int dirfd, const char *pathname);
int strconcat3 (char *dest, size_t size, const char *s1, const char *s2, const char *s3);
int open_fd_or_get_path (struct ovl_layer *l, const char *path, char *out, int *fd, int flags);
# define cleanup_file __attribute__((cleanup (cleanup_filep)))
# define cleanup_free __attribute__((cleanup (cleanup_freep)))
# define cleanup_close __attribute__((cleanup (cleanup_closep)))
# define cleanup_dir __attribute__((cleanup (cleanup_dirp)))
#define cleanup_file __attribute__ ((cleanup (cleanup_filep)))
#define cleanup_free __attribute__ ((cleanup (cleanup_freep)))
#define cleanup_close __attribute__ ((cleanup (cleanup_closep)))
#define cleanup_dir __attribute__ ((cleanup (cleanup_dirp)))
# define LIKELY(x) __builtin_expect((x),1)
# define UNLIKELY(x) __builtin_expect((x),0)
#define LIKELY(x) __builtin_expect ((x), 1)
#define UNLIKELY(x) __builtin_expect ((x), 0)
# ifdef HAVE_STATX
#ifdef HAVE_STATX
void statx_to_stat (struct statx *stx, struct stat *st);
# endif
#endif
int safe_openat (int dirfd, const char *pathname, int flags, mode_t mode);