2012-11-02 06:07:36 -05:00
|
|
|
#ifndef UTIL_LINUX_PATH_H
|
|
|
|
#define UTIL_LINUX_PATH_H
|
|
|
|
|
2011-09-15 01:52:29 -05:00
|
|
|
#include <stdio.h>
|
2012-11-05 05:28:00 -06:00
|
|
|
#include <stdint.h>
|
lib/path: new implementation
The goal is to avoid duplicate code in path.c and sysfs.c and make it
possible to define prefix for paths for all sysfs and procfs based
utils. Now we have /proc snapshots (for tests) for lscpu only. It
would be nice to have the same (for sysfs) for lsblk and another tools.
* very simple API to read numbers, strings and symlinks
* based on openat()
pc = ul_new_path("/sys/block/sda");
ul_path_read_u64(pc, &size, "size");
ul_path_read_u64(pc, &lsz, "queue/logical_block_size");
* printf-like API to generate paths, for example:
ul_path_readf_u64(pc, &num, "sda%d/size", partno)
* allow to define prefix to redirect hardcoded paths to another
location, for example:
pc = ul_new_path("/sys/block/sda");
ul_path_set_prefix(pc, "/my/regression/dump");
ul_path_read_u64(pc, &num, "size");
to read /my/regression/dump/sys/block/sda/size
* allow to extend the API by "dialects", for example for sysfs:
pc = ul_new_path(NULL);
sysfs_blkdev_init_path(pc, devno, NULL);
and use ul_path_* functions to read from @pc initialized by
sysfs_blkdev_init_path()
* add test_path binary
Signed-off-by: Karel Zak <kzak@redhat.com>
2018-05-09 08:54:12 -05:00
|
|
|
#include <stdint.h>
|
|
|
|
#include <sys/types.h>
|
|
|
|
#include <dirent.h>
|
|
|
|
|
2018-06-21 07:24:01 -05:00
|
|
|
#include "c.h"
|
|
|
|
|
lib/path: new implementation
The goal is to avoid duplicate code in path.c and sysfs.c and make it
possible to define prefix for paths for all sysfs and procfs based
utils. Now we have /proc snapshots (for tests) for lscpu only. It
would be nice to have the same (for sysfs) for lsblk and another tools.
* very simple API to read numbers, strings and symlinks
* based on openat()
pc = ul_new_path("/sys/block/sda");
ul_path_read_u64(pc, &size, "size");
ul_path_read_u64(pc, &lsz, "queue/logical_block_size");
* printf-like API to generate paths, for example:
ul_path_readf_u64(pc, &num, "sda%d/size", partno)
* allow to define prefix to redirect hardcoded paths to another
location, for example:
pc = ul_new_path("/sys/block/sda");
ul_path_set_prefix(pc, "/my/regression/dump");
ul_path_read_u64(pc, &num, "size");
to read /my/regression/dump/sys/block/sda/size
* allow to extend the API by "dialects", for example for sysfs:
pc = ul_new_path(NULL);
sysfs_blkdev_init_path(pc, devno, NULL);
and use ul_path_* functions to read from @pc initialized by
sysfs_blkdev_init_path()
* add test_path binary
Signed-off-by: Karel Zak <kzak@redhat.com>
2018-05-09 08:54:12 -05:00
|
|
|
struct path_cxt {
|
|
|
|
int dir_fd;
|
|
|
|
char *dir_path;
|
|
|
|
|
|
|
|
int refcount;
|
|
|
|
|
|
|
|
char *prefix;
|
|
|
|
char path_buffer[PATH_MAX];
|
|
|
|
|
|
|
|
void *dialect;
|
|
|
|
void (*free_dialect)(struct path_cxt *);
|
|
|
|
int (*redirect_on_enoent)(struct path_cxt *, const char *, int *);
|
|
|
|
};
|
|
|
|
|
2018-06-21 06:49:03 -05:00
|
|
|
struct path_cxt *ul_new_path(const char *dir, ...);
|
lib/path: new implementation
The goal is to avoid duplicate code in path.c and sysfs.c and make it
possible to define prefix for paths for all sysfs and procfs based
utils. Now we have /proc snapshots (for tests) for lscpu only. It
would be nice to have the same (for sysfs) for lsblk and another tools.
* very simple API to read numbers, strings and symlinks
* based on openat()
pc = ul_new_path("/sys/block/sda");
ul_path_read_u64(pc, &size, "size");
ul_path_read_u64(pc, &lsz, "queue/logical_block_size");
* printf-like API to generate paths, for example:
ul_path_readf_u64(pc, &num, "sda%d/size", partno)
* allow to define prefix to redirect hardcoded paths to another
location, for example:
pc = ul_new_path("/sys/block/sda");
ul_path_set_prefix(pc, "/my/regression/dump");
ul_path_read_u64(pc, &num, "size");
to read /my/regression/dump/sys/block/sda/size
* allow to extend the API by "dialects", for example for sysfs:
pc = ul_new_path(NULL);
sysfs_blkdev_init_path(pc, devno, NULL);
and use ul_path_* functions to read from @pc initialized by
sysfs_blkdev_init_path()
* add test_path binary
Signed-off-by: Karel Zak <kzak@redhat.com>
2018-05-09 08:54:12 -05:00
|
|
|
void ul_unref_path(struct path_cxt *pc);
|
|
|
|
void ul_ref_path(struct path_cxt *pc);
|
|
|
|
|
2018-05-15 06:43:29 -05:00
|
|
|
void ul_path_init_debug(void);
|
|
|
|
|
lib/path: new implementation
The goal is to avoid duplicate code in path.c and sysfs.c and make it
possible to define prefix for paths for all sysfs and procfs based
utils. Now we have /proc snapshots (for tests) for lscpu only. It
would be nice to have the same (for sysfs) for lsblk and another tools.
* very simple API to read numbers, strings and symlinks
* based on openat()
pc = ul_new_path("/sys/block/sda");
ul_path_read_u64(pc, &size, "size");
ul_path_read_u64(pc, &lsz, "queue/logical_block_size");
* printf-like API to generate paths, for example:
ul_path_readf_u64(pc, &num, "sda%d/size", partno)
* allow to define prefix to redirect hardcoded paths to another
location, for example:
pc = ul_new_path("/sys/block/sda");
ul_path_set_prefix(pc, "/my/regression/dump");
ul_path_read_u64(pc, &num, "size");
to read /my/regression/dump/sys/block/sda/size
* allow to extend the API by "dialects", for example for sysfs:
pc = ul_new_path(NULL);
sysfs_blkdev_init_path(pc, devno, NULL);
and use ul_path_* functions to read from @pc initialized by
sysfs_blkdev_init_path()
* add test_path binary
Signed-off-by: Karel Zak <kzak@redhat.com>
2018-05-09 08:54:12 -05:00
|
|
|
int ul_path_set_prefix(struct path_cxt *pc, const char *prefix);
|
|
|
|
const char *ul_path_get_prefix(struct path_cxt *pc);
|
|
|
|
|
|
|
|
int ul_path_set_dir(struct path_cxt *pc, const char *dir);
|
|
|
|
const char *ul_path_get_dir(struct path_cxt *pc);
|
|
|
|
|
|
|
|
int ul_path_set_dialect(struct path_cxt *pc, void *data, void free_data(struct path_cxt *));
|
|
|
|
void *ul_path_get_dialect(struct path_cxt *pc);
|
|
|
|
|
|
|
|
int ul_path_set_enoent_redirect(struct path_cxt *pc, int (*func)(struct path_cxt *, const char *, int *));
|
|
|
|
int ul_path_get_dirfd(struct path_cxt *pc);
|
2018-10-17 06:18:25 -05:00
|
|
|
void ul_path_close_dirfd(struct path_cxt *pc);
|
|
|
|
int ul_path_isopen_dirfd(struct path_cxt *pc);
|
lib/path: new implementation
The goal is to avoid duplicate code in path.c and sysfs.c and make it
possible to define prefix for paths for all sysfs and procfs based
utils. Now we have /proc snapshots (for tests) for lscpu only. It
would be nice to have the same (for sysfs) for lsblk and another tools.
* very simple API to read numbers, strings and symlinks
* based on openat()
pc = ul_new_path("/sys/block/sda");
ul_path_read_u64(pc, &size, "size");
ul_path_read_u64(pc, &lsz, "queue/logical_block_size");
* printf-like API to generate paths, for example:
ul_path_readf_u64(pc, &num, "sda%d/size", partno)
* allow to define prefix to redirect hardcoded paths to another
location, for example:
pc = ul_new_path("/sys/block/sda");
ul_path_set_prefix(pc, "/my/regression/dump");
ul_path_read_u64(pc, &num, "size");
to read /my/regression/dump/sys/block/sda/size
* allow to extend the API by "dialects", for example for sysfs:
pc = ul_new_path(NULL);
sysfs_blkdev_init_path(pc, devno, NULL);
and use ul_path_* functions to read from @pc initialized by
sysfs_blkdev_init_path()
* add test_path binary
Signed-off-by: Karel Zak <kzak@redhat.com>
2018-05-09 08:54:12 -05:00
|
|
|
|
2018-05-17 06:58:30 -05:00
|
|
|
char *ul_path_get_abspath(struct path_cxt *pc, char *buf, size_t bufsz, const char *path, ...)
|
|
|
|
__attribute__ ((__format__ (__printf__, 4, 5)));
|
|
|
|
|
lib/path: new implementation
The goal is to avoid duplicate code in path.c and sysfs.c and make it
possible to define prefix for paths for all sysfs and procfs based
utils. Now we have /proc snapshots (for tests) for lscpu only. It
would be nice to have the same (for sysfs) for lsblk and another tools.
* very simple API to read numbers, strings and symlinks
* based on openat()
pc = ul_new_path("/sys/block/sda");
ul_path_read_u64(pc, &size, "size");
ul_path_read_u64(pc, &lsz, "queue/logical_block_size");
* printf-like API to generate paths, for example:
ul_path_readf_u64(pc, &num, "sda%d/size", partno)
* allow to define prefix to redirect hardcoded paths to another
location, for example:
pc = ul_new_path("/sys/block/sda");
ul_path_set_prefix(pc, "/my/regression/dump");
ul_path_read_u64(pc, &num, "size");
to read /my/regression/dump/sys/block/sda/size
* allow to extend the API by "dialects", for example for sysfs:
pc = ul_new_path(NULL);
sysfs_blkdev_init_path(pc, devno, NULL);
and use ul_path_* functions to read from @pc initialized by
sysfs_blkdev_init_path()
* add test_path binary
Signed-off-by: Karel Zak <kzak@redhat.com>
2018-05-09 08:54:12 -05:00
|
|
|
int ul_path_access(struct path_cxt *pc, int mode, const char *path);
|
|
|
|
int ul_path_accessf(struct path_cxt *pc, int mode, const char *path, ...)
|
|
|
|
__attribute__ ((__format__ (__printf__, 3, 4)));
|
|
|
|
|
|
|
|
int ul_path_open(struct path_cxt *pc, int flags, const char *path);
|
|
|
|
int ul_path_openf(struct path_cxt *pc, int flags, const char *path, ...)
|
|
|
|
__attribute__ ((__format__ (__printf__, 3, 4)));
|
|
|
|
int ul_path_vopenf(struct path_cxt *pc, int flags, const char *path, va_list ap);
|
|
|
|
|
|
|
|
FILE *ul_path_fopen(struct path_cxt *pc, const char *mode, const char *path);
|
|
|
|
FILE *ul_path_fopenf(struct path_cxt *pc, const char *mode, const char *path, ...)
|
|
|
|
__attribute__ ((__format__ (__printf__, 3, 4)));
|
|
|
|
FILE *ul_path_vfopenf(struct path_cxt *pc, const char *mode, const char *path, va_list ap);
|
|
|
|
|
|
|
|
DIR *ul_path_opendir(struct path_cxt *pc, const char *path);
|
|
|
|
DIR *ul_path_vopendirf(struct path_cxt *pc, const char *path, va_list ap);
|
|
|
|
DIR *ul_path_opendirf(struct path_cxt *pc, const char *path, ...)
|
|
|
|
__attribute__ ((__format__ (__printf__, 2, 3)));
|
|
|
|
|
|
|
|
ssize_t ul_path_readlink(struct path_cxt *pc, char *buf, size_t bufsiz, const char *path);
|
|
|
|
ssize_t ul_path_readlinkf(struct path_cxt *pc, char *buf, size_t bufsiz, const char *path, ...)
|
|
|
|
__attribute__ ((__format__ (__printf__, 4, 5)));
|
|
|
|
|
|
|
|
int ul_path_read(struct path_cxt *pc, char *buf, size_t len, const char *path);
|
|
|
|
int ul_path_vreadf(struct path_cxt *pc, char *buf, size_t len, const char *path, va_list ap);
|
|
|
|
int ul_path_readf(struct path_cxt *pc, char *buf, size_t len, const char *path, ...)
|
|
|
|
__attribute__ ((__format__ (__printf__, 4, 5)));
|
|
|
|
|
|
|
|
int ul_path_read_string(struct path_cxt *pc, char **str, const char *path);
|
|
|
|
int ul_path_readf_string(struct path_cxt *pc, char **str, const char *path, ...)
|
|
|
|
__attribute__ ((__format__ (__printf__, 3, 4)));
|
|
|
|
|
2018-05-17 08:42:37 -05:00
|
|
|
int ul_path_read_buffer(struct path_cxt *pc, char *buf, size_t bufsz, const char *path);
|
|
|
|
int ul_path_readf_buffer(struct path_cxt *pc, char *buf, size_t bufsz, const char *path, ...)
|
|
|
|
__attribute__ ((__format__ (__printf__, 4, 5)));
|
|
|
|
|
lib/path: new implementation
The goal is to avoid duplicate code in path.c and sysfs.c and make it
possible to define prefix for paths for all sysfs and procfs based
utils. Now we have /proc snapshots (for tests) for lscpu only. It
would be nice to have the same (for sysfs) for lsblk and another tools.
* very simple API to read numbers, strings and symlinks
* based on openat()
pc = ul_new_path("/sys/block/sda");
ul_path_read_u64(pc, &size, "size");
ul_path_read_u64(pc, &lsz, "queue/logical_block_size");
* printf-like API to generate paths, for example:
ul_path_readf_u64(pc, &num, "sda%d/size", partno)
* allow to define prefix to redirect hardcoded paths to another
location, for example:
pc = ul_new_path("/sys/block/sda");
ul_path_set_prefix(pc, "/my/regression/dump");
ul_path_read_u64(pc, &num, "size");
to read /my/regression/dump/sys/block/sda/size
* allow to extend the API by "dialects", for example for sysfs:
pc = ul_new_path(NULL);
sysfs_blkdev_init_path(pc, devno, NULL);
and use ul_path_* functions to read from @pc initialized by
sysfs_blkdev_init_path()
* add test_path binary
Signed-off-by: Karel Zak <kzak@redhat.com>
2018-05-09 08:54:12 -05:00
|
|
|
int ul_path_scanf(struct path_cxt *pc, const char *path, const char *fmt, ...);
|
|
|
|
int ul_path_scanff(struct path_cxt *pc, const char *path, va_list ap, const char *fmt, ...)
|
|
|
|
__attribute__ ((__format__ (__scanf__, 4, 5)));
|
|
|
|
|
|
|
|
int ul_path_read_majmin(struct path_cxt *pc, dev_t *res, const char *path);
|
|
|
|
int ul_path_readf_majmin(struct path_cxt *pc, dev_t *res, const char *path, ...)
|
|
|
|
__attribute__ ((__format__ (__printf__, 3, 4)));
|
|
|
|
|
|
|
|
int ul_path_read_u32(struct path_cxt *pc, uint32_t *res, const char *path);
|
|
|
|
int ul_path_readf_u32(struct path_cxt *pc, uint32_t *res, const char *path, ...)
|
|
|
|
__attribute__ ((__format__ (__printf__, 3, 4)));
|
|
|
|
|
|
|
|
int ul_path_read_s32(struct path_cxt *pc, int32_t *res, const char *path);
|
|
|
|
int ul_path_readf_s32(struct path_cxt *pc, int32_t *res, const char *path, ...)
|
|
|
|
__attribute__ ((__format__ (__printf__, 3, 4)));
|
|
|
|
|
|
|
|
int ul_path_read_u64(struct path_cxt *pc, uint64_t *res, const char *path);
|
|
|
|
int ul_path_readf_u64(struct path_cxt *pc, uint64_t *res, const char *path, ...)
|
|
|
|
__attribute__ ((__format__ (__printf__, 3, 4)));
|
|
|
|
|
|
|
|
int ul_path_read_s64(struct path_cxt *pc, int64_t *res, const char *path);
|
|
|
|
int ul_path_readf_s64(struct path_cxt *pc, int64_t *res, const char *path, ...)
|
|
|
|
__attribute__ ((__format__ (__printf__, 3, 4)));
|
|
|
|
|
|
|
|
int ul_path_write_string(struct path_cxt *pc, const char *str, const char *path);
|
|
|
|
int ul_path_writef_string(struct path_cxt *pc, const char *str, const char *path, ...)
|
|
|
|
__attribute__ ((__format__ (__printf__, 3, 4)));
|
|
|
|
|
|
|
|
int ul_path_write_u64(struct path_cxt *pc, uint64_t num, const char *path);
|
|
|
|
int ul_path_writef_u64(struct path_cxt *pc, uint64_t num, const char *path, ...)
|
|
|
|
__attribute__ ((__format__ (__printf__, 3, 4)));
|
|
|
|
|
|
|
|
int ul_path_count_dirents(struct path_cxt *pc, const char *path);
|
|
|
|
int ul_path_countf_dirents(struct path_cxt *pc, const char *path, ...)
|
|
|
|
__attribute__ ((__format__ (__printf__, 2, 3)));
|
|
|
|
|
2018-05-25 05:47:06 -05:00
|
|
|
FILE *ul_prefix_fopen(const char *prefix, const char *path, const char *mode);
|
2011-09-15 01:52:29 -05:00
|
|
|
|
2012-11-02 06:07:36 -05:00
|
|
|
|
|
|
|
#ifdef HAVE_CPU_SET_T
|
|
|
|
# include "cpuset.h"
|
lib/path: new implementation
The goal is to avoid duplicate code in path.c and sysfs.c and make it
possible to define prefix for paths for all sysfs and procfs based
utils. Now we have /proc snapshots (for tests) for lscpu only. It
would be nice to have the same (for sysfs) for lsblk and another tools.
* very simple API to read numbers, strings and symlinks
* based on openat()
pc = ul_new_path("/sys/block/sda");
ul_path_read_u64(pc, &size, "size");
ul_path_read_u64(pc, &lsz, "queue/logical_block_size");
* printf-like API to generate paths, for example:
ul_path_readf_u64(pc, &num, "sda%d/size", partno)
* allow to define prefix to redirect hardcoded paths to another
location, for example:
pc = ul_new_path("/sys/block/sda");
ul_path_set_prefix(pc, "/my/regression/dump");
ul_path_read_u64(pc, &num, "size");
to read /my/regression/dump/sys/block/sda/size
* allow to extend the API by "dialects", for example for sysfs:
pc = ul_new_path(NULL);
sysfs_blkdev_init_path(pc, devno, NULL);
and use ul_path_* functions to read from @pc initialized by
sysfs_blkdev_init_path()
* add test_path binary
Signed-off-by: Karel Zak <kzak@redhat.com>
2018-05-09 08:54:12 -05:00
|
|
|
int ul_path_readf_cpuset(struct path_cxt *pc, cpu_set_t **set, int maxcpus, const char *path, ...)
|
|
|
|
__attribute__ ((__format__ (__printf__, 4, 5)));
|
2012-11-02 06:07:36 -05:00
|
|
|
|
lib/path: new implementation
The goal is to avoid duplicate code in path.c and sysfs.c and make it
possible to define prefix for paths for all sysfs and procfs based
utils. Now we have /proc snapshots (for tests) for lscpu only. It
would be nice to have the same (for sysfs) for lsblk and another tools.
* very simple API to read numbers, strings and symlinks
* based on openat()
pc = ul_new_path("/sys/block/sda");
ul_path_read_u64(pc, &size, "size");
ul_path_read_u64(pc, &lsz, "queue/logical_block_size");
* printf-like API to generate paths, for example:
ul_path_readf_u64(pc, &num, "sda%d/size", partno)
* allow to define prefix to redirect hardcoded paths to another
location, for example:
pc = ul_new_path("/sys/block/sda");
ul_path_set_prefix(pc, "/my/regression/dump");
ul_path_read_u64(pc, &num, "size");
to read /my/regression/dump/sys/block/sda/size
* allow to extend the API by "dialects", for example for sysfs:
pc = ul_new_path(NULL);
sysfs_blkdev_init_path(pc, devno, NULL);
and use ul_path_* functions to read from @pc initialized by
sysfs_blkdev_init_path()
* add test_path binary
Signed-off-by: Karel Zak <kzak@redhat.com>
2018-05-09 08:54:12 -05:00
|
|
|
int ul_path_readf_cpulist(struct path_cxt *pc, cpu_set_t **set, int maxcpus, const char *path, ...)
|
|
|
|
__attribute__ ((__format__ (__printf__, 4, 5)));
|
2012-11-02 06:07:36 -05:00
|
|
|
#endif /* HAVE_CPU_SET_T */
|
|
|
|
#endif /* UTIL_LINUX_PATH_H */
|