Commit 5a316cdf authored by Bernhard Link's avatar Bernhard Link

adopted returnvalues for dirs.{h,c}

parent f291727f
......@@ -22,6 +22,7 @@
#include <stdio.h>
#include <malloc.h>
#include <string.h>
#include "error.h"
#include "dirs.h"
extern int verbose;
......@@ -29,42 +30,48 @@ extern int verbose;
//TODO: rewrite with retvalue return values...
/* create directory dirname. returns 0 on success or if already existing, -1 otherwise */
static int check_dir(const char *dirname) {
static retvalue dirs_check(const char *dirname) {
int ret;
ret = mkdir(dirname,0775);
if( ret == 0 ) {
if( verbose > 0)
fprintf(stderr,"Created directory \"%s\"\n",dirname);
return 0;
fprintf(stderr,"Created directory \"%s\"\n",dirname);
return RET_OK;
} else if( ret < 0 && errno != EEXIST ) {
fprintf(stderr,"Can not create directory \"%s\": %m\n",dirname);
return -1;
return RET_ERROR;
}
return 0;
return RET_NOTHING;
}
/* create recursively all parent directories before the last '/' */
int make_parent_dirs(const char *filename) {
retvalue dirs_make_parent(const char *filename) {
const char *p;
char *h;
int i;
retvalue r;
for( p = filename+1, i = 1 ; *p != '\0' ; p++,i++) {
if( *p == '/' ) {
h = strndup(filename,i);
if( check_dir(h) < 0 ) {
r = dirs_check(h);
if( RET_WAS_ERROR(r) ) {
free(h);
return -1;
return r;
}
free(h);
}
}
return 0;
return RET_OK;
}
/* create dirname and any '/'-seperated part of it */
int make_dir_recursive(const char *dirname) {
make_parent_dirs(dirname);
return check_dir(dirname);
retvalue dirs_make_recursive(const char *directory) {
retvalue r,result;
r = dirs_make_parent(directory);
result = dirs_check(directory);
RET_UPDATE(result,r);
return result;
}
#ifndef __MIRRORER_DIRS_H
#define __MIRRORER_DIRS_H
/* everything here returns -1 on error, 0 on success. */
#ifndef __MIRRORER_ERROR_H
#warning "What is happening here?"
#include "error.h"
#endif
/* create recursively all parent directories before the last '/' */
int make_parent_dirs(const char *filename);
retvalue dirs_make_parent(const char *filename);
/* create dirname and any '/'-seperated part of it */
int make_dir_recursive(const char *filename);
retvalue dirs_make_recursive(const char *directory);
#endif
......@@ -41,12 +41,14 @@ DB *files_initialize(const char *dbpath) {
DB *dbp;
int dbret;
char *filename;
retvalue r;
filename = mprintf("%s/files.db",dbpath);
if( !filename )
return NULL;
if( make_parent_dirs(filename) < 0 ) {
r = dirs_make_parent(filename);
if( RET_WAS_ERROR(r) ) {
free(filename);
return NULL;
}
......
......@@ -552,10 +552,10 @@ static retvalue showmissingsourcefiles(void *data,const char *chunk,const char *
/* look for directory */
if( (dn = calc_fullfilename(mirrordir,directory))) {
r = make_dir_recursive(dn);
r = dirs_make_recursive(dn);
free(dn);
if( r < 0 )
return RET_ERROR;
if( RET_WAS_ERROR(r) )
return r;
}
ret = r = RET_NOTHING;
......@@ -633,10 +633,12 @@ static retvalue showmissing(void *data,const char *chunk,const char *package,con
if( r == RET_NOTHING ) {
/* look for directory */
if( (fn = calc_fullfilename(mirrordir,filekey))) {
//TODO: what to do if fails?
make_parent_dirs(fn);
r = dirs_make_parent(fn);
free(fn);
}
if( RET_WAS_ERROR(r) )
return r;
} else
return RET_ERROR_OOM;
/* File missing */
printf("%s %s/%s\n",oldfile,mirrordir,filekey);
return RET_OK;
......@@ -1188,8 +1190,10 @@ static int update(int argc,char *argv[]) {
return 1;
}
// TODO: check for resturn-value
make_dir_recursive(listdir);
result = dirs_make_recursive(listdir);
if( RET_WAS_ERROR(result) ) {
return EXIT_RET(result);
}
result = updates_foreach(confdir,argc-1,argv+1,fetchupstreamlists,NULL,force);
return EXIT_RET(result);
}
......
......@@ -51,12 +51,14 @@ DB *packages_initialize(const char *dbpath,const char *dbname) {
DB *dbp;
int dbret;
char *filename;
retvalue r;
filename=mprintf("%s/packages.db",dbpath);
if( !filename )
return NULL;
if( make_parent_dirs(filename) < 0 ) {
r = dirs_make_parent(filename);
if( RET_WAS_ERROR(r) ) {
free(filename);
return NULL;
}
......
......@@ -47,12 +47,14 @@ DB *references_initialize(const char *dbpath) {
DB *dbp;
int ret;
char *filename;
retvalue r;
filename = mprintf("%s/references.db",dbpath);
if( !filename )
return NULL;
if( make_parent_dirs(filename) < 0 ) {
r = dirs_make_parent(filename);
if( RET_WAS_ERROR(r) ) {
free(filename);
return NULL;
}
......
......@@ -264,7 +264,7 @@ retvalue release_genbinary(const struct release *release,const char *arch,const
if( !filename ) {
return RET_ERROR_OOM;
}
(void)make_parent_dirs(filename);
(void)dirs_make_parent(filename);
f = fopen(filename,"w");
if( !f ) {
e = errno;
......@@ -303,7 +303,7 @@ retvalue release_gensource(const struct release *release,const char *component,c
if( !filename ) {
return RET_ERROR_OOM;
}
(void)make_parent_dirs(filename);
(void)dirs_make_parent(filename);
f = fopen(filename,"w");
if( !f ) {
e = errno;
......@@ -462,7 +462,7 @@ retvalue release_gen(const struct release *release,const char *distdir,const cha
free(dirofdist);
return RET_ERROR_OOM;
}
(void)make_parent_dirs(filename);
(void)dirs_make_parent(filename);
f = fopen(filename,"w");
if( !f ) {
e = errno;
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment