error handling: return errors for save functions

Note! This just returns the error (and uses "report_error()" to generate
a string that is currently printed to stderr).  Nothing actually *uses*
that error return yet, and we don't show the error string in the GUI.

Baby steps.

Signed-off-by: Linus Torvalds <torvalds@linux-foundation.org>
Signed-off-by: Dirk Hohndel <dirk@hohndel.org>
This commit is contained in:
Linus Torvalds 2014-03-14 10:11:26 -07:00 committed by Dirk Hohndel
parent 717f4ef102
commit 76d4e3d916
2 changed files with 30 additions and 24 deletions

8
dive.h
View file

@ -681,10 +681,10 @@ extern void parse_file(const char *filename, char **error);
extern void parse_csv_file(const char *filename, int time, int depth, int temp, int po2f, int cnsf, int stopdepthf, int sepidx, const char *csvtemplate, int units, char **error); extern void parse_csv_file(const char *filename, int time, int depth, int temp, int po2f, int cnsf, int stopdepthf, int sepidx, const char *csvtemplate, int units, char **error);
extern void parse_manual_file(const char *filename, int separator_index, int units, int number, int date, int time, int duration, int location, int gps, int maxdepth, int meandepth, int buddy, int notes, int weight, int tags, char **error); extern void parse_manual_file(const char *filename, int separator_index, int units, int number, int date, int time, int duration, int location, int gps, int maxdepth, int meandepth, int buddy, int notes, int weight, int tags, char **error);
extern void save_dives(const char *filename); extern int save_dives(const char *filename);
extern void save_dives_logic(const char *filename, bool select_only); extern int save_dives_logic(const char *filename, bool select_only);
extern void save_dive(FILE *f, struct dive *dive); extern int save_dive(FILE *f, struct dive *dive);
extern void export_dives_uddf(const char *filename, const bool selected); extern int export_dives_uddf(const char *filename, const bool selected);
struct git_oid; struct git_oid;
struct git_repository; struct git_repository;

View file

@ -426,12 +426,14 @@ void save_one_dive(struct membuffer *b, struct dive *dive)
put_format(b, "</dive>\n"); put_format(b, "</dive>\n");
} }
void save_dive(FILE *f, struct dive *dive) int save_dive(FILE *f, struct dive *dive)
{ {
struct membuffer buf = { 0 }; struct membuffer buf = { 0 };
save_one_dive(&buf, dive); save_one_dive(&buf, dive);
flush_buffer(&buf, f); flush_buffer(&buf, f);
/* Error handling? */
return 0;
} }
static void save_trip(struct membuffer *b, dive_trip_t *trip) static void save_trip(struct membuffer *b, dive_trip_t *trip)
@ -493,9 +495,9 @@ static void save_one_device(void *_f, const char *model, uint32_t deviceid,
#define VERSION 2 #define VERSION 2
void save_dives(const char *filename) int save_dives(const char *filename)
{ {
save_dives_logic(filename, false); return save_dives_logic(filename, false);
} }
void save_dives_buffer(struct membuffer *b, const bool select_only) void save_dives_buffer(struct membuffer *b, const bool select_only)
@ -593,33 +595,36 @@ static void try_to_backup(const char *filename)
} }
} }
void save_dives_logic(const char *filename, const bool select_only) int save_dives_logic(const char *filename, const bool select_only)
{ {
struct membuffer buf = { 0 }; struct membuffer buf = { 0 };
FILE *f; FILE *f;
void *git; void *git;
const char *branch; const char *branch;
int error;
git = is_git_repository(filename, &branch); git = is_git_repository(filename, &branch);
if (git) { if (git)
/* error returns, anybody? */ return git_save_dives(git, branch, select_only);
git_save_dives(git, branch, select_only);
return;
}
try_to_backup(filename); try_to_backup(filename);
save_dives_buffer(&buf, select_only); save_dives_buffer(&buf, select_only);
error = -1;
f = subsurface_fopen(filename, "w"); f = subsurface_fopen(filename, "w");
if (f) { if (f) {
flush_buffer(&buf, f); flush_buffer(&buf, f);
fclose(f); error = fclose(f);
} }
if (error)
report_error("Save failed (%s)", strerror(errno));
free_buffer(&buf); free_buffer(&buf);
return error;
} }
void export_dives_uddf(const char *filename, const bool selected) int export_dives_uddf(const char *filename, const bool selected)
{ {
FILE *f; FILE *f;
struct membuffer buf = { 0 }; struct membuffer buf = { 0 };
@ -628,7 +633,7 @@ void export_dives_uddf(const char *filename, const bool selected)
xmlDoc *transformed; xmlDoc *transformed;
if (!filename) if (!filename)
return; return report_error("No filename for UDDF export");
/* Save XML to file and convert it into a memory buffer */ /* Save XML to file and convert it into a memory buffer */
save_dives_buffer(&buf, selected); save_dives_buffer(&buf, selected);
@ -640,26 +645,27 @@ void export_dives_uddf(const char *filename, const bool selected)
*/ */
doc = xmlReadMemory(buf.buffer, buf.len, "divelog", NULL, 0); doc = xmlReadMemory(buf.buffer, buf.len, "divelog", NULL, 0);
free_buffer(&buf); free_buffer(&buf);
if (!doc) { if (!doc)
fprintf(stderr, "Failed to read XML memory\n"); return report_error("Failed to read XML memory");
return;
}
/* Convert to UDDF format */ /* Convert to UDDF format */
xslt = get_stylesheet("uddf-export.xslt"); xslt = get_stylesheet("uddf-export.xslt");
if (!xslt)
return report_error("Failed to open UDDF conversion stylesheet");
if (!xslt) {
fprintf(stderr, "Failed to open UDDF conversion stylesheet\n");
return;
}
transformed = xsltApplyStylesheet(xslt, doc, NULL); transformed = xsltApplyStylesheet(xslt, doc, NULL);
xsltFreeStylesheet(xslt); xsltFreeStylesheet(xslt);
xmlFreeDoc(doc); xmlFreeDoc(doc);
/* Write the transformed XML to file */ /* Write the transformed XML to file */
f = subsurface_fopen(filename, "w"); f = subsurface_fopen(filename, "w");
if (!f)
return report_error("Failed to open %s for writing (%s)", filename, strerror(errno));
xmlDocFormatDump(f, transformed, 1); xmlDocFormatDump(f, transformed, 1);
xmlFreeDoc(transformed); xmlFreeDoc(transformed);
fclose(f); fclose(f);
/* Check write errors? */
return 0;
} }