]>
git.ipfire.org Git - people/ms/pakfire.git/blob - src/libpakfire/include/pakfire/util.h
1 /*#############################################################################
3 # Pakfire - The IPFire package management system #
4 # Copyright (C) 2013 Pakfire development team #
6 # This program is free software: you can redistribute it and/or modify #
7 # it under the terms of the GNU General Public License as published by #
8 # the Free Software Foundation, either version 3 of the License, or #
9 # (at your option) any later version. #
11 # This program is distributed in the hope that it will be useful, #
12 # but WITHOUT ANY WARRANTY; without even the implied warranty of #
13 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the #
14 # GNU General Public License for more details. #
16 # You should have received a copy of the GNU General Public License #
17 # along with this program. If not, see <http://www.gnu.org/licenses/>. #
19 #############################################################################*/
21 #ifndef PAKFIRE_UTIL_H
22 #define PAKFIRE_UTIL_H
24 #ifdef PAKFIRE_PRIVATE
29 #define PCRE2_CODE_UNIT_WIDTH 8
32 #include <pakfire/ctx.h>
33 #include <pakfire/digest.h>
34 #include <pakfire/pakfire.h>
36 int pakfire_path_exists(const char* path
);
37 int pakfire_path_match(const char* p
, const char* s
);
38 time_t pakfire_path_age(const char* path
);
40 int pakfire_path_strip_extension(char* path
);
42 #define pakfire_path_replace_extension(path, extension) \
43 __pakfire_path_replace_extension(path, sizeof(path), extension)
44 int __pakfire_path_replace_extension(char* path
, const size_t length
, const char* extension
);
46 char* pakfire_remove_trailing_newline(char* str
);
48 int pakfire_read_file_into_buffer(FILE* f
, char** buffer
, size_t* len
);
50 #define pakfire_hexlify(digest) __pakfire_hexlify(digest, sizeof(digest))
51 char* __pakfire_hexlify(const unsigned char* digest
, const size_t length
);
52 #define pakfire_unhexlify(dst, src) __pakfire_unhexlify(dst, sizeof(dst), src)
53 int __pakfire_unhexlify(unsigned char* dst
, const size_t l
, const char* src
);
55 int pakfire_path_is_absolute(const char* path
);
56 const char* pakfire_path_relpath(const char* root
, const char* path
);
58 #define pakfire_path_realpath(dest, path) \
59 __pakfire_path_realpath(dest, sizeof(dest), path)
60 int __pakfire_path_realpath(char* dest
, const size_t length
, const char* path
);
64 int pakfire_file_write(struct pakfire
* pakfire
, const char* path
,
65 uid_t owner
, gid_t group
, mode_t mode
, const char* format
, ...);
67 int pakfire_touch(const char* path
, mode_t mode
);
68 int pakfire_mkparentdir(const char* path
, mode_t mode
);
69 int pakfire_mkdir(const char* path
, mode_t mode
);
70 FILE* pakfire_mktemp(char* path
, const mode_t mode
);
71 char* pakfire_mkdtemp(char* path
);
72 int pakfire_symlink(struct pakfire_ctx
* ctx
, const char* target
, const char* linkpath
);
73 int pakfire_rmtree(const char* path
, int flags
);
75 #define pakfire_which(pakfire, path, what) \
76 __pakfire_which(pakfire, path, sizeof(path), what)
77 int __pakfire_which(struct pakfire
* pakfire
, char* path
, const size_t length
, const char* what
);
81 int pakfire_uuid_is_valid(const char* s
);
82 char* pakfire_generate_uuid(void);
84 int pakfire_tty_is_noninteractive(void);
88 struct json_object
* pakfire_json_parse(struct pakfire_ctx
* ctx
,
89 const char* buffer
, const size_t length
);
90 struct json_object
* pakfire_json_parse_from_file(struct pakfire_ctx
* ctx
, const char* path
);
91 int pakfire_json_add_string(struct pakfire
* pakfire
,
92 struct json_object
* json
, const char* name
, const char* value
);
93 int pakfire_json_add_integer(struct pakfire
* pakfire
,
94 struct json_object
* json
, const char* name
, int64_t value
);
95 int pakfire_json_add_string_array(struct pakfire
* pakfire
,
96 struct json_object
* json
, const char* name
, char** array
);
100 #define PAKFIRE_RLIMIT_NOFILE_MAX (512 * 1024)
102 int pakfire_rlimit_set(struct pakfire
* pakfire
, int limit
);
103 int pakfire_rlimit_reset_nofile(struct pakfire
* pakfire
);
107 int pakfire_compile_regex(struct pakfire
* pakfire
, pcre2_code
** regex
,
108 const char* pattern
);
112 int pakfire_b64encode(struct pakfire_ctx
* ctx
, char** output
,
113 const void* buffer
, const size_t length
);
114 int pakfire_b64decode(struct pakfire_ctx
* ctx
, void** output
, size_t* length
,
118 int pakfire_copy(struct pakfire
* pakfire
, FILE* src
, FILE* dst
);
122 static inline double pakfire_timespec_delta(struct timespec
* t1
, struct timespec
* t2
) {
123 // Compute delta in seconds
125 ((t1
->tv_sec
* 1000) + (t1
->tv_nsec
/ 1000000))
127 ((t2
->tv_sec
* 1000) + (t2
->tv_nsec
/ 1000000))
133 #endif /* PAKFIRE_UTIL_H */