2 * Simple functions to access files. Paths can be globally prefixed to read
3 * data from an alternative source (e.g. a /proc dump for regression tests).
5 * The paths is possible to format by printf-like way for functions with "f"
6 * postfix in the name (e.g. readf, openf, ... ul_path_readf_u64()).
8 * The ul_path_read_* API is possible to use without path_cxt handler. In this
9 * case is not possible to use global prefix and printf-like formatting.
11 * No copyright is claimed. This code is in the public domain; do with
14 * Written by Karel Zak <kzak@redhat.com> [February 2018]
24 #include "fileutils.h"
31 * Debug stuff (based on include/debug.h)
33 static UL_DEBUG_DEFINE_MASK(ulpath
);
34 UL_DEBUG_DEFINE_MASKNAMES(ulpath
) = UL_DEBUG_EMPTY_MASKNAMES
;
36 #define ULPATH_DEBUG_INIT (1 << 1)
37 #define ULPATH_DEBUG_CXT (1 << 2)
39 #define DBG(m, x) __UL_DBG(ulpath, ULPATH_DEBUG_, m, x)
40 #define ON_DBG(m, x) __UL_DBG_CALL(ulpath, ULPATH_DEBUG_, m, x)
42 #define UL_DEBUG_CURRENT_MASK UL_DEBUG_MASK(ulpath)
45 void ul_path_init_debug(void)
47 if (ulpath_debug_mask
)
49 __UL_INIT_DEBUG_FROM_ENV(ulpath
, ULPATH_DEBUG_
, 0, ULPATH_DEBUG
);
52 struct path_cxt
*ul_new_path(const char *dir
, ...)
54 struct path_cxt
*pc
= calloc(1, sizeof(*pc
));
59 DBG(CXT
, ul_debugobj(pc
, "alloc"));
69 rc
= vasprintf(&pc
->dir_path
, dir
, ap
);
72 if (rc
< 0 || !pc
->dir_path
)
81 void ul_ref_path(struct path_cxt
*pc
)
87 void ul_unref_path(struct path_cxt
*pc
)
94 if (pc
->refcount
<= 0) {
95 DBG(CXT
, ul_debugobj(pc
, "dealloc"));
98 ul_path_close_dirfd(pc
);
105 int ul_path_set_prefix(struct path_cxt
*pc
, const char *prefix
)
109 assert(pc
->dir_fd
< 0);
119 DBG(CXT
, ul_debugobj(pc
, "new prefix: '%s'", p
));
123 const char *ul_path_get_prefix(struct path_cxt
*pc
)
125 return pc
? pc
->prefix
: NULL
;
128 int ul_path_set_dir(struct path_cxt
*pc
, const char *dir
)
138 if (pc
->dir_fd
>= 0) {
145 DBG(CXT
, ul_debugobj(pc
, "new dir: '%s'", p
));
149 const char *ul_path_get_dir(struct path_cxt
*pc
)
151 return pc
? pc
->dir_path
: NULL
;
154 int ul_path_set_dialect(struct path_cxt
*pc
, void *data
, void free_data(struct path_cxt
*))
157 pc
->free_dialect
= free_data
;
158 DBG(CXT
, ul_debugobj(pc
, "(re)set dialect"));
162 void *ul_path_get_dialect(struct path_cxt
*pc
)
164 return pc
? pc
->dialect
: NULL
;
167 int ul_path_set_enoent_redirect(struct path_cxt
*pc
, int (*func
)(struct path_cxt
*, const char *, int *))
169 pc
->redirect_on_enoent
= func
;
173 static const char *get_absdir(struct path_cxt
*pc
)
181 dirpath
= pc
->dir_path
;
187 rc
= snprintf(pc
->path_buffer
, sizeof(pc
->path_buffer
), "%s/%s", pc
->prefix
, dirpath
);
190 if ((size_t)rc
>= sizeof(pc
->path_buffer
)) {
191 errno
= ENAMETOOLONG
;
195 return pc
->path_buffer
;
198 int ul_path_get_dirfd(struct path_cxt
*pc
)
201 assert(pc
->dir_path
);
203 if (pc
->dir_fd
< 0) {
204 const char *path
= get_absdir(pc
);
208 DBG(CXT
, ul_debugobj(pc
, "opening dir: '%s'", path
));
209 pc
->dir_fd
= open(path
, O_RDONLY
|O_CLOEXEC
);
215 /* Note that next ul_path_get_dirfd() will reopen the directory */
216 void ul_path_close_dirfd(struct path_cxt
*pc
)
220 if (pc
->dir_fd
>= 0) {
221 DBG(CXT
, ul_debugobj(pc
, "closing dir"));
227 int ul_path_isopen_dirfd(struct path_cxt
*pc
)
229 return pc
&& pc
->dir_fd
>= 0;
232 static const char *ul_path_mkpath(struct path_cxt
*pc
, const char *path
, va_list ap
)
238 rc
= vsnprintf(pc
->path_buffer
, sizeof(pc
->path_buffer
), path
, ap
);
245 if ((size_t)rc
>= sizeof(pc
->path_buffer
)) {
246 errno
= ENAMETOOLONG
;
250 return pc
->path_buffer
;
253 char *ul_path_get_abspath(struct path_cxt
*pc
, char *buf
, size_t bufsz
, const char *path
, ...)
258 const char *tail
= NULL
, *dirpath
= pc
->dir_path
;
261 tail
= ul_path_mkpath(pc
, path
, ap
);
264 if (dirpath
&& *dirpath
== '/')
266 if (tail
&& *tail
== '/')
269 rc
= snprintf(buf
, bufsz
, "%s/%s/%s",
270 pc
->prefix
? pc
->prefix
: "",
271 dirpath
? dirpath
: "",
274 if ((size_t)rc
>= bufsz
) {
275 errno
= ENAMETOOLONG
;
279 const char *tmp
= get_absdir(pc
);
283 xstrncpy(buf
, tmp
, bufsz
);
290 int ul_path_access(struct path_cxt
*pc
, int mode
, const char *path
)
295 rc
= access(path
, mode
);
296 DBG(CXT
, ul_debug("access '%s' [no context, rc=%d]", path
, rc
));
298 int dir
= ul_path_get_dirfd(pc
);
304 rc
= faccessat(dir
, path
, mode
, 0);
306 if (rc
&& errno
== ENOENT
307 && pc
->redirect_on_enoent
308 && pc
->redirect_on_enoent(pc
, path
, &dir
) == 0)
309 rc
= faccessat(dir
, path
, mode
, 0);
311 DBG(CXT
, ul_debugobj(pc
, "access: '%s' [rc=%d]", path
, rc
));
316 int ul_path_accessf(struct path_cxt
*pc
, int mode
, const char *path
, ...)
322 p
= ul_path_mkpath(pc
, path
, ap
);
325 return !p
? -errno
: ul_path_access(pc
, mode
, p
);
328 int ul_path_stat(struct path_cxt
*pc
, struct stat
*sb
, const char *path
)
334 DBG(CXT
, ul_debug("stat '%s' [no context, rc=%d]", path
, rc
));
336 int dir
= ul_path_get_dirfd(pc
);
342 rc
= fstatat(dir
, path
, sb
, 0);
344 if (rc
&& errno
== ENOENT
345 && pc
->redirect_on_enoent
346 && pc
->redirect_on_enoent(pc
, path
, &dir
) == 0)
347 rc
= fstatat(dir
, path
, sb
, 0);
349 DBG(CXT
, ul_debugobj(pc
, "stat '%s' [rc=%d]", path
, rc
));
354 int ul_path_open(struct path_cxt
*pc
, int flags
, const char *path
)
359 fd
= open(path
, flags
);
360 DBG(CXT
, ul_debug("opening '%s' [no context]", path
));
363 int dir
= ul_path_get_dirfd(pc
);
370 fdx
= fd
= openat(dir
, path
, flags
);
372 if (fd
< 0 && errno
== ENOENT
373 && pc
->redirect_on_enoent
374 && pc
->redirect_on_enoent(pc
, path
, &dir
) == 0)
375 fd
= openat(dir
, path
, flags
);
377 DBG(CXT
, ul_debugobj(pc
, "opening '%s'%s", path
, fdx
!= fd
? " [redirected]" : ""));
382 int ul_path_vopenf(struct path_cxt
*pc
, int flags
, const char *path
, va_list ap
)
384 const char *p
= ul_path_mkpath(pc
, path
, ap
);
386 return !p
? -errno
: ul_path_open(pc
, flags
, p
);
389 int ul_path_openf(struct path_cxt
*pc
, int flags
, const char *path
, ...)
395 rc
= ul_path_vopenf(pc
, flags
, path
, ap
);
402 * Maybe stupid, but good enough ;-)
404 static int mode2flags(const char *mode
)
409 for (p
= mode
; p
&& *p
; p
++) {
410 if (*p
== 'r' && *(p
+ 1) == '+')
415 else if (*p
== 'w' && *(p
+ 1) == '+')
416 flags
|= O_RDWR
| O_TRUNC
;
418 flags
|= O_WRONLY
| O_TRUNC
;
420 else if (*p
== 'a' && *(p
+ 1) == '+')
421 flags
|= O_RDWR
| O_APPEND
;
423 flags
|= O_WRONLY
| O_APPEND
;
425 else if (*p
== *UL_CLOEXECSTR
)
433 FILE *ul_path_fopen(struct path_cxt
*pc
, const char *mode
, const char *path
)
435 int flags
= mode2flags(mode
);
436 int fd
= ul_path_open(pc
, flags
, path
);
441 return fdopen(fd
, mode
);
445 FILE *ul_path_vfopenf(struct path_cxt
*pc
, const char *mode
, const char *path
, va_list ap
)
447 const char *p
= ul_path_mkpath(pc
, path
, ap
);
449 return !p
? NULL
: ul_path_fopen(pc
, mode
, p
);
452 FILE *ul_path_fopenf(struct path_cxt
*pc
, const char *mode
, const char *path
, ...)
458 f
= ul_path_vfopenf(pc
, mode
, path
, ap
);
465 * Open directory @path in read-onl mode. If the path is NULL then duplicate FD
466 * to the directory addressed by @pc.
468 DIR *ul_path_opendir(struct path_cxt
*pc
, const char *path
)
474 fd
= ul_path_open(pc
, O_RDONLY
|O_CLOEXEC
, path
);
475 else if (pc
->dir_path
) {
478 DBG(CXT
, ul_debugobj(pc
, "duplicate dir path"));
479 dirfd
= ul_path_get_dirfd(pc
);
481 fd
= dup_fd_cloexec(dirfd
, STDERR_FILENO
+ 1);
499 * Open directory @path in read-onl mode. If the path is NULL then duplicate FD
500 * to the directory addressed by @pc.
502 DIR *ul_path_vopendirf(struct path_cxt
*pc
, const char *path
, va_list ap
)
504 const char *p
= ul_path_mkpath(pc
, path
, ap
);
506 return !p
? NULL
: ul_path_opendir(pc
, p
);
510 * Open directory @path in read-onl mode. If the path is NULL then duplicate FD
511 * to the directory addressed by @pc.
513 DIR *ul_path_opendirf(struct path_cxt
*pc
, const char *path
, ...)
519 dir
= ul_path_vopendirf(pc
, path
, ap
);
526 * If @path is NULL then readlink is called on @pc directory.
528 ssize_t
ul_path_readlink(struct path_cxt
*pc
, char *buf
, size_t bufsiz
, const char *path
)
533 const char *p
= get_absdir(pc
);
536 return readlink(p
, buf
, bufsiz
);
539 dirfd
= ul_path_get_dirfd(pc
);
546 return readlinkat(dirfd
, path
, buf
, bufsiz
);
550 * If @path is NULL then readlink is called on @pc directory.
552 ssize_t
ul_path_readlinkf(struct path_cxt
*pc
, char *buf
, size_t bufsiz
, const char *path
, ...)
558 p
= ul_path_mkpath(pc
, path
, ap
);
561 return !p
? -errno
: ul_path_readlink(pc
, buf
, bufsiz
, p
);
564 int ul_path_read(struct path_cxt
*pc
, char *buf
, size_t len
, const char *path
)
569 fd
= ul_path_open(pc
, O_RDONLY
|O_CLOEXEC
, path
);
573 DBG(CXT
, ul_debug(" reading '%s'", path
));
574 rc
= read_all(fd
, buf
, len
);
582 int ul_path_vreadf(struct path_cxt
*pc
, char *buf
, size_t len
, const char *path
, va_list ap
)
584 const char *p
= ul_path_mkpath(pc
, path
, ap
);
586 return !p
? -errno
: ul_path_read(pc
, buf
, len
, p
);
589 int ul_path_readf(struct path_cxt
*pc
, char *buf
, size_t len
, const char *path
, ...)
595 rc
= ul_path_vreadf(pc
, buf
, len
, path
, ap
);
603 * Returns newly allocated buffer with data from file. Maximal size is BUFSIZ
604 * (send patch if you need something bigger;-)
606 * Returns size of the string!
608 int ul_path_read_string(struct path_cxt
*pc
, char **str
, const char *path
)
617 rc
= ul_path_read(pc
, buf
, sizeof(buf
) - 1, path
);
621 /* Remove tailing newline (usual in sysfs) */
622 if (rc
> 0 && *(buf
+ rc
- 1) == '\n')
633 int ul_path_readf_string(struct path_cxt
*pc
, char **str
, const char *path
, ...)
639 p
= ul_path_mkpath(pc
, path
, ap
);
642 return !p
? -errno
: ul_path_read_string(pc
, str
, p
);
645 int ul_path_read_buffer(struct path_cxt
*pc
, char *buf
, size_t bufsz
, const char *path
)
647 int rc
= ul_path_read(pc
, buf
, bufsz
- 1, path
);
651 /* Remove tailing newline (usual in sysfs) */
652 if (rc
> 0 && *(buf
+ rc
- 1) == '\n')
660 int ul_path_readf_buffer(struct path_cxt
*pc
, char *buf
, size_t bufsz
, const char *path
, ...)
666 p
= ul_path_mkpath(pc
, path
, ap
);
669 return !p
? -errno
: ul_path_read_buffer(pc
, buf
, bufsz
, p
);
672 int ul_path_scanf(struct path_cxt
*pc
, const char *path
, const char *fmt
, ...)
678 f
= ul_path_fopen(pc
, "r" UL_CLOEXECSTR
, path
);
682 DBG(CXT
, ul_debug(" fscanf [%s] '%s'", fmt
, path
));
684 va_start(fmt_ap
, fmt
);
685 rc
= vfscanf(f
, fmt
, fmt_ap
);
692 int ul_path_scanff(struct path_cxt
*pc
, const char *path
, va_list ap
, const char *fmt
, ...)
698 f
= ul_path_vfopenf(pc
, "r" UL_CLOEXECSTR
, path
, ap
);
702 va_start(fmt_ap
, fmt
);
703 rc
= vfscanf(f
, fmt
, fmt_ap
);
711 int ul_path_read_s64(struct path_cxt
*pc
, int64_t *res
, const char *path
)
716 rc
= ul_path_scanf(pc
, path
, "%"SCNd64
, &x
);
724 int ul_path_readf_s64(struct path_cxt
*pc
, int64_t *res
, const char *path
, ...)
730 p
= ul_path_mkpath(pc
, path
, ap
);
733 return !p
? -errno
: ul_path_read_s64(pc
, res
, p
);
736 int ul_path_read_u64(struct path_cxt
*pc
, uint64_t *res
, const char *path
)
741 rc
= ul_path_scanf(pc
, path
, "%"SCNu64
, &x
);
749 int ul_path_readf_u64(struct path_cxt
*pc
, uint64_t *res
, const char *path
, ...)
755 p
= ul_path_mkpath(pc
, path
, ap
);
758 return !p
? -errno
: ul_path_read_u64(pc
, res
, p
);
761 int ul_path_read_s32(struct path_cxt
*pc
, int *res
, const char *path
)
765 rc
= ul_path_scanf(pc
, path
, "%d", &x
);
773 int ul_path_readf_s32(struct path_cxt
*pc
, int *res
, const char *path
, ...)
779 p
= ul_path_mkpath(pc
, path
, ap
);
782 return !p
? -errno
: ul_path_read_s32(pc
, res
, p
);
785 int ul_path_read_u32(struct path_cxt
*pc
, unsigned int *res
, const char *path
)
790 rc
= ul_path_scanf(pc
, path
, "%u", &x
);
798 int ul_path_readf_u32(struct path_cxt
*pc
, unsigned int *res
, const char *path
, ...)
804 p
= ul_path_mkpath(pc
, path
, ap
);
807 return !p
? -errno
: ul_path_read_u32(pc
, res
, p
);
810 int ul_path_read_majmin(struct path_cxt
*pc
, dev_t
*res
, const char *path
)
814 rc
= ul_path_scanf(pc
, path
, "%d:%d", &maj
, &min
);
818 *res
= makedev(maj
, min
);
822 int ul_path_readf_majmin(struct path_cxt
*pc
, dev_t
*res
, const char *path
, ...)
828 p
= ul_path_mkpath(pc
, path
, ap
);
831 return !p
? -errno
: ul_path_read_majmin(pc
, res
, p
);
834 int ul_path_write_string(struct path_cxt
*pc
, const char *str
, const char *path
)
839 fd
= ul_path_open(pc
, O_WRONLY
|O_CLOEXEC
, path
);
843 rc
= write_all(fd
, str
, strlen(str
));
851 int ul_path_writef_string(struct path_cxt
*pc
, const char *str
, const char *path
, ...)
857 p
= ul_path_mkpath(pc
, path
, ap
);
860 return !p
? -errno
: ul_path_write_string(pc
, str
, p
);
863 int ul_path_write_s64(struct path_cxt
*pc
, int64_t num
, const char *path
)
865 char buf
[sizeof(stringify_value(LLONG_MAX
))];
869 fd
= ul_path_open(pc
, O_WRONLY
|O_CLOEXEC
, path
);
873 len
= snprintf(buf
, sizeof(buf
), "%" PRId64
, num
);
874 if (len
< 0 || (size_t) len
>= sizeof(buf
))
875 rc
= len
< 0 ? -errno
: -E2BIG
;
877 rc
= write_all(fd
, buf
, len
);
885 int ul_path_write_u64(struct path_cxt
*pc
, uint64_t num
, const char *path
)
887 char buf
[sizeof(stringify_value(ULLONG_MAX
))];
891 fd
= ul_path_open(pc
, O_WRONLY
|O_CLOEXEC
, path
);
895 len
= snprintf(buf
, sizeof(buf
), "%" PRIu64
, num
);
896 if (len
< 0 || (size_t) len
>= sizeof(buf
))
897 rc
= len
< 0 ? -errno
: -E2BIG
;
899 rc
= write_all(fd
, buf
, len
);
907 int ul_path_writef_u64(struct path_cxt
*pc
, uint64_t num
, const char *path
, ...)
913 p
= ul_path_mkpath(pc
, path
, ap
);
916 return !p
? -errno
: ul_path_write_u64(pc
, num
, p
);
920 int ul_path_count_dirents(struct path_cxt
*pc
, const char *path
)
925 dir
= ul_path_opendir(pc
, path
);
929 while (xreaddir(dir
)) r
++;
935 int ul_path_countf_dirents(struct path_cxt
*pc
, const char *path
, ...)
941 p
= ul_path_mkpath(pc
, path
, ap
);
944 return !p
? -errno
: ul_path_count_dirents(pc
, p
);
948 * Like fopen() but, @path is always prefixed by @prefix. This function is
949 * useful in case when ul_path_* API is overkill.
951 FILE *ul_prefix_fopen(const char *prefix
, const char *path
, const char *mode
)
958 return fopen(path
, mode
);
962 snprintf(buf
, sizeof(buf
), "%s/%s", prefix
, path
);
963 return fopen(buf
, mode
);
966 #ifdef HAVE_CPU_SET_T
967 static int ul_path_cpuparse(struct path_cxt
*pc
, cpu_set_t
**set
, int maxcpus
, int islist
, const char *path
, va_list ap
)
970 size_t setsize
, len
= maxcpus
* 7;
976 f
= ul_path_vfopenf(pc
, "r" UL_CLOEXECSTR
, path
, ap
);
980 rc
= fgets(buf
, len
, f
) == NULL
? -errno
: 0;
987 if (buf
[len
- 1] == '\n')
990 *set
= cpuset_alloc(maxcpus
, &setsize
, NULL
);
995 if (cpulist_parse(buf
, *set
, setsize
, 0)) {
1000 if (cpumask_parse(buf
, *set
, setsize
)) {
1008 int ul_path_readf_cpuset(struct path_cxt
*pc
, cpu_set_t
**set
, int maxcpus
, const char *path
, ...)
1014 rc
= ul_path_cpuparse(pc
, set
, maxcpus
, 0, path
, ap
);
1020 int ul_path_readf_cpulist(struct path_cxt
*pc
, cpu_set_t
**set
, int maxcpus
, const char *path
, ...)
1026 rc
= ul_path_cpuparse(pc
, set
, maxcpus
, 1, path
, ap
);
1032 #endif /* HAVE_CPU_SET_T */
1035 #ifdef TEST_PROGRAM_PATH
1038 static void __attribute__((__noreturn__
)) usage(void)
1040 fprintf(stdout
, " %s [options] <dir> <command>\n\n", program_invocation_short_name
);
1041 fputs(" -p, --prefix <dir> redirect hardcoded paths to <dir>\n", stdout
);
1043 fputs(" Commands:\n", stdout
);
1044 fputs(" read-u64 <file> read uint64_t from file\n", stdout
);
1045 fputs(" read-s64 <file> read int64_t from file\n", stdout
);
1046 fputs(" read-u32 <file> read uint32_t from file\n", stdout
);
1047 fputs(" read-s32 <file> read int32_t from file\n", stdout
);
1048 fputs(" read-string <file> read string from file\n", stdout
);
1049 fputs(" read-majmin <file> read devno from file\n", stdout
);
1050 fputs(" read-link <file> read symlink\n", stdout
);
1051 fputs(" write-string <file> <str> write string from file\n", stdout
);
1052 fputs(" write-u64 <file> <str> write uint64_t from file\n", stdout
);
1057 int main(int argc
, char *argv
[])
1060 const char *prefix
= NULL
, *dir
, *file
, *command
;
1061 struct path_cxt
*pc
= NULL
;
1063 static const struct option longopts
[] = {
1064 { "prefix", 1, NULL
, 'p' },
1065 { "help", 0, NULL
, 'h' },
1066 { NULL
, 0, NULL
, 0 },
1069 while((c
= getopt_long(argc
, argv
, "p:h", longopts
, NULL
)) != -1) {
1078 err(EXIT_FAILURE
, "try --help");
1083 errx(EXIT_FAILURE
, "<dir> not defined");
1084 dir
= argv
[optind
++];
1086 ul_path_init_debug();
1088 pc
= ul_new_path(dir
);
1090 err(EXIT_FAILURE
, "failed to initialize path context");
1092 ul_path_set_prefix(pc
, prefix
);
1095 errx(EXIT_FAILURE
, "<command> not defined");
1096 command
= argv
[optind
++];
1098 if (strcmp(command
, "read-u32") == 0) {
1102 errx(EXIT_FAILURE
, "<file> not defined");
1103 file
= argv
[optind
++];
1105 if (ul_path_read_u32(pc
, &res
, file
) != 0)
1106 err(EXIT_FAILURE
, "read u64 failed");
1107 printf("read: %s: %u\n", file
, res
);
1109 if (ul_path_readf_u32(pc
, &res
, "%s", file
) != 0)
1110 err(EXIT_FAILURE
, "readf u64 failed");
1111 printf("readf: %s: %u\n", file
, res
);
1113 } else if (strcmp(command
, "read-s32") == 0) {
1117 errx(EXIT_FAILURE
, "<file> not defined");
1118 file
= argv
[optind
++];
1120 if (ul_path_read_s32(pc
, &res
, file
) != 0)
1121 err(EXIT_FAILURE
, "read u64 failed");
1122 printf("read: %s: %d\n", file
, res
);
1124 if (ul_path_readf_s32(pc
, &res
, "%s", file
) != 0)
1125 err(EXIT_FAILURE
, "readf u64 failed");
1126 printf("readf: %s: %d\n", file
, res
);
1128 } else if (strcmp(command
, "read-u64") == 0) {
1132 errx(EXIT_FAILURE
, "<file> not defined");
1133 file
= argv
[optind
++];
1135 if (ul_path_read_u64(pc
, &res
, file
) != 0)
1136 err(EXIT_FAILURE
, "read u64 failed");
1137 printf("read: %s: %" PRIu64
"\n", file
, res
);
1139 if (ul_path_readf_u64(pc
, &res
, "%s", file
) != 0)
1140 err(EXIT_FAILURE
, "readf u64 failed");
1141 printf("readf: %s: %" PRIu64
"\n", file
, res
);
1143 } else if (strcmp(command
, "read-s64") == 0) {
1147 errx(EXIT_FAILURE
, "<file> not defined");
1148 file
= argv
[optind
++];
1150 if (ul_path_read_s64(pc
, &res
, file
) != 0)
1151 err(EXIT_FAILURE
, "read u64 failed");
1152 printf("read: %s: %" PRIu64
"\n", file
, res
);
1154 if (ul_path_readf_s64(pc
, &res
, "%s", file
) != 0)
1155 err(EXIT_FAILURE
, "readf u64 failed");
1156 printf("readf: %s: %" PRIu64
"\n", file
, res
);
1158 } else if (strcmp(command
, "read-majmin") == 0) {
1162 errx(EXIT_FAILURE
, "<file> not defined");
1163 file
= argv
[optind
++];
1165 if (ul_path_read_majmin(pc
, &res
, file
) != 0)
1166 err(EXIT_FAILURE
, "read maj:min failed");
1167 printf("read: %s: %d\n", file
, (int) res
);
1169 if (ul_path_readf_majmin(pc
, &res
, "%s", file
) != 0)
1170 err(EXIT_FAILURE
, "readf maj:min failed");
1171 printf("readf: %s: %d\n", file
, (int) res
);
1173 } else if (strcmp(command
, "read-string") == 0) {
1177 errx(EXIT_FAILURE
, "<file> not defined");
1178 file
= argv
[optind
++];
1180 if (ul_path_read_string(pc
, &res
, file
) < 0)
1181 err(EXIT_FAILURE
, "read string failed");
1182 printf("read: %s: %s\n", file
, res
);
1184 if (ul_path_readf_string(pc
, &res
, "%s", file
) < 0)
1185 err(EXIT_FAILURE
, "readf string failed");
1186 printf("readf: %s: %s\n", file
, res
);
1188 } else if (strcmp(command
, "read-link") == 0) {
1192 errx(EXIT_FAILURE
, "<file> not defined");
1193 file
= argv
[optind
++];
1195 if (ul_path_readlink(pc
, res
, sizeof(res
), file
) < 0)
1196 err(EXIT_FAILURE
, "read symlink failed");
1197 printf("read: %s: %s\n", file
, res
);
1199 if (ul_path_readlinkf(pc
, res
, sizeof(res
), "%s", file
) < 0)
1200 err(EXIT_FAILURE
, "readf symlink failed");
1201 printf("readf: %s: %s\n", file
, res
);
1203 } else if (strcmp(command
, "write-string") == 0) {
1206 if (optind
+ 1 == argc
)
1207 errx(EXIT_FAILURE
, "<file> <string> not defined");
1208 file
= argv
[optind
++];
1209 str
= argv
[optind
++];
1211 if (ul_path_write_string(pc
, str
, file
) != 0)
1212 err(EXIT_FAILURE
, "write string failed");
1213 if (ul_path_writef_string(pc
, str
, "%s", file
) != 0)
1214 err(EXIT_FAILURE
, "writef string failed");
1216 } else if (strcmp(command
, "write-u64") == 0) {
1219 if (optind
+ 1 == argc
)
1220 errx(EXIT_FAILURE
, "<file> <num> not defined");
1221 file
= argv
[optind
++];
1222 num
= strtoumax(argv
[optind
++], NULL
, 0);
1224 if (ul_path_write_u64(pc
, num
, file
) != 0)
1225 err(EXIT_FAILURE
, "write u64 failed");
1226 if (ul_path_writef_u64(pc
, num
, "%s", file
) != 0)
1227 err(EXIT_FAILURE
, "writef u64 failed");
1231 return EXIT_SUCCESS
;
1233 #endif /* TEST_PROGRAM_PATH */