app: Don't overload head_size with different meanings

The code in file_procedure_find() tries to lazy-open the image file, so
it's not opened if unnecessary. To keep track of whether the image was
opened or not, it overrides head_size to keep track of this. head_size
is also assigned the return value of fread() in the loop. The patch
separates this by introducing another variable.

I think it's best to move the file opening code outside the while loop,
as it's most likely bound to be opened anyway, and the penalty is not
too bad.
This commit is contained in:
Mukund Sivaraman
2011-10-02 18:35:26 +05:30
parent f007ce352d
commit 7d083ce457

View File

@ -113,7 +113,8 @@ file_procedure_find (GSList *procs,
{
GimpPlugInProcedure *size_matched_proc = NULL;
FILE *ifp = NULL;
gint head_size = -2;
gboolean opened = FALSE;
gint head_size = 0;
gint size_match_count = 0;
guchar head[256];
@ -124,21 +125,17 @@ file_procedure_find (GSList *procs,
if (file_proc->magics_list)
{
if (head_size == -2)
if (G_UNLIKELY (!opened))
{
head_size = 0;
if ((ifp = g_fopen (filename, "rb")) != NULL)
{
head_size = fread ((gchar *) head, 1, sizeof (head), ifp);
}
ifp = g_fopen (filename, "rb");
if (ifp != NULL)
head_size = fread ((gchar *) head, 1, sizeof (head), ifp);
else
{
g_set_error_literal (error,
G_FILE_ERROR,
g_file_error_from_errno (errno),
g_strerror (errno));
}
g_set_error_literal (error,
G_FILE_ERROR,
g_file_error_from_errno (errno),
g_strerror (errno));
opened = TRUE;
}
if (head_size >= 4)