summaryrefslogtreecommitdiffstats
path: root/src
diff options
context:
space:
mode:
authorJohn Smith <john.smith7545@yahoo.com>2012-11-30 02:05:35 +0000
committerJohn Smith <john.smith7545@yahoo.com>2012-11-30 02:05:35 +0000
commit91276e1763b33970684e93ef6a20cd2134a5be57 (patch)
tree253044f18ee0506fbb40902fbf47c97d48697c66 /src
parentFix for #955141: Converting clipped object to pattern produces rasterised pat... (diff)
downloadinkscape-91276e1763b33970684e93ef6a20cd2134a5be57.tar.gz
inkscape-91276e1763b33970684e93ef6a20cd2134a5be57.zip
Fix for 430301 : core dump exporting to non existing folder
(bzr r11915)
Diffstat (limited to 'src')
-rw-r--r--src/io/sys.cpp34
-rw-r--r--src/io/sys.h2
-rw-r--r--src/main.cpp82
3 files changed, 89 insertions, 29 deletions
diff --git a/src/io/sys.cpp b/src/io/sys.cpp
index 5f19ee5db..26c1993a7 100644
--- a/src/io/sys.cpp
+++ b/src/io/sys.cpp
@@ -239,6 +239,40 @@ bool Inkscape::IO::file_is_writable( char const *utf8name)
return success;
}
+/**Checks if directory of file exists, useful
+ * because inkscape doesn't create directories.*/
+bool Inkscape::IO::file_directory_exists( char const *utf8name ){
+ bool exists = true;
+
+ if ( utf8name) {
+ gchar *filename = NULL;
+ if (utf8name && !g_utf8_validate(utf8name, -1, NULL)) {
+ /* FIXME: Trying to guess whether or not a filename is already in utf8 is unreliable.
+ If any callers pass non-utf8 data (e.g. using g_get_home_dir), then change caller to
+ use simple g_file_test. Then add g_return_val_if_fail(g_utf_validate(...), false)
+ to beginning of this function. */
+ filename = g_strdup(utf8name);
+ // Looks like g_get_home_dir isn't safe.
+ //g_warning("invalid UTF-8 detected internally. HUNT IT DOWN AND KILL IT!!!");
+ } else {
+ filename = g_filename_from_utf8 ( utf8name, -1, NULL, NULL, NULL );
+ }
+ if ( filename ) {
+ gchar *dirname = g_path_get_dirname(filename);
+ exists = Inkscape::IO::file_test( dirname, G_FILE_TEST_EXISTS);
+ g_free(filename);
+ g_free(dirname);
+ filename = NULL;
+ dirname = NULL;
+ } else {
+ g_warning( "Unable to convert filename in IO:file_test" );
+ }
+ }
+
+ return exists;
+
+}
+
/** Wrapper around g_dir_open, but taking a utf8name as first argument. */
GDir *
Inkscape::IO::dir_open(gchar const *const utf8name, guint const flags, GError **const error)
diff --git a/src/io/sys.h b/src/io/sys.h
index fbfe4d4c4..78d25afa3 100644
--- a/src/io/sys.h
+++ b/src/io/sys.h
@@ -36,6 +36,8 @@ int file_open_tmp( std::string& name_used, const std::string& prefix );
bool file_test( char const *utf8name, GFileTest test );
+bool file_directory_exists( char const *utf8name );
+
bool file_is_writable( char const *utf8name);
GDir *dir_open(gchar const *utf8name, guint flags, GError **error);
diff --git a/src/main.cpp b/src/main.cpp
index be8049cf5..20330edbb 100644
--- a/src/main.cpp
+++ b/src/main.cpp
@@ -169,10 +169,10 @@ enum {
int sp_main_gui(int argc, char const **argv);
int sp_main_console(int argc, char const **argv);
-static void sp_do_export_png(SPDocument *doc);
-static void do_export_ps_pdf(SPDocument* doc, gchar const* uri, char const *mime);
+static int sp_do_export_png(SPDocument *doc);
+static int do_export_ps_pdf(SPDocument* doc, gchar const* uri, char const *mime);
#ifdef WIN32
-static void do_export_emf(SPDocument* doc, gchar const* uri, char const *mime);
+static int do_export_emf(SPDocument* doc, gchar const* uri, char const *mime);
#endif //WIN32
static void do_query_dimension (SPDocument *doc, bool extent, Geom::Dim2 const axis, const gchar *id);
static void do_query_all (SPDocument *doc);
@@ -1046,7 +1046,7 @@ static int sp_process_file_list(GSList *fl)
sp_print_document_to_file(doc, sp_global_printer);
}
if (sp_export_png || (sp_export_id && sp_export_use_hints)) {
- sp_do_export_png(doc);
+ retVal |= sp_do_export_png(doc);
}
if (sp_export_svg) {
if (sp_export_text_to_path) {
@@ -1081,17 +1081,17 @@ static int sp_process_file_list(GSList *fl)
doc->getBase(), sp_export_svg);
}
if (sp_export_ps) {
- do_export_ps_pdf(doc, sp_export_ps, "image/x-postscript");
+ retVal |= do_export_ps_pdf(doc, sp_export_ps, "image/x-postscript");
}
if (sp_export_eps) {
- do_export_ps_pdf(doc, sp_export_eps, "image/x-e-postscript");
+ retVal |= do_export_ps_pdf(doc, sp_export_eps, "image/x-e-postscript");
}
if (sp_export_pdf) {
- do_export_ps_pdf(doc, sp_export_pdf, "application/pdf");
+ retVal |= do_export_ps_pdf(doc, sp_export_pdf, "application/pdf");
}
#ifdef WIN32
if (sp_export_emf) {
- do_export_emf(doc, sp_export_emf, "image/x-emf");
+ retVal |= do_export_emf(doc, sp_export_emf, "image/x-emf");
}
#endif //WIN32
if (sp_query_all) {
@@ -1299,7 +1299,7 @@ do_query_all_recurse (SPObject *o)
}
-static void sp_do_export_png(SPDocument *doc)
+static int sp_do_export_png(SPDocument *doc)
{
Glib::ustring filename;
bool filename_from_hint = false;
@@ -1330,7 +1330,7 @@ static void sp_do_export_png(SPDocument *doc)
if (o) {
if (!SP_IS_ITEM (o)) {
g_warning("Object with id=\"%s\" is not a visible item. Nothing exported.", sp_export_id);
- return;
+ return 1;
}
items = g_slist_prepend (items, SP_ITEM(o));
@@ -1377,11 +1377,11 @@ static void sp_do_export_png(SPDocument *doc)
area = *areaMaybe;
} else {
g_warning("Unable to determine a valid bounding box. Nothing exported.");
- return;
+ return 1;
}
} else {
g_warning("Object with id=\"%s\" was not found in the document. Nothing exported.", sp_export_id);
- return;
+ return 1;
}
}
@@ -1390,7 +1390,7 @@ static void sp_do_export_png(SPDocument *doc)
gdouble x0,y0,x1,y1;
if (sscanf(sp_export_area, "%lg:%lg:%lg:%lg", &x0, &y0, &x1, &y1) != 4) {
g_warning("Cannot parse export area '%s'; use 'x0:y0:x1:y1'. Nothing exported.", sp_export_area);
- return;
+ return 1;
}
area = Geom::Rect(Geom::Interval(x0,x1), Geom::Interval(y0,y1));
} else if (sp_export_area_page || !(sp_export_id || sp_export_area_drawing)) {
@@ -1404,7 +1404,7 @@ static void sp_do_export_png(SPDocument *doc)
if (filename.empty()) {
if (!sp_export_png) {
g_warning ("No export filename given and no filename hint. Nothing exported.");
- return;
+ return 1;
}
filename = sp_export_png;
}
@@ -1413,7 +1413,7 @@ static void sp_do_export_png(SPDocument *doc)
dpi = atof(sp_export_dpi);
if ((dpi < 0.1) || (dpi > 10000.0)) {
g_warning("DPI value %s out of range [0.1 - 10000.0]. Nothing exported.", sp_export_dpi);
- return;
+ return 1;
}
g_print("DPI: %g\n", dpi);
}
@@ -1435,7 +1435,7 @@ static void sp_do_export_png(SPDocument *doc)
width = strtoul(sp_export_width, NULL, 0);
if ((width < 1) || (width > PNG_UINT_31_MAX) || (errno == ERANGE) ) {
g_warning("Export width %lu out of range (1 - %lu). Nothing exported.", width, (unsigned long int)PNG_UINT_31_MAX);
- return;
+ return 1;
}
dpi = (gdouble) width * PX_PER_IN / area.width();
}
@@ -1445,7 +1445,7 @@ static void sp_do_export_png(SPDocument *doc)
height = strtoul(sp_export_height, NULL, 0);
if ((height < 1) || (height > PNG_UINT_31_MAX)) {
g_warning("Export height %lu out of range (1 - %lu). Nothing exported.", height, (unsigned long int)PNG_UINT_31_MAX);
- return;
+ return 1;
}
dpi = (gdouble) height * PX_PER_IN / area.height();
}
@@ -1508,19 +1508,28 @@ static void sp_do_export_png(SPDocument *doc)
path = filename;
}
- g_print("Background RRGGBBAA: %08x\n", bgcolor);
+ int retcode = 0;
+ //check if specified directory exists
- g_print("Area %g:%g:%g:%g exported to %lu x %lu pixels (%g dpi)\n", area[Geom::X][0], area[Geom::Y][0], area[Geom::X][1], area[Geom::Y][1], width, height, dpi);
+ if (!Inkscape::IO::file_directory_exists(filename.c_str())) {
+ g_warning("File path \"%s\" includes directory that doesn't exist.\n", filename.c_str());
+ retcode = 1;
+ } else {
+ g_print("Background RRGGBBAA: %08x\n", bgcolor);
- g_print("Bitmap saved as: %s\n", filename.c_str());
+ g_print("Area %g:%g:%g:%g exported to %lu x %lu pixels (%g dpi)\n", area[Geom::X][0], area[Geom::Y][0], area[Geom::X][1], area[Geom::Y][1], width, height, dpi);
- if ((width >= 1) && (height >= 1) && (width <= PNG_UINT_31_MAX) && (height <= PNG_UINT_31_MAX)) {
- sp_export_png_file(doc, path.c_str(), area, width, height, dpi, dpi, bgcolor, NULL, NULL, true, sp_export_id_only ? items : NULL);
- } else {
- g_warning("Calculated bitmap dimensions %lu %lu are out of range (1 - %lu). Nothing exported.", width, height, (unsigned long int)PNG_UINT_31_MAX);
+ g_print("Bitmap saved as: %s\n", filename.c_str());
+
+ if ((width >= 1) && (height >= 1) && (width <= PNG_UINT_31_MAX) && (height <= PNG_UINT_31_MAX)) {
+ sp_export_png_file(doc, path.c_str(), area, width, height, dpi, dpi, bgcolor, NULL, NULL, true, sp_export_id_only ? items : NULL);
+ } else {
+ g_warning("Calculated bitmap dimensions %lu %lu are out of range (1 - %lu). Nothing exported.", width, height, (unsigned long int)PNG_UINT_31_MAX);
+ }
}
g_slist_free (items);
+ return retcode;
}
@@ -1532,7 +1541,7 @@ static void sp_do_export_png(SPDocument *doc)
* \param mime MIME type to export as.
*/
-static void do_export_ps_pdf(SPDocument* doc, gchar const* uri, char const* mime)
+static int do_export_ps_pdf(SPDocument* doc, gchar const* uri, char const* mime)
{
Inkscape::Extension::DB::OutputList o;
Inkscape::Extension::db.get_output_list(o);
@@ -1544,14 +1553,14 @@ static void do_export_ps_pdf(SPDocument* doc, gchar const* uri, char const* mime
if (i == o.end())
{
g_warning ("Could not find an extension to export to MIME type %s.", mime);
- return;
+ return 1;
}
if (sp_export_id) {
SPObject *o = doc->getObjectById(sp_export_id);
if (o == NULL) {
g_warning("Object with id=\"%s\" was not found in the document. Nothing exported.", sp_export_id);
- return;
+ return 1;
}
(*i)->set_param_string ("exportId", sp_export_id);
} else {
@@ -1612,7 +1621,14 @@ static void do_export_ps_pdf(SPDocument* doc, gchar const* uri, char const* mime
(*i)->set_param_int("resolution", (int) dpi);
}
+ //check if specified directory exists
+ if (!Inkscape::IO::file_directory_exists(uri)) {
+ g_warning("File path \"%s\" includes directory that doesn't exist.\n", uri);
+ return 1;
+ }
+
(*i)->save(doc, uri);
+ return 0;
}
#ifdef WIN32
@@ -1624,7 +1640,7 @@ static void do_export_ps_pdf(SPDocument* doc, gchar const* uri, char const* mime
* \param mime MIME type to export as (should be "image/x-emf")
*/
-static void do_export_emf(SPDocument* doc, gchar const* uri, char const* mime)
+static int do_export_emf(SPDocument* doc, gchar const* uri, char const* mime)
{
Inkscape::Extension::DB::OutputList o;
Inkscape::Extension::db.get_output_list(o);
@@ -1636,10 +1652,18 @@ static void do_export_emf(SPDocument* doc, gchar const* uri, char const* mime)
if (i == o.end())
{
g_warning ("Could not find an extension to export to MIME type %s.", mime);
- return;
+ return 1;
+ }
+
+ //check if specified directory exists
+ if (!Inkscape::IO::file_directory_exists(uri)){
+ g_warning("File path \"%s\" includes directory that doesn't exist.\n",
+ uri);
+ return 1;
}
(*i)->save(doc, uri);
+ return 0;
}
#endif //WIN32