Commit da672c41 authored by Lev Walkin's avatar Lev Walkin

better error reporting when destination directory is not available

parent 4645db07
......@@ -36,7 +36,8 @@ int mkstemp(char *template) {
#endif
FILE *
asn1c_open_file(const char* destdir, const char *name, const char *ext, char **opt_tmpname) {
asn1c_open_file(const char *destdir, const char *name, const char *ext,
char **opt_tmpname) {
char fname[PATH_MAX];
int created = 1;
#ifndef _WIN32
......@@ -49,10 +50,8 @@ asn1c_open_file(const char* destdir, const char *name, const char *ext, char **o
/*
* Compute filenames.
*/
ret = snprintf(fname, sizeof(fname), "%s%s%s%s",
opt_tmpname ? "" : destdir,
name, ext,
opt_tmpname ? ".XXXXXX" : "");
ret = snprintf(fname, sizeof(fname), "%s%s%s%s", destdir ? destdir : "",
name, ext, opt_tmpname ? ".XXXXXX" : "");
assert(ret > 0 && ret < (ssize_t)sizeof(fname));
if(opt_tmpname) {
......@@ -61,8 +60,10 @@ asn1c_open_file(const char* destdir, const char *name, const char *ext, char **o
*/
fd = mkstemp(fname);
#ifndef _WIN32
if(fd != -1) {
/* fchmod() does not respect umask */
(void)fchmod(fd, REASONABLE_FILE_MODE);
}
#endif
} else {
/*
......@@ -75,9 +76,15 @@ asn1c_open_file(const char* destdir, const char *name, const char *ext, char **o
}
}
if(fd == -1) {
struct stat st;
if(destdir && stat(destdir, &st) == -1) {
fprintf(stderr, "%s: No such directory\n", destdir);
return NULL;
} else {
perror(fname);
return NULL;
}
}
#ifndef _WIN32
/*
......
......@@ -40,8 +40,10 @@ enum include_type_result {
static int asn1c_dump_streams(arg_t *arg, asn1c_dep_chainset *, const char *, int, char **);
static int asn1c_print_streams(arg_t *arg);
static int asn1c_save_streams(arg_t *arg, asn1c_dep_chainset *, const char *, int, char **);
static int asn1c_copy_over(arg_t *arg, const char *destdir, const char *path, const char *msg);
static int asn1c_save_streams(arg_t *arg, asn1c_dep_chainset *, const char *,
int, char **);
static int asn1c_copy_over(arg_t *arg, const char *destdir, const char *path,
const char *msg);
static int identical_files(const char *fname1, const char *fname2);
static int need_to_generate_pdu_collection(arg_t *arg);
static abuf *generate_pdu_collection(arg_t *arg);
......@@ -55,7 +57,8 @@ static int asn1c__pdu_type_lookup(const char *typename);
static int
asn1c__save_library_makefile(arg_t *arg, const asn1c_dep_chainset *deps,
const char *datadir, const char *destdir, const char *makefile_name) {
const char *datadir, const char *destdir,
const char *makefile_name) {
asn1p_module_t *mod;
FILE *mkf;
......@@ -80,7 +83,8 @@ asn1c__save_library_makefile(arg_t *arg, const asn1c_dep_chainset *deps,
TQ_FOR(arg->expr, &(mod->members), next) {
if(asn1_lang_map[arg->expr->meta_type]
[arg->expr->expr_type].type_cb) {
safe_fprintf(mkf, "\t\\\n\t%s%s.h", destdir,
safe_fprintf(
mkf, "\t\\\n\t%s%s.h", destdir,
asn1c_make_identifier(AMI_MASK_ONLY_SPACES, arg->expr, 0));
}
}
......@@ -131,7 +135,8 @@ asn1c__save_library_makefile(arg_t *arg, const asn1c_dep_chainset *deps,
} else {
what_kind = "SRCS";
}
safe_fprintf(mkf, "ASN_MODULE_%s+=%s%s\n", what_kind, destdir, fname);
safe_fprintf(mkf, "ASN_MODULE_%s+=%s%s\n", what_kind, destdir,
fname);
}
asn1c_dep_chain_free(dlist);
......@@ -160,8 +165,9 @@ asn1c__save_library_makefile(arg_t *arg, const asn1c_dep_chainset *deps,
}
static int
asn1c__save_example_mk_makefile(arg_t *arg, const asn1c_dep_chainset *deps, const char *datadir,
const char *destdir, const char *makefile_name,
asn1c__save_example_mk_makefile(arg_t *arg, const asn1c_dep_chainset *deps,
const char *datadir, const char *destdir,
const char *makefile_name,
const char *library_makefile_name, int argc,
char **argv) {
FILE *mkf;
......@@ -173,7 +179,8 @@ asn1c__save_example_mk_makefile(arg_t *arg, const asn1c_dep_chainset *deps, cons
perror(makefile_name);
return -1;
}
safe_fprintf(mkf,
safe_fprintf(
mkf,
"include %s%s\n\n"
"LIBS += -lm\n"
"CFLAGS += $(ASN_MODULE_CFLAGS) %s%s-I.\n"
......@@ -202,15 +209,17 @@ asn1c__save_example_mk_makefile(arg_t *arg, const asn1c_dep_chainset *deps, cons
if(need_to_generate_pdu_collection(arg)) {
safe_fprintf(mkf, "\\\n\t%spdu_collection.c", destdir);
if(generate_pdu_collection_file(arg, destdir))
if(generate_pdu_collection_file(arg, destdir)) {
return -1;
}
}
safe_fprintf(
mkf,
"\n\nall: $(ASN_PROGRAM)\n"
"\n$(ASN_PROGRAM): $(ASN_LIBRARY) $(ASN_PROGRAM_SRCS:.c=.o)"
"\n\t$(CC) $(CFLAGS) $(CPPFLAGS) -o $(ASN_PROGRAM) $(ASN_PROGRAM_SRCS:.c=.o) $(LDFLAGS) $(ASN_LIBRARY) $(LIBS)\n"
"\n\t$(CC) $(CFLAGS) $(CPPFLAGS) -o $(ASN_PROGRAM) "
"$(ASN_PROGRAM_SRCS:.c=.o) $(LDFLAGS) $(ASN_LIBRARY) $(LIBS)\n"
"\n$(ASN_LIBRARY): $(ASN_MODULE_SRCS:.c=.o)"
"\n\t$(AR) rcs $@ $(ASN_MODULE_SRCS:.c=.o)\n"
"\n.SUFFIXES:"
......@@ -244,7 +253,6 @@ asn1c__save_example_am_makefile(arg_t *arg, const asn1c_dep_chainset *deps, cons
/* Generate example.am snippet */
mkf = asn1c_open_file(destdir, makefile_name, "", 0);
if(mkf == NULL) {
perror(makefile_name);
return -1;
}
safe_fprintf(mkf,
......@@ -387,7 +395,8 @@ asn1c_save_compiled_output(arg_t *arg, const char *datadir, const char *destdir,
TQ_FOR(arg->expr, &(mod->members), next) {
if(asn1_lang_map[arg->expr->meta_type][arg->expr->expr_type]
.type_cb) {
if(asn1c_dump_streams(arg, deps, destdir, optc, argv)) break;
ret = asn1c_dump_streams(arg, deps, destdir, optc, argv);
if(ret) break;
}
}
}
......@@ -401,6 +410,8 @@ asn1c_save_compiled_output(arg_t *arg, const char *datadir, const char *destdir,
break;
}
if(ret) break;
ret = asn1c__save_library_makefile(arg, deps, datadir, destdir,
library_makefile);
if(ret) break;
......@@ -432,8 +443,8 @@ asn1c_save_compiled_output(arg_t *arg, const char *datadir, const char *destdir,
* Dump the streams.
*/
static int
asn1c_dump_streams(arg_t *arg, asn1c_dep_chainset *deps, const char* destdir, int optc,
char **argv) {
asn1c_dump_streams(arg_t *arg, asn1c_dep_chainset *deps, const char *destdir,
int optc, char **argv) {
if(arg->flags & A1C_PRINT_COMPILED) {
return asn1c_print_streams(arg);
} else {
......@@ -465,8 +476,8 @@ asn1c_print_streams(arg_t *arg) {
}
static int
asn1c_save_streams(arg_t *arg, asn1c_dep_chainset *deps, const char* destdir, int optc,
char **argv) {
asn1c_save_streams(arg_t *arg, asn1c_dep_chainset *deps, const char *destdir,
int optc, char **argv) {
asn1p_expr_t *expr = arg->expr;
compiler_streams_t *cs = expr->data;
out_chunk_t *ot;
......@@ -486,10 +497,14 @@ asn1c_save_streams(arg_t *arg, asn1c_dep_chainset *deps, const char* destdir, in
filename = strdup(asn1c_make_identifier(AMI_MASK_ONLY_SPACES, expr, (char*)0));
fp_c = asn1c_open_file(destdir, filename, ".c", &tmpname_c);
if(fp_c == NULL) {
return -1;
}
fp_h = asn1c_open_file(destdir, filename, ".h", &tmpname_h);
if(fp_c == NULL || fp_h == NULL) {
if(fp_c) { unlink(tmpname_c); free(tmpname_c); fclose(fp_c); }
if(fp_h) { unlink(tmpname_h); free(tmpname_h); fclose(fp_h); }
if(fp_h == NULL) {
unlink(tmpname_c);
free(tmpname_c);
fclose(fp_c);
return -1;
}
......@@ -690,7 +705,8 @@ real_copy(const char *src, const char *dst) {
}
static int
asn1c_copy_over(arg_t *arg, const char* destdir, const char *path, const char *msg) {
asn1c_copy_over(arg_t *arg, const char *destdir, const char *path,
const char *msg) {
#ifdef _WIN32
int use_real_copy = 1;
#else
......@@ -741,7 +757,7 @@ asn1c_copy_over(arg_t *arg, const char* destdir, const char *path, const char *m
static int
generate_pdu_collection_file(arg_t *arg, const char* destdir) {
generate_pdu_collection_file(arg_t *arg, const char *destdir) {
abuf *buf = generate_pdu_collection(arg);
assert(buf);
......
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