#define PAKFIRE_RLIMIT_NOFILE_MAX (512 * 1024)
-int pakfire_rlimit_set(struct pakfire* pakfire, int limit);
-int pakfire_rlimit_reset_nofile(struct pakfire* pakfire);
+int pakfire_rlimit_set(struct pakfire_ctx* ctx, int limit);
+int pakfire_rlimit_reset_nofile(struct pakfire_ctx* ctx);
// Regex
-int pakfire_compile_regex(struct pakfire* pakfire, pcre2_code** regex,
- const char* pattern);
+int pakfire_compile_regex(struct pakfire_ctx* ctx, pcre2_code** regex, const char* pattern);
// Base64
const char* buffer);
// Copy
-int pakfire_copy(struct pakfire* pakfire, FILE* src, FILE* dst);
+int pakfire_copy(struct pakfire_ctx* ctx, FILE* src, FILE* dst);
// Time
#define PCRE2_CODE_UNIT_WIDTH 8
#include <pcre2.h>
-// Enable legacy logging
-#define PAKFIRE_LEGACY_LOGGING
-
#include <pakfire/ctx.h>
#include <pakfire/constants.h>
#include <pakfire/logging.h>
// Resource Limits
-int pakfire_rlimit_set(struct pakfire* pakfire, int limit) {
+int pakfire_rlimit_set(struct pakfire_ctx* ctx, int limit) {
struct rlimit rl;
+ int r;
// Sanity check
- if (limit < 3) {
- errno = EINVAL;
- return 1;
- }
+ if (limit < 3)
+ return -EINVAL;
// Fetch current configuration
- if (getrlimit(RLIMIT_NOFILE, &rl) < 0) {
- ERROR(pakfire, "Could not read RLIMIT_NOFILE: %m\n");
- return 1;
+ r = getrlimit(RLIMIT_NOFILE, &rl);
+ if (r < 0) {
+ CTX_ERROR(ctx, "Could not read RLIMIT_NOFILE: %m\n");
+ return -errno;
}
// Do not attempt to set higher than maximum
rl.rlim_cur = limit;
// Set the new limit
- if (setrlimit(RLIMIT_NOFILE, &rl) < 0) {
- ERROR(pakfire, "Could not set RLIMIT_NOFILE to %lu: %m\n", rl.rlim_cur);
- return 1;
+ r = setrlimit(RLIMIT_NOFILE, &rl);
+ if (r < 0) {
+ CTX_ERROR(ctx, "Could not set RLIMIT_NOFILE to %lu: %m\n", rl.rlim_cur);
+ return -errno;
}
- DEBUG(pakfire, "RLIMIT_NOFILE set to %d\n", limit);
-
+ CTX_DEBUG(ctx, "RLIMIT_NOFILE set to %d\n", limit);
return 0;
}
Resets RLIMIT_NOFILE to FD_SETSIZE (e.g. 1024)
for compatibility with software that uses select()
*/
-int pakfire_rlimit_reset_nofile(struct pakfire* pakfire) {
- return pakfire_rlimit_set(pakfire, FD_SETSIZE);
+int pakfire_rlimit_reset_nofile(struct pakfire_ctx* ctx) {
+ return pakfire_rlimit_set(ctx, FD_SETSIZE);
}
// Regex
-int pakfire_compile_regex(struct pakfire* pakfire, pcre2_code** regex, const char* pattern) {
+int pakfire_compile_regex(struct pakfire_ctx* ctx, pcre2_code** regex, const char* pattern) {
int pcre2_errno;
size_t pcre2_offset;
PCRE2_UCHAR errmsg[256];
if (!*regex) {
pcre2_get_error_message(pcre2_errno, errmsg, sizeof(errmsg));
- ERROR(pakfire, "PCRE2 compilation failed for '%s' at offset %zu: %s\n",
+ CTX_ERROR(ctx, "PCRE2 compilation failed for '%s' at offset %zu: %s\n",
pattern, pcre2_offset, errmsg);
return 1;
}
pcre2_errno = pcre2_jit_compile(*regex, PCRE2_JIT_COMPLETE);
if (pcre2_errno) {
pcre2_get_error_message(pcre2_errno, errmsg, sizeof(errmsg));
- ERROR(pakfire, "Enabling JIT on '%s' failed: %s\n", pattern, errmsg);
+ CTX_ERROR(ctx, "Enabling JIT on '%s' failed: %s\n", pattern, errmsg);
return 1;
}
// Copy
-int pakfire_copy(struct pakfire* pakfire, FILE* src, FILE* dst) {
+int pakfire_copy(struct pakfire_ctx* ctx, FILE* src, FILE* dst) {
char buffer[BUFFER_SIZE];
size_t bytes_read;
size_t bytes_written;
// Check for any errors
if (ferror(src)) {
- ERROR(pakfire, "Could not read data: %m\n");
+ CTX_ERROR(ctx, "Could not read data: %m\n");
return 1;
}
// Write the data
bytes_written = fwrite(buffer, 1, bytes_read, dst);
if (bytes_written < bytes_read) {
- ERROR(pakfire, "Could not write data: %m\n");
+ CTX_ERROR(ctx, "Could not write data: %m\n");
return 1;
}
}