00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021 #include "opensync.h"
00022 #include "opensync_internals.h"
00023
00024 #include "opensync_file.h"
00025
00026 osync_bool osync_file_write(const char *filename, const char *data, unsigned int size, int mode, OSyncError **oserror)
00027 {
00028 osync_bool ret = FALSE;
00029 GError *error = NULL;
00030 gsize writen;
00031
00032 GIOChannel *chan = g_io_channel_new_file(filename, "w", &error);
00033 if (!chan) {
00034 osync_trace(TRACE_INTERNAL, "Unable to open file %s for writing: %s", filename, error->message);
00035 osync_error_set(oserror, OSYNC_ERROR_IO_ERROR, "Unable to open file %s for writing: %s", filename, error->message);
00036 return FALSE;
00037 }
00038 if (mode) {
00039 if (g_chmod(filename, mode)) {
00040 osync_trace(TRACE_INTERNAL, "Unable to set file permissions %i for file %s", mode, filename);
00041 osync_error_set(oserror, OSYNC_ERROR_IO_ERROR, "Unable to set file permissions %i for file %s", mode, filename);
00042 return FALSE;
00043 }
00044 }
00045
00046 g_io_channel_set_encoding(chan, NULL, NULL);
00047 if (g_io_channel_write_chars(chan, data, size, &writen, &error) != G_IO_STATUS_NORMAL) {
00048 osync_trace(TRACE_INTERNAL, "Unable to write contents of file %s: %s", filename, error->message);
00049 osync_error_set(oserror, OSYNC_ERROR_IO_ERROR, "Unable to write contents of file %s: %s", filename, error->message);
00050 } else {
00051 g_io_channel_flush(chan, NULL);
00052 ret = TRUE;
00053 }
00054 g_io_channel_shutdown(chan, FALSE, NULL);
00055 g_io_channel_unref(chan);
00056 return ret;
00057 }
00058
00059 osync_bool osync_file_read(const char *filename, char **data, unsigned int *size, OSyncError **oserror)
00060 {
00061 osync_bool ret = FALSE;
00062 GError *error = NULL;
00063 gsize sz = 0;
00064 GIOChannel *chan = NULL;
00065
00066 if (!filename) {
00067 osync_trace(TRACE_INTERNAL, "No file open specified");
00068 osync_error_set(oserror, OSYNC_ERROR_IO_ERROR, "No file to open specified");
00069 return FALSE;
00070 }
00071
00072 chan = g_io_channel_new_file(filename, "r", &error);
00073 if (!chan) {
00074 osync_trace(TRACE_INTERNAL, "Unable to read file %s: %s", filename, error->message);
00075 osync_error_set(oserror, OSYNC_ERROR_IO_ERROR, "Unable to open file %s for reading: %s", filename, error->message);
00076 return FALSE;
00077 }
00078
00079 g_io_channel_set_encoding(chan, NULL, NULL);
00080 if (g_io_channel_read_to_end(chan, data, &sz, &error) != G_IO_STATUS_NORMAL) {
00081 osync_trace(TRACE_INTERNAL, "Unable to read contents of file %s: %s", filename, error->message);
00082 osync_error_set(oserror, OSYNC_ERROR_IO_ERROR, "Unable to read contents of file %s: %s", filename, error->message);
00083 } else {
00084 ret = TRUE;
00085 if (size)
00086 *size = (int)sz;
00087 }
00088 g_io_channel_shutdown(chan, FALSE, NULL);
00089 g_io_channel_unref(chan);
00090 return ret;
00091 }
00092
00093 int osync_remove_directory_recursively(const char *dirname)
00094 {
00095 GDir *gdir = NULL;
00096 GError *gerror = NULL;
00097 const char *gdir_entry = NULL;
00098 char *gdir_entry_path = NULL;
00099
00100 gdir = g_dir_open(dirname, 0, &gerror);
00101 if (!gdir)
00102 goto error;
00103 while ((gdir_entry = g_dir_read_name(gdir))) {
00104 gdir_entry_path = g_strdup_printf("%s%c%s", dirname, G_DIR_SEPARATOR, gdir_entry);
00105 if (g_file_test(gdir_entry_path, G_FILE_TEST_IS_DIR)) {
00106 if (osync_remove_directory_recursively(gdir_entry_path) < 0){
00107 g_set_error(&gerror, G_FILE_ERROR, g_file_error_from_errno(errno), "%s", gdir_entry_path);
00108 g_free(gdir_entry_path);
00109 goto error;
00110 }
00111 }else{
00112 if (g_unlink(gdir_entry_path) < 0){
00113 g_set_error(&gerror, G_FILE_ERROR, g_file_error_from_errno(errno), "%s", gdir_entry_path);
00114 g_free(gdir_entry_path);
00115 goto error;
00116 }
00117 }
00118 g_free(gdir_entry_path);
00119 }
00120 g_dir_close(gdir);
00121 gdir = NULL;
00122 if (g_rmdir(dirname) < 0){
00123 g_set_error(&gerror, G_FILE_ERROR, g_file_error_from_errno(errno), "%s", dirname);
00124 goto error;
00125 }
00126
00127 return 0;
00128
00129 error:
00130 osync_trace(TRACE_EXIT_ERROR, "%s: %s", __func__, gerror->message);
00131 g_error_free(gerror);
00132 if (gdir)
00133 g_dir_close(gdir);
00134 return -1;
00135 }
00136