*/
static int pakfire_build_enable_repos(struct pakfire_build* build) {
char repopath[PATH_MAX];
- FILE* f = NULL;
int r = 1;
// Fetch repository configuration
if (r < 0)
goto ERROR;
- // Open configuration file for writing
- f = fopen(repopath, "w");
- if (!f) {
- ERROR(build->pakfire, "Could not open %s for writing: %m\n", path);
- r = 1;
- goto ERROR;
- }
-
- // Write configuration
- size_t bytes_written = fprintf(f, "%s", config);
- if (bytes_written < strlen(config)) {
+ // Write repository configuration
+ r = pakfire_file_write(build->pakfire, repopath, 0, 0, 0644, "%s", config);
+ if (r) {
ERROR(build->pakfire, "Could not write repository configuration: %m\n");
- r = 1;
goto ERROR;
}
- // Success
- r = 0;
-
ERROR:
- if (f)
- fclose(f);
if (config)
free(config);
const char* first, const char* second);
const char* pakfire_path_relpath(const char* root, const char* path);
+// File stuff
+
+int pakfire_file_write(struct pakfire* pakfire, const char* path,
+ uid_t owner, gid_t group, mode_t mode, const char* format, ...);
+
int pakfire_touch(const char* path, mode_t mode);
int pakfire_mkparentdir(const char* path, mode_t mode);
int pakfire_mkdir(const char* path, mode_t mode);
return -1;
}
+int pakfire_file_write(struct pakfire* pakfire, const char* path,
+ uid_t owner, gid_t group, mode_t mode, const char* format, ...) {
+ va_list args;
+ int r = 1;
+
+ // Open the destination file
+ FILE* f = fopen(path, "w");
+ if (!f)
+ goto ERROR;
+
+ // Fetch file-descriptor
+ int fd = fileno(f);
+
+ // Set owner/group
+ if (owner && group) {
+ r = fchown(fd, owner, group);
+ if (r)
+ goto ERROR;
+ }
+
+ // Set mode
+ if (mode) {
+ r = fchmod(fd, mode);
+ if (r)
+ goto ERROR;
+ }
+
+ // Write data
+ va_start(args, format);
+ r = vfprintf(f, format, args);
+ va_end(args);
+
+ // Check for writing error
+ if (r < 0)
+ goto ERROR;
+
+ // Close the file
+ return fclose(f);
+
+ERROR:
+ if (f)
+ fclose(f);
+
+ return r;
+}
+
char* pakfire_basename(const char* path) {
char* name = strdup(path);