Mercurial > pidgin
comparison src/util.c @ 10414:26eac2362c32
[gaim-migrate @ 11664]
I'm starting to feel better.
Little change here, little change there.
Mostly I added a function gaim_util_write_xml_file to be used to
write our config files.
committer: Tailor Script <tailor@pidgin.im>
author | Mark Doliner <mark@kingant.net> |
---|---|
date | Sat, 25 Dec 2004 18:33:27 +0000 |
parents | 618a330c0260 |
children | 5b7a74d397cc |
comparison
equal
deleted
inserted
replaced
10413:960b6a41d02f | 10414:26eac2362c32 |
---|---|
1928 g_free(dir); | 1928 g_free(dir); |
1929 return 0; | 1929 return 0; |
1930 } | 1930 } |
1931 | 1931 |
1932 /* | 1932 /* |
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 | |
1935 * people's settings if there is a problem writing the new values. | |
1936 */ | |
1937 gboolean | |
1938 gaim_util_write_xml_file(const char *filename, const char *data) | |
1939 { | |
1940 const char *user_dir = gaim_user_dir(); | |
1941 gchar *filename_temp, *filename_full; | |
1942 FILE *file; | |
1943 size_t datalen, byteswritten; | |
1944 struct stat st; | |
1945 | |
1946 g_return_val_if_fail(user_dir != NULL, FALSE); | |
1947 | |
1948 gaim_debug_info("util", "Writing file %s to directory %s\n", | |
1949 filename, user_dir); | |
1950 | |
1951 /* Ensure the user directory exists */ | |
1952 if (!g_file_test(user_dir, G_FILE_TEST_IS_DIR)) | |
1953 { | |
1954 if (mkdir(user_dir, S_IRUSR | S_IWUSR | S_IXUSR) == -1) | |
1955 { | |
1956 gaim_debug_error("util", "Error creating directory %s: %s\n", | |
1957 user_dir, strerror(errno)); | |
1958 return FALSE; | |
1959 } | |
1960 } | |
1961 | |
1962 filename_full = g_strdup_printf("%s" G_DIR_SEPARATOR_S "%s.xml", user_dir, filename); | |
1963 filename_temp = g_strdup_printf("%s.save", filename_full); | |
1964 | |
1965 /* Remove an old temporary file, if one exists */ | |
1966 if (g_file_test(filename_temp, G_FILE_TEST_EXISTS)) | |
1967 { | |
1968 if (unlink(filename_temp) == -1) | |
1969 { | |
1970 gaim_debug_error("util", "Error removing old file %s: %s\n", | |
1971 filename_temp, strerror(errno)); | |
1972 } | |
1973 } | |
1974 | |
1975 /* Open file */ | |
1976 file = fopen(filename_temp, "w"); | |
1977 if (file == NULL) | |
1978 { | |
1979 gaim_debug_error("util", "Error opening file %s for writing: %s\n", | |
1980 filename_temp, strerror(errno)); | |
1981 g_free(filename_full); | |
1982 g_free(filename_temp); | |
1983 return FALSE; | |
1984 } | |
1985 | |
1986 /* Write to file */ | |
1987 datalen = strlen(data); | |
1988 byteswritten = fwrite(data, 1, datalen, file); | |
1989 | |
1990 /* Close file */ | |
1991 if (fclose(file) != 0) | |
1992 { | |
1993 gaim_debug_error("util", "Error closing file %s: %s\n", | |
1994 filename_temp, strerror(errno)); | |
1995 g_free(filename_full); | |
1996 g_free(filename_temp); | |
1997 return FALSE; | |
1998 } | |
1999 | |
2000 /* Ensure the file is the correct size */ | |
2001 if (byteswritten != datalen) | |
2002 { | |
2003 gaim_debug_error("util", "Error writing to file %s: Wrote %z bytes " | |
2004 "but should have written %z; is your disk full?\n", | |
2005 filename_temp, byteswritten, datalen); | |
2006 g_free(filename_full); | |
2007 g_free(filename_temp); | |
2008 return FALSE; | |
2009 } | |
2010 /* Use stat to be absolutely sure. */ | |
2011 if ((stat(filename_temp, &st) == -1) || (st.st_size != datalen)) | |
2012 { | |
2013 gaim_debug_error("util", "Error writing data to file %s: " | |
2014 "Incomplete file written; is your disk full?\n", | |
2015 filename_temp); | |
2016 g_free(filename_full); | |
2017 g_free(filename_temp); | |
2018 return FALSE; | |
2019 } | |
2020 | |
2021 /* Set file permissions */ | |
2022 if (chmod(filename_temp, S_IRUSR | S_IWUSR) == -1) | |
2023 { | |
2024 gaim_debug_error("util", "Error setting permissions of file %s: %s\n", | |
2025 filename_temp, strerror(errno)); | |
2026 } | |
2027 | |
2028 /* Remove the old file, if it exists */ | |
2029 if (g_file_test(filename_full, G_FILE_TEST_EXISTS)) | |
2030 { | |
2031 if (unlink(filename_full) == -1) | |
2032 { | |
2033 gaim_debug_error("util", "Error removing old file %s: %s\n", | |
2034 filename_full, strerror(errno)); | |
2035 } | |
2036 } | |
2037 | |
2038 /* Rename to the REAL name */ | |
2039 if (rename(filename_temp, filename_full) == -1) | |
2040 { | |
2041 gaim_debug_error("util", "Error renaming %s to %s: %s\n", | |
2042 filename_temp, filename_full, strerror(errno)); | |
2043 } | |
2044 | |
2045 g_free(filename_full); | |
2046 g_free(filename_temp); | |
2047 | |
2048 return TRUE; | |
2049 } | |
2050 | |
2051 /* | |
1933 * Like mkstemp() but returns a file pointer, uses a pre-set template, | 2052 * Like mkstemp() but returns a file pointer, uses a pre-set template, |
1934 * uses the semantics of tempnam() for the directory to use and allocates | 2053 * uses the semantics of tempnam() for the directory to use and allocates |
1935 * the space for the filepath. | 2054 * the space for the filepath. |
1936 * | 2055 * |
1937 * Caller is responsible for closing the file and removing it when done, | 2056 * Caller is responsible for closing the file and removing it when done, |