]> git.ipfire.org Git - people/ms/pakfire.git/blob - src/libpakfire/include/pakfire/util.h
util: Create a function to create relative symlinks
[people/ms/pakfire.git] / src / libpakfire / include / pakfire / util.h
1 /*#############################################################################
2 # #
3 # Pakfire - The IPFire package management system #
4 # Copyright (C) 2013 Pakfire development team #
5 # #
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. #
10 # #
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. #
15 # #
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/>. #
18 # #
19 #############################################################################*/
20
21 #ifndef PAKFIRE_UTIL_H
22 #define PAKFIRE_UTIL_H
23
24 #ifdef PAKFIRE_PRIVATE
25
26 #include <pwd.h>
27 #include <stdio.h>
28
29 #define PCRE2_CODE_UNIT_WIDTH 8
30 #include <pcre2.h>
31
32 #include <pakfire/ctx.h>
33 #include <pakfire/digest.h>
34 #include <pakfire/pakfire.h>
35
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);
39
40 int pakfire_path_strip_extension(char* path);
41
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);
45
46 char* pakfire_remove_trailing_newline(char* str);
47
48 int pakfire_read_file_into_buffer(FILE* f, char** buffer, size_t* len);
49
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);
54
55 int pakfire_path_is_absolute(const char* path);
56 const char* pakfire_path_relpath(const char* root, const char* path);
57
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);
61
62 // File stuff
63
64 int pakfire_file_write(struct pakfire* pakfire, const char* path,
65 uid_t owner, gid_t group, mode_t mode, const char* format, ...);
66
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);
74
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);
78
79 // UUID Stuff
80
81 int pakfire_uuid_is_valid(const char* s);
82 char* pakfire_generate_uuid(void);
83
84 int pakfire_tty_is_noninteractive(void);
85
86 // JSON Stuff
87
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);
97
98 // Resource Limits
99
100 #define PAKFIRE_RLIMIT_NOFILE_MAX (512 * 1024)
101
102 int pakfire_rlimit_set(struct pakfire* pakfire, int limit);
103 int pakfire_rlimit_reset_nofile(struct pakfire* pakfire);
104
105 // Regex
106
107 int pakfire_compile_regex(struct pakfire* pakfire, pcre2_code** regex,
108 const char* pattern);
109
110 // Base64
111
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,
115 const char* buffer);
116
117 // Copy
118 int pakfire_copy(struct pakfire* pakfire, FILE* src, FILE* dst);
119
120 // Time
121
122 static inline double pakfire_timespec_delta(struct timespec* t1, struct timespec* t2) {
123 // Compute delta in seconds
124 return (
125 ((t1->tv_sec * 1000) + (t1->tv_nsec / 1000000))
126 -
127 ((t2->tv_sec * 1000) + (t2->tv_nsec / 1000000))
128 ) / 1000.0;
129 }
130
131 #endif
132
133 #endif /* PAKFIRE_UTIL_H */