Changeset View
Changeset View
Standalone View
Standalone View
b/g10/tdbio.c
Context not available. | |||||
int | int | ||||
tdbio_set_dbname( const char *new_dbname, int create, int *r_nofile) | tdbio_set_dbname( const char *new_dbname, int create, int *r_nofile) | ||||
{ | { | ||||
char *fname; | char *fname, *p; | ||||
struct stat statbuf; | struct stat statbuf; | ||||
static int initialized = 0; | static int initialized = 0; | ||||
Context not available. | |||||
if (stat (fname, &statbuf) == 0 && statbuf.st_size > 0) | if (stat (fname, &statbuf) == 0 && statbuf.st_size > 0) | ||||
/* OK, we have the valid trustdb.gpg already. */ | /* OK, we have the valid trustdb.gpg already. */ | ||||
return 0; | return 0; | ||||
else if (!create) { | |||||
*r_nofile = 1; | |||||
return 0; | |||||
} | |||||
/* Here comes: No valid trustdb.gpg AND CREATE==1 */ | |||||
/* | |||||
* Make sure the directory exists. This should be done before | |||||
* acquiring the lock, which assumes the directory existence. | |||||
*/ | |||||
p = strrchr( fname, DIRSEP_C ); | |||||
assert(p); /* See the code above. Always, it has DIRSEP_C. */ | |||||
*p = 0; | |||||
if( access( fname, F_OK ) ) { | |||||
try_make_homedir( fname ); | |||||
if (access (fname, F_OK )) | |||||
log_fatal (_("%s: directory does not exist!\n"), p); | |||||
} | |||||
*p = DIRSEP_C; | |||||
take_write_lock (); | take_write_lock (); | ||||
/* Check the file after aquiring the lock. */ | |||||
if( access( fname, R_OK ) ) { | if( access( fname, R_OK ) ) { | ||||
FILE *fp; | |||||
TRUSTREC rec; | |||||
int rc; | |||||
mode_t oldmask; | |||||
if( errno != ENOENT ) | if( errno != ENOENT ) | ||||
log_fatal( _("can't access `%s': %s\n"), fname, strerror(errno) ); | log_fatal( _("can't access `%s': %s\n"), fname, strerror(errno) ); | ||||
if (!create) | oldmask=umask(077); | ||||
*r_nofile = 1; | if (is_secured_filename (fname)) { | ||||
else { | fp = NULL; | ||||
FILE *fp; | errno = EPERM; | ||||
TRUSTREC rec; | } | ||||
int rc; | else | ||||
char *p = strrchr( fname, DIRSEP_C ); | fp =fopen( fname, "wb" ); | ||||
mode_t oldmask; | umask(oldmask); | ||||
if( !fp ) | |||||
assert(p); | log_fatal( _("can't create `%s': %s\n"), fname, strerror(errno) ); | ||||
*p = 0; | fclose(fp); | ||||
if( access( fname, F_OK ) ) { | db_fd = open( db_name, O_RDWR | MY_O_BINARY ); | ||||
try_make_homedir( fname ); | if( db_fd == -1 ) | ||||
if (access (fname, F_OK )) | log_fatal( _("can't open `%s': %s\n"), db_name, strerror(errno) ); | ||||
log_fatal (_("%s: directory does not exist!\n"), fname); | |||||
} | rc = create_version_record (); | ||||
*p = DIRSEP_C; | if( rc ) | ||||
log_fatal( _("%s: failed to create version record: %s"), | |||||
oldmask=umask(077); | fname, g10_errstr(rc)); | ||||
if (is_secured_filename (fname)) { | /* and read again to check that we are okay */ | ||||
fp = NULL; | if( tdbio_read_record( 0, &rec, RECTYPE_VER ) ) | ||||
errno = EPERM; | log_fatal( _("%s: invalid trustdb created\n"), db_name ); | ||||
} | |||||
else | if( !opt.quiet ) | ||||
fp =fopen( fname, "wb" ); | log_info(_("%s: trustdb created\n"), db_name); | ||||
umask(oldmask); | |||||
if( !fp ) | |||||
log_fatal( _("can't create `%s': %s\n"), fname, strerror(errno) ); | |||||
fclose(fp); | |||||
db_fd = open( db_name, O_RDWR | MY_O_BINARY ); | |||||
if( db_fd == -1 ) | |||||
log_fatal( _("can't open `%s': %s\n"), db_name, strerror(errno) ); | |||||
rc = create_version_record (); | |||||
if( rc ) | |||||
log_fatal( _("%s: failed to create version record: %s"), | |||||
fname, g10_errstr(rc)); | |||||
/* and read again to check that we are okay */ | |||||
if( tdbio_read_record( 0, &rec, RECTYPE_VER ) ) | |||||
log_fatal( _("%s: invalid trustdb created\n"), db_name ); | |||||
if( !opt.quiet ) | |||||
log_info(_("%s: trustdb created\n"), db_name); | |||||
} | |||||
} | } | ||||
release_write_lock (); | release_write_lock (); | ||||
Context not available. |