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