2006-12-10 05:33:38 +08:00
|
|
|
/* GIMP - The GNU Image Manipulation Program
|
2005-01-25 06:41:24 +08:00
|
|
|
* Copyright (C) 1995-1997 Spencer Kimball and Peter Mattis
|
|
|
|
*
|
|
|
|
* File Utitility functions for GimpConfig.
|
|
|
|
* Copyright (C) 2001-2003 Sven Neumann <sven@gimp.org>
|
|
|
|
*
|
2009-01-18 06:28:01 +08:00
|
|
|
* This program is free software: you can redistribute it and/or modify
|
2005-01-25 06:41:24 +08:00
|
|
|
* it under the terms of the GNU General Public License as published by
|
2009-01-18 06:28:01 +08:00
|
|
|
* the Free Software Foundation; either version 3 of the License, or
|
2005-01-25 06:41:24 +08:00
|
|
|
* (at your option) any later version.
|
|
|
|
*
|
|
|
|
* This program is distributed in the hope that it will be useful,
|
|
|
|
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
|
* GNU General Public License for more details.
|
|
|
|
*
|
|
|
|
* You should have received a copy of the GNU General Public License
|
2009-01-18 06:28:01 +08:00
|
|
|
* along with this program. If not, see <http://www.gnu.org/licenses/>.
|
2005-01-25 06:41:24 +08:00
|
|
|
*/
|
|
|
|
|
|
|
|
#include "config.h"
|
|
|
|
|
|
|
|
#include <errno.h>
|
|
|
|
#include <sys/types.h>
|
|
|
|
|
2013-10-15 07:58:39 +08:00
|
|
|
#include <gio/gio.h>
|
2005-02-07 09:24:22 +08:00
|
|
|
#include <glib/gstdio.h>
|
2005-01-25 06:41:24 +08:00
|
|
|
|
|
|
|
#include "libgimpbase/gimpbase.h"
|
2005-01-26 03:11:26 +08:00
|
|
|
#include "libgimpconfig/gimpconfig.h"
|
2005-01-25 06:41:24 +08:00
|
|
|
|
|
|
|
#ifdef G_OS_WIN32
|
|
|
|
#include "libgimpbase/gimpwin32-io.h"
|
|
|
|
#endif
|
|
|
|
|
|
|
|
#include "config-types.h"
|
|
|
|
|
2006-10-30 18:13:06 +08:00
|
|
|
#include "gimpconfig-file.h"
|
|
|
|
|
2005-01-25 06:41:24 +08:00
|
|
|
#include "gimp-intl.h"
|
|
|
|
|
|
|
|
|
|
|
|
gboolean
|
2014-01-01 23:32:27 +08:00
|
|
|
gimp_config_file_copy (const gchar *source,
|
|
|
|
const gchar *dest,
|
|
|
|
const gchar *old_options_pattern,
|
|
|
|
GRegexEvalCallback update_callback,
|
|
|
|
GError **error)
|
2005-01-25 06:41:24 +08:00
|
|
|
{
|
|
|
|
gchar buffer[8192];
|
|
|
|
FILE *sfile;
|
|
|
|
FILE *dfile;
|
|
|
|
struct stat stat_buf;
|
|
|
|
gint nbytes;
|
2012-12-12 14:30:31 +08:00
|
|
|
gint unwritten_len = 0;
|
|
|
|
GRegex *old_options_regexp = NULL;
|
|
|
|
|
2012-12-12 18:29:00 +08:00
|
|
|
if (old_options_pattern && update_callback)
|
|
|
|
{
|
|
|
|
old_options_regexp = g_regex_new (old_options_pattern, 0, 0, error);
|
|
|
|
|
|
|
|
/* error set by g_regex_new. */
|
|
|
|
if (! old_options_regexp)
|
|
|
|
return FALSE;
|
|
|
|
}
|
2005-01-25 06:41:24 +08:00
|
|
|
|
2005-02-07 09:38:18 +08:00
|
|
|
sfile = g_fopen (source, "rb");
|
2005-01-25 06:41:24 +08:00
|
|
|
if (sfile == NULL)
|
|
|
|
{
|
|
|
|
g_set_error (error, G_FILE_ERROR, g_file_error_from_errno (errno),
|
|
|
|
_("Could not open '%s' for reading: %s"),
|
|
|
|
gimp_filename_to_utf8 (source), g_strerror (errno));
|
2012-12-12 14:30:31 +08:00
|
|
|
if (old_options_regexp)
|
|
|
|
g_regex_unref (old_options_regexp);
|
2005-01-25 06:41:24 +08:00
|
|
|
return FALSE;
|
|
|
|
}
|
|
|
|
|
2005-02-07 09:38:18 +08:00
|
|
|
dfile = g_fopen (dest, "wb");
|
2005-01-25 06:41:24 +08:00
|
|
|
if (dfile == NULL)
|
|
|
|
{
|
|
|
|
g_set_error (error, G_FILE_ERROR, g_file_error_from_errno (errno),
|
|
|
|
_("Could not open '%s' for writing: %s"),
|
|
|
|
gimp_filename_to_utf8 (dest), g_strerror (errno));
|
|
|
|
fclose (sfile);
|
2012-12-12 14:30:31 +08:00
|
|
|
if (old_options_regexp)
|
|
|
|
g_regex_unref (old_options_regexp);
|
2005-01-25 06:41:24 +08:00
|
|
|
return FALSE;
|
|
|
|
}
|
|
|
|
|
2012-12-12 18:29:00 +08:00
|
|
|
while ((nbytes = fread (buffer + unwritten_len, 1,
|
|
|
|
sizeof (buffer) - unwritten_len, sfile)) > 0 || unwritten_len)
|
2005-01-25 06:41:24 +08:00
|
|
|
{
|
2012-12-12 14:30:31 +08:00
|
|
|
size_t read_len = nbytes + unwritten_len;
|
|
|
|
size_t write_len;
|
2013-01-26 21:05:46 +08:00
|
|
|
gchar* eol = NULL;
|
2012-12-12 14:30:31 +08:00
|
|
|
gchar* write_bytes = NULL;
|
|
|
|
|
|
|
|
if (old_options_regexp && update_callback)
|
|
|
|
{
|
2013-01-26 21:05:46 +08:00
|
|
|
eol = g_strrstr_len (buffer, read_len, "\n");
|
|
|
|
if (eol)
|
|
|
|
{
|
|
|
|
*eol = '\0';
|
|
|
|
read_len = strlen (buffer) + 1;
|
|
|
|
*eol++ = '\n';
|
|
|
|
}
|
|
|
|
else if (! feof (sfile))
|
|
|
|
{
|
2014-01-01 23:32:27 +08:00
|
|
|
gchar format[256];
|
|
|
|
|
2013-01-26 21:05:46 +08:00
|
|
|
/* We are in unlikely case where a single config line is
|
|
|
|
* longer than the buffer!
|
|
|
|
*/
|
2014-01-01 23:32:27 +08:00
|
|
|
|
|
|
|
g_snprintf (format, sizeof (format),
|
|
|
|
_("Error parsing '%%s': line longer than %s characters."),
|
|
|
|
G_GINT64_FORMAT);
|
|
|
|
|
2013-01-26 21:05:46 +08:00
|
|
|
g_set_error (error, GIMP_CONFIG_ERROR, GIMP_CONFIG_ERROR_PARSE,
|
2014-01-01 23:32:27 +08:00
|
|
|
format,
|
|
|
|
gimp_filename_to_utf8 (source),
|
|
|
|
(gint64) sizeof (buffer));
|
|
|
|
|
2013-01-26 21:05:46 +08:00
|
|
|
fclose (sfile);
|
|
|
|
fclose (dfile);
|
|
|
|
g_regex_unref (old_options_regexp);
|
|
|
|
return FALSE;
|
|
|
|
}
|
|
|
|
|
2012-12-12 18:29:00 +08:00
|
|
|
write_bytes = g_regex_replace_eval (old_options_regexp, buffer,
|
|
|
|
read_len, 0, 0, update_callback,
|
|
|
|
NULL, error);
|
2012-12-12 14:30:31 +08:00
|
|
|
if (write_bytes == NULL)
|
|
|
|
{
|
|
|
|
/* error already set. */
|
|
|
|
fclose (sfile);
|
|
|
|
fclose (dfile);
|
|
|
|
g_regex_unref (old_options_regexp);
|
|
|
|
return FALSE;
|
|
|
|
}
|
|
|
|
write_len = strlen (write_bytes);
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
write_bytes = buffer;
|
|
|
|
write_len = read_len;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (fwrite (write_bytes, 1, write_len, dfile) < write_len)
|
2006-04-12 20:49:29 +08:00
|
|
|
{
|
|
|
|
g_set_error (error, G_FILE_ERROR, g_file_error_from_errno (errno),
|
2006-08-10 20:04:35 +08:00
|
|
|
_("Error writing '%s': %s"),
|
2005-01-25 06:41:24 +08:00
|
|
|
gimp_filename_to_utf8 (dest), g_strerror (errno));
|
2012-12-12 14:30:31 +08:00
|
|
|
if (old_options_regexp && update_callback)
|
|
|
|
{
|
|
|
|
g_free (write_bytes);
|
|
|
|
g_regex_unref (old_options_regexp);
|
|
|
|
}
|
2006-04-12 20:49:29 +08:00
|
|
|
fclose (sfile);
|
|
|
|
fclose (dfile);
|
|
|
|
return FALSE;
|
|
|
|
}
|
2012-12-12 14:30:31 +08:00
|
|
|
|
|
|
|
if (old_options_regexp && update_callback)
|
|
|
|
{
|
|
|
|
g_free (write_bytes);
|
|
|
|
|
2013-01-26 21:05:46 +08:00
|
|
|
if (eol)
|
|
|
|
{
|
|
|
|
unwritten_len = nbytes + unwritten_len - read_len;
|
|
|
|
memmove (buffer, eol, unwritten_len);
|
|
|
|
}
|
|
|
|
else
|
|
|
|
/* EOF */
|
|
|
|
break;
|
2012-12-12 14:30:31 +08:00
|
|
|
}
|
2005-01-25 06:41:24 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
if (ferror (sfile))
|
|
|
|
{
|
|
|
|
g_set_error (error, G_FILE_ERROR, g_file_error_from_errno (errno),
|
2006-08-10 20:04:35 +08:00
|
|
|
_("Error reading '%s': %s"),
|
2005-01-25 06:41:24 +08:00
|
|
|
gimp_filename_to_utf8 (source), g_strerror (errno));
|
|
|
|
fclose (sfile);
|
|
|
|
fclose (dfile);
|
2012-12-12 14:30:31 +08:00
|
|
|
if (old_options_regexp)
|
|
|
|
g_regex_unref (old_options_regexp);
|
2005-01-25 06:41:24 +08:00
|
|
|
return FALSE;
|
|
|
|
}
|
|
|
|
|
|
|
|
fclose (sfile);
|
|
|
|
|
|
|
|
if (fclose (dfile) == EOF)
|
|
|
|
{
|
|
|
|
g_set_error (error, G_FILE_ERROR, g_file_error_from_errno (errno),
|
2006-08-10 20:04:35 +08:00
|
|
|
_("Error writing '%s': %s"),
|
2005-01-25 06:41:24 +08:00
|
|
|
gimp_filename_to_utf8 (dest), g_strerror (errno));
|
2012-12-12 14:30:31 +08:00
|
|
|
if (old_options_regexp)
|
|
|
|
g_regex_unref (old_options_regexp);
|
2005-01-25 06:41:24 +08:00
|
|
|
return FALSE;
|
|
|
|
}
|
|
|
|
|
2005-02-07 09:24:22 +08:00
|
|
|
if (g_stat (source, &stat_buf) == 0)
|
2005-01-25 06:41:24 +08:00
|
|
|
{
|
2005-09-26 07:22:05 +08:00
|
|
|
g_chmod (dest, stat_buf.st_mode);
|
2005-01-25 06:41:24 +08:00
|
|
|
}
|
|
|
|
|
2012-12-12 14:30:31 +08:00
|
|
|
if (old_options_regexp)
|
|
|
|
g_regex_unref (old_options_regexp);
|
2005-01-25 06:41:24 +08:00
|
|
|
return TRUE;
|
|
|
|
}
|
|
|
|
|
|
|
|
gboolean
|
2014-07-02 02:02:13 +08:00
|
|
|
gimp_config_file_backup_on_error (GFile *file,
|
2005-01-25 06:41:24 +08:00
|
|
|
const gchar *name,
|
|
|
|
GError **error)
|
|
|
|
{
|
2014-07-02 02:02:13 +08:00
|
|
|
gchar *path;
|
2005-01-25 06:41:24 +08:00
|
|
|
gchar *backup;
|
|
|
|
gboolean success;
|
|
|
|
|
2014-07-02 02:02:13 +08:00
|
|
|
g_return_val_if_fail (G_IS_FILE (file), FALSE);
|
2005-01-25 06:41:24 +08:00
|
|
|
g_return_val_if_fail (name != NULL, FALSE);
|
|
|
|
g_return_val_if_fail (error == NULL || *error == NULL, FALSE);
|
|
|
|
|
2014-07-02 02:02:13 +08:00
|
|
|
path = g_file_get_path (file);
|
|
|
|
backup = g_strconcat (path, "~", NULL);
|
2005-01-25 06:41:24 +08:00
|
|
|
|
2014-07-02 02:02:13 +08:00
|
|
|
success = gimp_config_file_copy (path, backup, NULL, NULL, error);
|
2005-01-25 06:41:24 +08:00
|
|
|
|
|
|
|
if (success)
|
|
|
|
g_message (_("There was an error parsing your '%s' file. "
|
|
|
|
"Default values will be used. A backup of your "
|
|
|
|
"configuration has been created at '%s'."),
|
|
|
|
name, gimp_filename_to_utf8 (backup));
|
|
|
|
|
|
|
|
g_free (backup);
|
2014-07-02 02:02:13 +08:00
|
|
|
g_free (path);
|
2005-01-25 06:41:24 +08:00
|
|
|
|
|
|
|
return success;
|
|
|
|
}
|