open the temporary file before forking. This allows us to return an error

2008-08-18  Sven Neumann  <sven@gimp.org>

	* plug-ins/common/file-compressor.c: open the temporary file
	before forking. This allows us to return an error message if the
	file can't be opened. Also changed the code to not use 
g_message()
	from the child process.


svn path=/trunk/; revision=26653
This commit is contained in:
Sven Neumann
2008-08-18 20:47:19 +00:00
committed by Sven Neumann
parent 7c57332c6c
commit cc60f022b1
2 changed files with 64 additions and 38 deletions

View File

@ -1,3 +1,10 @@
2008-08-18 Sven Neumann <sven@gimp.org>
* plug-ins/common/file-compressor.c: open the temporary file
before doing the fork. This allows us to return an error message
if the file can't be opened. Also changed the code to not use
g_message() from the child process.
2008-08-18 Sven Neumann <sven@gimp.org> 2008-08-18 Sven Neumann <sven@gimp.org>
* plug-ins/common/file-compressor.c: pass some error message with * plug-ins/common/file-compressor.c: pass some error message with

View File

@ -407,42 +407,50 @@ save_image (const Compressor *compressor,
#ifndef G_OS_WIN32 #ifndef G_OS_WIN32
{ {
gint pid; FILE *f;
gint pid;
f = g_fopen (filename, "wb");
if (! f)
{
g_unlink (tmpname);
g_free (tmpname);
g_set_error (error, G_FILE_ERROR, g_file_error_from_errno (errno),
_("Could not open '%s' for writing: %s"),
gimp_filename_to_utf8 (filename), g_strerror (errno));
return GIMP_PDB_EXECUTION_ERROR;
}
/* fork off a compressor process */ /* fork off a compressor process */
if ((pid = fork ()) < 0) if ((pid = fork ()) < 0)
{ {
g_message ("fork() failed: %s", g_strerror (errno)); g_message ("fork() failed: %s", g_strerror (errno));
fclose (f);
g_unlink (tmpname);
g_free (tmpname); g_free (tmpname);
return GIMP_PDB_EXECUTION_ERROR; return GIMP_PDB_EXECUTION_ERROR;
} }
else if (pid == 0) else if (pid == 0) /* child process */
{ {
FILE *f;
if (!(f = g_fopen (filename, "wb")))
{
g_message (_("Could not open '%s' for writing: %s"),
gimp_filename_to_utf8 (filename), g_strerror (errno));
g_free (tmpname);
_exit (127);
}
/* make stdout for this process be the output file */ /* make stdout for this process be the output file */
if (dup2 (fileno (f), fileno (stdout)) == -1) if (dup2 (fileno (f), fileno (stdout)) == -1)
g_message ("dup2() failed: %s", g_strerror (errno)); g_printerr ("dup2() failed: %s", g_strerror (errno));
/* and compress into it */ /* and compress into it */
execlp (compressor->save_program, execlp (compressor->save_program,
compressor->save_program, compressor->save_program,
compressor->save_options, tmpname, NULL); compressor->save_options, tmpname, NULL);
g_message ("execlp(\"%s %s\") failed: %s", g_printerr ("execlp(\"%s %s\") failed: %s",
compressor->save_program, compressor->save_program,
compressor->save_options, compressor->save_options,
g_strerror (errno)); g_strerror (errno));
g_free (tmpname);
_exit(127); _exit(127);
} }
else else
@ -450,6 +458,8 @@ save_image (const Compressor *compressor,
gint wpid; gint wpid;
gint process_status; gint process_status;
fclose (f);
wpid = waitpid (pid, &process_status, 0); wpid = waitpid (pid, &process_status, 0);
if ((wpid < 0) if ((wpid < 0)
@ -459,6 +469,8 @@ save_image (const Compressor *compressor,
g_message ("%s exited abnormally on file '%s'", g_message ("%s exited abnormally on file '%s'",
compressor->save_program, compressor->save_program,
gimp_filename_to_utf8 (tmpname)); gimp_filename_to_utf8 (tmpname));
g_unlink (tmpname);
g_free (tmpname); g_free (tmpname);
return GIMP_PDB_EXECUTION_ERROR; return GIMP_PDB_EXECUTION_ERROR;
@ -562,12 +574,28 @@ load_image (const Compressor *compressor,
#ifndef G_OS_WIN32 #ifndef G_OS_WIN32
{ {
gint pid; FILE *f;
gint pid;
f = g_fopen (tmpname, "wb");
if (! f)
{
g_set_error (error, G_FILE_ERROR, g_file_error_from_errno (errno),
_("Could not open '%s' for writing: %s"),
gimp_filename_to_utf8 (tmpname), g_strerror (errno));
g_free (tmpname);
*status = GIMP_PDB_EXECUTION_ERROR;
return -1;
}
/* fork off a compressor and wait for it */ /* fork off a compressor and wait for it */
if ((pid = fork ()) < 0) if ((pid = fork ()) < 0)
{ {
g_message ("fork() failed: %s", g_strerror (errno)); g_message ("fork() failed: %s", g_strerror (errno));
g_unlink (tmpname);
g_free (tmpname); g_free (tmpname);
*status = GIMP_PDB_EXECUTION_ERROR; *status = GIMP_PDB_EXECUTION_ERROR;
@ -575,33 +603,20 @@ load_image (const Compressor *compressor,
} }
else if (pid == 0) /* child process */ else if (pid == 0) /* child process */
{ {
FILE *f;
if (! (f = g_fopen (tmpname, "wb")))
{
g_message (_("Could not open '%s' for writing: %s"),
gimp_filename_to_utf8 (tmpname), g_strerror (errno));
g_free (tmpname);
_exit(127);
}
/* make stdout for this child process be the temp file */ /* make stdout for this child process be the temp file */
if (dup2 (fileno (f), fileno (stdout)) == -1) if (dup2 (fileno (f), fileno (stdout)) == -1)
{ g_printerr ("dup2() failed: %s", g_strerror (errno));
g_free (tmpname);
g_message ("dup2() failed: %s", g_strerror (errno));
}
/* and uncompress into it */ /* and uncompress into it */
execlp (compressor->load_program, execlp (compressor->load_program,
compressor->load_program, compressor->load_program,
compressor->load_options, filename, NULL); compressor->load_options, filename, NULL);
g_message ("execlp(\"%s %s\") failed: %s", g_printerr ("execlp(\"%s %s\") failed: %s",
compressor->load_program, compressor->load_program,
compressor->load_options, compressor->load_options,
g_strerror (errno)); g_strerror (errno));
g_free (tmpname);
_exit(127); _exit(127);
} }
else /* parent process */ else /* parent process */
@ -609,6 +624,8 @@ load_image (const Compressor *compressor,
gint wpid; gint wpid;
gint process_status; gint process_status;
fclose (f);
wpid = waitpid (pid, &process_status, 0); wpid = waitpid (pid, &process_status, 0);
if ((wpid < 0) if ((wpid < 0)
@ -618,6 +635,8 @@ load_image (const Compressor *compressor,
g_message ("%s exited abnormally on file '%s'", g_message ("%s exited abnormally on file '%s'",
compressor->load_program, compressor->load_program,
gimp_filename_to_utf8 (filename)); gimp_filename_to_utf8 (filename));
g_unlink (tmpname);
g_free (tmpname); g_free (tmpname);
*status = GIMP_PDB_EXECUTION_ERROR; *status = GIMP_PDB_EXECUTION_ERROR;