Coverage Report

Created: 2026-05-16 06:39

next uncovered line (L), next uncovered region (R), next uncovered branch (B)
/src/util-linux/include/fileutils.h
Line
Count
Source
1
/*
2
 * No copyright is claimed.  This code is in the public domain; do with
3
 * it what you wish.
4
 **/
5
#ifndef UTIL_LINUX_FILEUTILS
6
#define UTIL_LINUX_FILEUTILS
7
8
#include <stdio.h>
9
#include <fcntl.h>
10
#include <unistd.h>
11
#include <dirent.h>
12
#include <sys/stat.h>
13
14
#include "c.h"
15
16
extern int mkstemp_cloexec(char *template);
17
18
extern int xmkstemp(char **tmpname, const char *dir, const char *prefix);
19
20
static inline FILE *xfmkstemp(char **tmpname, const char *dir, const char *prefix)
21
0
{
22
0
  int fd;
23
0
  FILE *ret;
24
0
25
0
  fd = xmkstemp(tmpname, dir, prefix);
26
0
  if (fd == -1)
27
0
    return NULL;
28
0
29
0
  if (!(ret = fdopen(fd, "w+" UL_CLOEXECSTR))) {
30
0
    close(fd);
31
0
    return NULL;
32
0
  }
33
0
  return ret;
34
0
}
Unexecuted instantiation: last.c:xfmkstemp
Unexecuted instantiation: fileutils.c:xfmkstemp
35
36
#ifdef HAVE_OPENAT
37
static inline FILE *fopen_at(int dir, const char *filename,
38
                             int flags, const char *mode)
39
0
{
40
0
  int fd = openat(dir, filename, flags);
41
0
  FILE *ret;
42
0
43
0
  if (fd < 0)
44
0
    return NULL;
45
0
46
0
  ret = fdopen(fd, mode);
47
0
  if (!ret)
48
0
    close(fd);
49
0
  return ret;
50
0
}
Unexecuted instantiation: last.c:fopen_at
Unexecuted instantiation: fileutils.c:fopen_at
51
52
extern FILE *fopen_at_no_link(int dir, const char *filename,
53
                             int flags, mode_t perm, const char *mode);
54
#endif /* HAVE_OPENAT */
55
56
static inline int is_same_inode(const int fd, const struct stat *st)
57
0
{
58
0
  struct stat f;
59
0
60
0
  if (fstat(fd, &f) < 0)
61
0
    return 0;
62
0
  else if (f.st_dev != st->st_dev || f.st_ino != st->st_ino)
63
0
    return 0;
64
0
  return 1;
65
0
}
Unexecuted instantiation: last.c:is_same_inode
Unexecuted instantiation: fileutils.c:is_same_inode
66
67
extern int dup_fd_cloexec(int oldfd, int lowfd);
68
extern unsigned int get_fd_tabsize(void);
69
70
extern int ul_mkdir_p(const char *path, mode_t mode);
71
extern char *stripoff_last_component(char *path);
72
73
/* This is readdir()-like function, but skips "." and ".." directory entries */
74
static inline struct dirent *xreaddir(DIR *dp)
75
0
{
76
0
  struct dirent *d;
77
78
0
  while ((d = readdir(dp))) {
79
0
    if (!strcmp(d->d_name, ".") ||
80
0
        !strcmp(d->d_name, ".."))
81
0
      continue;
82
0
    break;
83
0
  }
84
0
  return d;
85
0
}
Unexecuted instantiation: last.c:xreaddir
Unexecuted instantiation: fileutils.c:xreaddir
86
87
88
#ifdef HAVE_SYS_SYSCALL_H
89
# include <sys/syscall.h>
90
91
# if !defined(HAVE_CLOSE_RANGE) && defined(SYS_close_range)
92
#  include <sys/types.h>
93
static inline int close_range(unsigned int first, unsigned int last, int flags)
94
{
95
  return syscall(SYS_close_range, first, last, flags);
96
}
97
#  define HAVE_CLOSE_RANGE 1
98
# endif /* SYS_close_range */
99
100
# if !defined(HAVE_STATX) && defined(HAVE_STRUCT_STATX) && defined(SYS_statx)
101
static inline int statx(int fd, const char *restrict path, int flags,
102
      unsigned int mask, struct statx *stx)
103
{
104
  return syscall(SYS_statx, fd, path, flags, mask, stx);
105
}
106
#  define HAVE_STATX 1
107
# endif /* SYS_statx */
108
109
# if !defined(HAVE_COPY_FILE_RANGE) && defined(SYS_copy_file_range)
110
static inline ssize_t copy_file_range(int fd_in, off_t *off_in,
111
      int fd_out, off_t *off_out, size_t size, unsigned int flags)
112
{
113
  return syscall(SYS_copy_file_range, fd_in, off_in, fd_out,
114
    off_out, size, flags);
115
}
116
#  define HAVE_COPY_FILE_RANGE 1
117
# endif /* SYS_copy_file_range */
118
119
#endif  /* HAVE_SYS_SYSCALL_H */
120
121
122
extern void ul_close_all_fds(unsigned int first, unsigned int last);
123
124
0
#define UL_COPY_READ_ERROR (-1)
125
0
#define UL_COPY_WRITE_ERROR (-2)
126
int ul_copy_file(int from, int to);
127
128
extern int ul_reopen(int fd, int flags);
129
extern char *ul_basename(char *path);
130
131
extern char *ul_restricted_path_oper(const char *path,
132
    int (*oper)(const char *path, char **result, void *data),
133
    void *data);
134
135
/* return 1 if @d is "." or ".." */
136
static inline bool is_dotdir_dirent(const struct dirent *d)
137
0
{
138
0
  return (d && d->d_name[0] == '.'
139
0
    && (d->d_name[1] == 0
140
0
      || (d->d_name[1] == '.' && d->d_name[2] == 0)));
141
0
}
Unexecuted instantiation: last.c:is_dotdir_dirent
Unexecuted instantiation: fileutils.c:is_dotdir_dirent
142
143
#endif /* UTIL_LINUX_FILEUTILS */