1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268
|
/* GIMP - The GNU Image Manipulation Program
* Copyright (C) 1995-1997 Spencer Kimball and Peter Mattis
*
* File Utitility functions for GimpConfig.
* Copyright (C) 2001-2003 Sven Neumann <sven@gimp.org>
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation; either version 3 of the License, or
* (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
* along with this program. If not, see <https://www.gnu.org/licenses/>.
*/
#include "config.h"
#include <errno.h>
#include <sys/types.h>
#include <gio/gio.h>
#include <glib/gstdio.h>
#include "libgimpbase/gimpbase.h"
#include "libgimpconfig/gimpconfig.h"
#ifdef G_OS_WIN32
#include "libgimpbase/gimpwin32-io.h"
#endif
#include "config-types.h"
#include "gimpconfig-file.h"
#include "gimp-intl.h"
gboolean
gimp_config_file_copy (const gchar *source,
const gchar *dest,
const gchar *old_options_pattern,
GRegexEvalCallback update_callback,
GimpCopyPostProcess post_process_callback,
gpointer user_data,
GError **error)
{
gchar buffer[8192];
FILE *sfile;
FILE *dfile;
GStatBuf stat_buf;
gint nbytes;
gint unwritten_len = 0;
GRegex *old_options_regexp = NULL;
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;
}
sfile = g_fopen (source, "rb");
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));
if (old_options_regexp)
g_regex_unref (old_options_regexp);
return FALSE;
}
dfile = g_fopen (dest, "wb");
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);
if (old_options_regexp)
g_regex_unref (old_options_regexp);
return FALSE;
}
while ((nbytes = fread (buffer + unwritten_len, 1,
sizeof (buffer) - unwritten_len, sfile)) > 0 || unwritten_len)
{
size_t read_len = nbytes + unwritten_len;
size_t write_len;
gchar* eol = NULL;
gchar* write_bytes = NULL;
if (old_options_regexp && update_callback)
{
eol = g_strrstr_len (buffer, read_len, "\n");
if (eol)
{
*eol = '\0';
read_len = strlen (buffer) + 1;
*eol++ = '\n';
}
else if (! feof (sfile))
{
gchar format[256];
/* We are in unlikely case where a single config line is
* longer than the buffer!
*/
g_snprintf (format, sizeof (format),
_("Error parsing '%%s': line longer than %s characters."),
G_GINT64_FORMAT);
g_set_error (error, GIMP_CONFIG_ERROR, GIMP_CONFIG_ERROR_PARSE,
format,
gimp_filename_to_utf8 (source),
(gint64) sizeof (buffer));
fclose (sfile);
fclose (dfile);
g_regex_unref (old_options_regexp);
return FALSE;
}
write_bytes = g_regex_replace_eval (old_options_regexp, buffer,
read_len, 0, 0, update_callback,
user_data, error);
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)
{
g_set_error (error, G_FILE_ERROR, g_file_error_from_errno (errno),
_("Error writing '%s': %s"),
gimp_filename_to_utf8 (dest), g_strerror (errno));
if (old_options_regexp && update_callback)
{
g_free (write_bytes);
g_regex_unref (old_options_regexp);
}
fclose (sfile);
fclose (dfile);
return FALSE;
}
if (old_options_regexp && update_callback)
{
g_free (write_bytes);
if (eol)
{
unwritten_len = nbytes + unwritten_len - read_len;
memmove (buffer, eol, unwritten_len);
}
else
/* EOF */
break;
}
}
if (ferror (sfile))
{
g_set_error (error, G_FILE_ERROR, g_file_error_from_errno (errno),
_("Error reading '%s': %s"),
gimp_filename_to_utf8 (source), g_strerror (errno));
fclose (sfile);
fclose (dfile);
if (old_options_regexp)
g_regex_unref (old_options_regexp);
return FALSE;
}
fclose (sfile);
if (post_process_callback)
{
gchar* write_bytes;
size_t write_len;
write_bytes = post_process_callback (user_data);
write_len = strlen (write_bytes);
if (fwrite (write_bytes, 1, write_len, dfile) < write_len)
{
g_set_error (error, G_FILE_ERROR, g_file_error_from_errno (errno),
_("Error writing '%s': %s"),
gimp_filename_to_utf8 (dest), g_strerror (errno));
if (old_options_regexp)
g_regex_unref (old_options_regexp);
g_free (write_bytes);
fclose (dfile);
return FALSE;
}
g_free (write_bytes);
}
if (fclose (dfile) == EOF)
{
g_set_error (error, G_FILE_ERROR, g_file_error_from_errno (errno),
_("Error writing '%s': %s"),
gimp_filename_to_utf8 (dest), g_strerror (errno));
if (old_options_regexp)
g_regex_unref (old_options_regexp);
return FALSE;
}
if (g_stat (source, &stat_buf) == 0)
{
g_chmod (dest, stat_buf.st_mode);
}
if (old_options_regexp)
g_regex_unref (old_options_regexp);
return TRUE;
}
gboolean
gimp_config_file_backup_on_error (GFile *file,
const gchar *name,
GError **error)
{
gchar *path;
gchar *backup;
gboolean success;
g_return_val_if_fail (G_IS_FILE (file), FALSE);
g_return_val_if_fail (name != NULL, FALSE);
g_return_val_if_fail (error == NULL || *error == NULL, FALSE);
path = g_file_get_path (file);
backup = g_strconcat (path, "~", NULL);
success = gimp_config_file_copy (path, backup, NULL, NULL, NULL, NULL, error);
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);
g_free (path);
return success;
}
|