Mercurial > pidgin
comparison src/util.c @ 10415:5b7a74d397cc
[gaim-migrate @ 11665]
Ability to save statuses.
committer: Tailor Script <tailor@pidgin.im>
author | Mark Doliner <mark@kingant.net> |
---|---|
date | Sat, 25 Dec 2004 19:54:24 +0000 |
parents | 26eac2362c32 |
children | 9903182f2aac |
comparison
equal
deleted
inserted
replaced
10414:26eac2362c32 | 10415:5b7a74d397cc |
---|---|
1933 * This function is long and beautiful, like my--um, yeah. Anyway, | 1933 * This function is long and beautiful, like my--um, yeah. Anyway, |
1934 * it includes lots of error checking so as we don't overwrite | 1934 * it includes lots of error checking so as we don't overwrite |
1935 * people's settings if there is a problem writing the new values. | 1935 * people's settings if there is a problem writing the new values. |
1936 */ | 1936 */ |
1937 gboolean | 1937 gboolean |
1938 gaim_util_write_xml_file(const char *filename, const char *data) | 1938 gaim_util_write_data_to_file(const char *filename, const char *data, size_t size) |
1939 { | 1939 { |
1940 const char *user_dir = gaim_user_dir(); | 1940 const char *user_dir = gaim_user_dir(); |
1941 gchar *filename_temp, *filename_full; | 1941 gchar *filename_temp, *filename_full; |
1942 FILE *file; | 1942 FILE *file; |
1943 size_t datalen, byteswritten; | 1943 size_t real_size, byteswritten; |
1944 struct stat st; | 1944 struct stat st; |
1945 | 1945 |
1946 g_return_val_if_fail(user_dir != NULL, FALSE); | 1946 g_return_val_if_fail(user_dir != NULL, FALSE); |
1947 | 1947 |
1948 gaim_debug_info("util", "Writing file %s to directory %s\n", | 1948 gaim_debug_info("util", "Writing file %s to directory %s\n", |
1957 user_dir, strerror(errno)); | 1957 user_dir, strerror(errno)); |
1958 return FALSE; | 1958 return FALSE; |
1959 } | 1959 } |
1960 } | 1960 } |
1961 | 1961 |
1962 filename_full = g_strdup_printf("%s" G_DIR_SEPARATOR_S "%s.xml", user_dir, filename); | 1962 filename_full = g_strdup_printf("%s" G_DIR_SEPARATOR_S "%s", user_dir, filename); |
1963 filename_temp = g_strdup_printf("%s.save", filename_full); | 1963 filename_temp = g_strdup_printf("%s.save", filename_full); |
1964 | 1964 |
1965 /* Remove an old temporary file, if one exists */ | 1965 /* Remove an old temporary file, if one exists */ |
1966 if (g_file_test(filename_temp, G_FILE_TEST_EXISTS)) | 1966 if (g_file_test(filename_temp, G_FILE_TEST_EXISTS)) |
1967 { | 1967 { |
1982 g_free(filename_temp); | 1982 g_free(filename_temp); |
1983 return FALSE; | 1983 return FALSE; |
1984 } | 1984 } |
1985 | 1985 |
1986 /* Write to file */ | 1986 /* Write to file */ |
1987 datalen = strlen(data); | 1987 real_size = (size == -1) ? strlen(data) : size; |
1988 byteswritten = fwrite(data, 1, datalen, file); | 1988 byteswritten = fwrite(data, 1, real_size, file); |
1989 | 1989 |
1990 /* Close file */ | 1990 /* Close file */ |
1991 if (fclose(file) != 0) | 1991 if (fclose(file) != 0) |
1992 { | 1992 { |
1993 gaim_debug_error("util", "Error closing file %s: %s\n", | 1993 gaim_debug_error("util", "Error closing file %s: %s\n", |
1996 g_free(filename_temp); | 1996 g_free(filename_temp); |
1997 return FALSE; | 1997 return FALSE; |
1998 } | 1998 } |
1999 | 1999 |
2000 /* Ensure the file is the correct size */ | 2000 /* Ensure the file is the correct size */ |
2001 if (byteswritten != datalen) | 2001 if (byteswritten != real_size) |
2002 { | 2002 { |
2003 gaim_debug_error("util", "Error writing to file %s: Wrote %z bytes " | 2003 gaim_debug_error("util", "Error writing to file %s: Wrote %z bytes " |
2004 "but should have written %z; is your disk full?\n", | 2004 "but should have written %z; is your disk full?\n", |
2005 filename_temp, byteswritten, datalen); | 2005 filename_temp, byteswritten, real_size); |
2006 g_free(filename_full); | 2006 g_free(filename_full); |
2007 g_free(filename_temp); | 2007 g_free(filename_temp); |
2008 return FALSE; | 2008 return FALSE; |
2009 } | 2009 } |
2010 /* Use stat to be absolutely sure. */ | 2010 /* Use stat to be absolutely sure. */ |
2011 if ((stat(filename_temp, &st) == -1) || (st.st_size != datalen)) | 2011 if ((stat(filename_temp, &st) == -1) || (st.st_size != real_size)) |
2012 { | 2012 { |
2013 gaim_debug_error("util", "Error writing data to file %s: " | 2013 gaim_debug_error("util", "Error writing data to file %s: " |
2014 "Incomplete file written; is your disk full?\n", | 2014 "Incomplete file written; is your disk full?\n", |
2015 filename_temp); | 2015 filename_temp); |
2016 g_free(filename_full); | 2016 g_free(filename_full); |