Moved MooFileError into mooutils-fs.*

This commit is contained in:
Yevgen Muntyan 2006-04-16 12:14:12 -05:00
parent f67ac1ccd4
commit 3e05555f2b
6 changed files with 237 additions and 199 deletions

View File

@ -1090,7 +1090,15 @@ static void moo_app_quit_real (MooApp *app)
if (app->priv->tmpdir)
{
moo_rmdir (app->priv->tmpdir, TRUE);
GError *error = NULL;
moo_rmdir (app->priv->tmpdir, TRUE, &error);
if (error)
{
g_warning ("%s: %s", G_STRLOC, error->message);
g_error_free (error);
}
g_free (app->priv->tmpdir);
app->priv->tmpdir = NULL;
}

View File

@ -24,6 +24,7 @@
#define MOO_FILE_SYSTEM_COMPILATION
#include "moofileview/moofilesystem.h"
#include "moofileview/symlink.h"
#include "mooutils/mooutils-fs.h"
#include MOO_MARSHALS_H
#include <string.h>
#include <sys/types.h>

View File

@ -23,24 +23,6 @@
G_BEGIN_DECLS
#define MOO_FILE_ERROR (moo_file_error_quark ())
typedef enum
{
MOO_FILE_ERROR_NONEXISTENT,
MOO_FILE_ERROR_NOT_FOLDER,
MOO_FILE_ERROR_BAD_FILENAME,
MOO_FILE_ERROR_FAILED,
MOO_FILE_ERROR_ALREADY_EXISTS,
MOO_FILE_ERROR_ACCESS_DENIED,
MOO_FILE_ERROR_READONLY,
MOO_FILE_ERROR_DIFFERENT_FS,
MOO_FILE_ERROR_NOT_IMPLEMENTED
} MooFileError;
GQuark moo_file_error_quark (void);
#define MOO_TYPE_FILE (moo_file_get_type ())
#define MOO_TYPE_FILE_INFO (moo_file_info_get_type ())
#define MOO_TYPE_FILE_FLAGS (moo_file_flags_get_type ())

View File

@ -24,6 +24,7 @@
#include <io.h>
#endif
#define BROKEN_NAME "<" "????" ">"
struct _MooFileSystemPrivate {
GHashTable *folders;
@ -45,14 +46,14 @@ static gboolean create_folder (MooFileSystem *fs,
static MooFolder *get_parent_folder (MooFileSystem *fs,
MooFolder *folder,
MooFileFlags flags);
static gboolean delete_file (MooFileSystem *fs,
const char *path,
gboolean recursive,
GError **error);
#ifndef __WIN32__
static MooFolder *get_root_folder_unix (MooFileSystem *fs,
MooFileFlags wanted);
static gboolean delete_file_unix (MooFileSystem *fs,
const char *path,
gboolean recursive,
GError **error);
static gboolean move_file_unix (MooFileSystem *fs,
const char *old_path,
const char *new_path,
@ -82,10 +83,6 @@ static MooFolder *get_root_folder_win32 (MooFileSystem *fs,
static gboolean create_folder_win32 (MooFileSystem *fs,
const char *path,
GError **error);
static gboolean delete_file_win32 (MooFileSystem *fs,
const char *path,
gboolean recursive,
GError **error);
static gboolean move_file_win32 (MooFileSystem *fs,
const char *old_path,
const char *new_path,
@ -123,10 +120,10 @@ static void moo_file_system_class_init (MooFileSystemClass *klass)
klass->get_folder = get_folder;
klass->create_folder = create_folder;
klass->get_parent_folder = get_parent_folder;
klass->delete_file = delete_file;
#ifdef __WIN32__
klass->get_root_folder = get_root_folder_win32;
klass->delete_file = delete_file_win32;
klass->move_file = move_file_win32;
klass->normalize_path = normalize_path_win32;
klass->make_path = make_path_win32;
@ -134,7 +131,6 @@ static void moo_file_system_class_init (MooFileSystemClass *klass)
klass->get_absolute_path = get_absolute_path_win32;
#else /* !__WIN32__ */
klass->get_root_folder = get_root_folder_unix;
klass->delete_file = delete_file_unix;
klass->move_file = move_file_unix;
klass->normalize_path = normalize_path_unix;
klass->make_path = make_path_unix;
@ -216,9 +212,10 @@ MooFolder *moo_file_system_get_parent_folder (MooFileSystem *fs,
}
gboolean moo_file_system_create_folder (MooFileSystem *fs,
const char *path,
GError **error)
gboolean
moo_file_system_create_folder (MooFileSystem *fs,
const char *path,
GError **error)
{
g_return_val_if_fail (MOO_IS_FILE_SYSTEM (fs), FALSE);
g_return_val_if_fail (path != NULL, FALSE);
@ -226,10 +223,11 @@ gboolean moo_file_system_create_folder (MooFileSystem *fs,
}
gboolean moo_file_system_delete_file (MooFileSystem *fs,
const char *path,
gboolean recursive,
GError **error)
gboolean
moo_file_system_delete_file (MooFileSystem *fs,
const char *path,
gboolean recursive,
GError **error)
{
g_return_val_if_fail (MOO_IS_FILE_SYSTEM (fs), FALSE);
g_return_val_if_fail (path != NULL, FALSE);
@ -237,10 +235,11 @@ gboolean moo_file_system_delete_file (MooFileSystem *fs,
}
gboolean moo_file_system_move_file (MooFileSystem *fs,
const char *old_path,
const char *new_path,
GError **error)
gboolean
moo_file_system_move_file (MooFileSystem *fs,
const char *old_path,
const char *new_path,
GError **error)
{
g_return_val_if_fail (MOO_IS_FILE_SYSTEM (fs), FALSE);
g_return_val_if_fail (old_path && new_path, FALSE);
@ -248,10 +247,11 @@ gboolean moo_file_system_move_file (MooFileSystem *fs,
}
char *moo_file_system_make_path (MooFileSystem *fs,
const char *base_path,
const char *display_name,
GError **error)
char *
moo_file_system_make_path (MooFileSystem *fs,
const char *base_path,
const char *display_name,
GError **error)
{
g_return_val_if_fail (MOO_IS_FILE_SYSTEM (fs), FALSE);
g_return_val_if_fail (base_path != NULL && display_name != NULL, FALSE);
@ -259,10 +259,11 @@ char *moo_file_system_make_path (MooFileSystem *fs,
}
char *moo_file_system_normalize_path (MooFileSystem *fs,
const char *path,
gboolean is_folder,
GError **error)
char *
moo_file_system_normalize_path (MooFileSystem *fs,
const char *path,
gboolean is_folder,
GError **error)
{
g_return_val_if_fail (MOO_IS_FILE_SYSTEM (fs), FALSE);
g_return_val_if_fail (path != NULL, FALSE);
@ -270,12 +271,13 @@ char *moo_file_system_normalize_path (MooFileSystem *fs,
}
gboolean moo_file_system_parse_path (MooFileSystem *fs,
const char *path_utf8,
char **dirname,
char **display_dirname,
char **display_basename,
GError **error)
gboolean
moo_file_system_parse_path (MooFileSystem *fs,
const char *path_utf8,
char **dirname,
char **display_dirname,
char **display_basename,
GError **error)
{
g_return_val_if_fail (MOO_IS_FILE_SYSTEM (fs), FALSE);
g_return_val_if_fail (path_utf8 != NULL, FALSE);
@ -285,9 +287,10 @@ gboolean moo_file_system_parse_path (MooFileSystem *fs,
}
char *moo_file_system_get_absolute_path (MooFileSystem *fs,
const char *display_name,
const char *current_dir)
char *
moo_file_system_get_absolute_path (MooFileSystem *fs,
const char *display_name,
const char *current_dir)
{
g_return_val_if_fail (MOO_IS_FILE_SYSTEM (fs), NULL);
g_return_val_if_fail (display_name != NULL, NULL);
@ -295,9 +298,10 @@ char *moo_file_system_get_absolute_path (MooFileSystem *fs,
}
static void fam_error (MooFileWatch *fam,
GError *error,
MooFileSystem *fs)
static void
fam_error (MooFileWatch *fam,
GError *error,
MooFileSystem *fs)
{
g_return_if_fail (fs->priv->fam == fam);
g_warning ("%s: fam error", G_STRLOC);
@ -307,7 +311,8 @@ static void fam_error (MooFileWatch *fam,
}
MooFileWatch *moo_file_system_get_file_watch (MooFileSystem *fs)
MooFileWatch *
moo_file_system_get_file_watch (MooFileSystem *fs)
{
g_return_val_if_fail (MOO_IS_FILE_SYSTEM (fs), NULL);
@ -333,8 +338,9 @@ MooFileWatch *moo_file_system_get_file_watch (MooFileSystem *fs)
/* TODO what's this? */
static void folder_deleted (MooFolder *folder,
MooFileSystem *fs)
static void
folder_deleted (MooFolder *folder,
MooFileSystem *fs)
{
g_signal_handlers_disconnect_by_func (folder,
(gpointer) folder_deleted, fs);
@ -343,10 +349,11 @@ static void folder_deleted (MooFolder *folder,
/* XXX check setting error */
MooFolder *get_folder (MooFileSystem *fs,
const char *path,
MooFileFlags wanted,
GError **error)
MooFolder *
get_folder (MooFileSystem *fs,
const char *path,
MooFileFlags wanted,
GError **error)
{
MooFolder *folder;
char *norm_path = NULL;
@ -413,38 +420,11 @@ out:
}
static MooFileError moo_file_error_from_errno (int code)
{
switch (code)
{
case EACCES:
case EPERM:
return MOO_FILE_ERROR_ACCESS_DENIED;
case EEXIST:
return MOO_FILE_ERROR_ALREADY_EXISTS;
#ifndef __WIN32__
case ELOOP:
#endif
case ENAMETOOLONG:
return MOO_FILE_ERROR_BAD_FILENAME;
case ENOENT:
return MOO_FILE_ERROR_NONEXISTENT;
case ENOTDIR:
return MOO_FILE_ERROR_NOT_FOLDER;
case EROFS:
return MOO_FILE_ERROR_READONLY;
case EXDEV:
return MOO_FILE_ERROR_DIFFERENT_FS;
}
return MOO_FILE_ERROR_FAILED;
}
/* TODO */
gboolean create_folder (G_GNUC_UNUSED MooFileSystem *fs,
const char *path,
GError **error)
gboolean
create_folder (G_GNUC_UNUSED MooFileSystem *fs,
const char *path,
GError **error)
{
g_return_val_if_fail (path != NULL, FALSE);
g_return_val_if_fail (g_path_is_absolute (path), FALSE);
@ -467,20 +447,10 @@ gboolean create_folder (G_GNUC_UNUSED MooFileSystem *fs,
}
GQuark moo_file_error_quark (void)
{
static GQuark quark = 0;
if (quark == 0)
quark = g_quark_from_static_string ("moo-file-error-quark");
return quark;
}
/***************************************************************************/
/* common methods
*/
/* folder may be deleted, but this function returns parent
folder anyway, if that exists */
static MooFolder *
@ -590,6 +560,42 @@ normalize_path (const char *path)
}
gboolean
delete_file (G_GNUC_UNUSED MooFileSystem *fs,
const char *path,
gboolean recursive,
GError **error)
{
gboolean isdir;
g_return_val_if_fail (path != NULL, FALSE);
g_return_val_if_fail (g_path_is_absolute (path), FALSE);
if (g_file_test (path, G_FILE_TEST_IS_SYMLINK))
isdir = FALSE;
else
isdir = g_file_test (path, G_FILE_TEST_IS_DIR);
if (isdir)
return moo_rmdir (path, recursive, error);
if (m_remove (path))
{
int err = errno;
char *path_utf8 = g_filename_to_utf8 (path, -1, NULL, NULL, NULL);
g_set_error (error, MOO_FILE_ERROR,
moo_file_error_from_errno (err),
"Could not delete file '%s': %s",
path_utf8 ? path_utf8 : BROKEN_NAME,
g_strerror (err));
g_free (path_utf8);
return FALSE;
}
return TRUE;
}
/***************************************************************************/
/* UNIX methods
*/
@ -645,40 +651,6 @@ static gboolean rm_fr (const char *path,
}
gboolean
delete_file_unix (G_GNUC_UNUSED MooFileSystem *fs,
const char *path,
gboolean recursive,
GError **error)
{
g_return_val_if_fail (path != NULL, FALSE);
g_return_val_if_fail (g_path_is_absolute (path), FALSE);
#ifndef __WIN32__
if (recursive && g_file_test (path, G_FILE_TEST_IS_DIR))
{
return rm_fr (path, error);
}
if (remove (path))
{
int saved_errno = errno;
g_set_error (error, MOO_FILE_ERROR,
moo_file_error_from_errno (saved_errno),
"%s", g_strerror (saved_errno));
return FALSE;
}
return TRUE;
#else /* __WIN32__ */
g_set_error (error, MOO_FILE_ERROR,
MOO_FILE_ERROR_NOT_IMPLEMENTED,
"Not implemented");
return FALSE;
#endif /* __WIN32__ */
}
gboolean move_file_unix (G_GNUC_UNUSED MooFileSystem *fs,
const char *old_path,
const char *new_path,
@ -894,20 +866,6 @@ static MooFolder *get_root_folder_win32 (MooFileSystem *fs,
}
static gboolean
delete_file_win32 (MooFileSystem *fs,
const char *path,
gboolean recursive,
GError **error)
{
#warning "Implement me"
g_set_error (error, MOO_FILE_ERROR,
MOO_FILE_ERROR_NOT_IMPLEMENTED,
"Removing files is not implemented on win32");
return FALSE;
}
static gboolean
move_file_win32 (MooFileSystem *fs,
const char *old_path,

View File

@ -33,6 +33,8 @@
#include <sys/wait.h>
#endif
#define BROKEN_NAME "<" "????" ">"
/* XXX fix this */
gboolean
@ -76,9 +78,10 @@ moo_save_file_utf8 (const char *name,
#ifndef __WIN32__
static gboolean
rm_fr (const char *path)
rm_fr (const char *path,
GError **error)
{
GError *error = NULL;
GError *error_here = NULL;
char **argv;
char *child_err;
int status;
@ -90,11 +93,12 @@ rm_fr (const char *path)
argv[3] = g_strdup (path);
if (!g_spawn_sync (NULL, argv, NULL, G_SPAWN_STDOUT_TO_DEV_NULL,
NULL, NULL, NULL, &child_err, &status, &error))
NULL, NULL, NULL, &child_err, &status, &error_here))
{
g_warning ("%s: could not run 'rm' command: %s",
G_STRLOC, error->message);
g_error_free (error);
g_set_error (error, MOO_FILE_ERROR, MOO_FILE_ERROR_FAILED,
"Could not run 'rm' command: %s",
error_here->message);
g_error_free (error_here);
g_strfreev (argv);
return FALSE;
}
@ -103,8 +107,10 @@ rm_fr (const char *path)
if (!WIFEXITED (status) || WEXITSTATUS (status))
{
g_warning ("%s: 'rm' command failed: %s",
G_STRLOC, child_err ? child_err : "");
g_set_error (error, MOO_FILE_ERROR,
MOO_FILE_ERROR_FAILED,
"'rm' command failed: %s",
child_err ? child_err : "");
g_free (child_err);
return FALSE;
}
@ -119,23 +125,22 @@ rm_fr (const char *path)
#ifdef __WIN32__
static gboolean
rm_r (const char *path)
rm_r (const char *path,
GError **error)
{
GDir *dir;
GError *error = NULL;
GError *error_here = NULL;
const char *file;
char *file_path;
gboolean success = TRUE;
g_return_val_if_fail (path != NULL, FALSE);
dir = g_dir_open (path, 0, &error);
dir = g_dir_open (path, 0, &error_here);
if (!dir)
{
g_critical ("%s: could not open directory '%s'", G_STRLOC, path);
g_critical ("%s: %s", G_STRLOC, error->message);
g_error_free (error);
g_propagate_error (error, error_here);
return FALSE;
}
@ -151,14 +156,16 @@ rm_r (const char *path)
{
case ENOTEMPTY:
case EEXIST:
if (!rm_r (file_path))
if (!rm_r (file_path, error))
success = FALSE;
break;
default:
success = FALSE;
g_warning ("%s: could not remove '%s'", G_STRLOC, file_path);
g_warning ("%s: %s", G_STRLOC, g_strerror (err));
g_set_error (error, MOO_FILE_ERROR,
moo_file_error_from_errno (err),
"Could not remove '%s': %s", file_path,
g_strerror (err));
}
}
@ -167,12 +174,14 @@ rm_r (const char *path)
g_dir_close (dir);
if (m_remove (path))
if (success && m_remove (path))
{
int err = errno;
success = FALSE;
g_warning ("%s: could not remove '%s'", G_STRLOC, path);
g_warning ("%s: %s", G_STRLOC, g_strerror (err));
g_set_error (error, MOO_FILE_ERROR,
moo_file_error_from_errno (err),
"Could not remove '%s': %s", path,
g_strerror (err));
}
return success;
@ -180,20 +189,37 @@ rm_r (const char *path)
#endif
/* XXX set errno or use GError */
gboolean
moo_rmdir (const char *path,
gboolean recursive)
gboolean recursive,
GError **error)
{
g_return_val_if_fail (path != NULL, FALSE);
if (!recursive)
return m_rmdir (path) == 0;
{
if (m_rmdir (path))
{
int err = errno;
char *path_utf8 = g_filename_to_utf8 (path, -1, NULL, NULL, NULL);
g_set_error (error, MOO_FILE_ERROR,
moo_file_error_from_errno (err),
"Could not remove '%s': %s",
path_utf8 ? path_utf8 : BROKEN_NAME,
g_strerror (err));
g_free (path_utf8);
return FALSE;
}
else
{
return TRUE;
}
}
#ifndef __WIN32__
return rm_fr (path);
return rm_fr (path, error);
#else
return rm_r (path);
return rm_r (path, error);
#endif
}
@ -213,9 +239,10 @@ moo_mkdir (const char *path,
utf8_path = g_filename_to_utf8 (path, -1, NULL, NULL, NULL);
g_set_error (error,
G_FILE_ERROR, g_file_error_from_errno (err_code),
MOO_FILE_ERROR,
moo_file_error_from_errno (err_code),
"Could not create directory '%s': %s",
utf8_path ? utf8_path : "<ERROR>",
utf8_path ? utf8_path : BROKEN_NAME,
g_strerror (err_code));
g_free (utf8_path);
@ -230,9 +257,10 @@ moo_mkdir (const char *path,
utf8_path = g_filename_to_utf8 (path, -1, NULL, NULL, NULL);
g_set_error (error,
G_FILE_ERROR, g_file_error_from_errno (err_code),
MOO_FILE_ERROR,
moo_file_error_from_errno (err_code),
"Could not create directory '%s': %s",
utf8_path ? utf8_path : "<ERROR>",
utf8_path ? utf8_path : BROKEN_NAME,
g_strerror (err_code));
g_free (utf8_path);
@ -246,16 +274,58 @@ moo_mkdir (const char *path,
return TRUE;
utf8_path = g_filename_to_utf8 (path, -1, NULL, NULL, NULL);
g_set_error (error,
G_FILE_ERROR, g_file_error_from_errno (G_FILE_ERROR_EXIST),
g_set_error (error, MOO_FILE_ERROR,
MOO_FILE_ERROR_ALREADY_EXISTS,
"Could not create directory '%s': %s",
utf8_path ? utf8_path : "<ERROR>",
utf8_path ? utf8_path : BROKEN_NAME,
g_strerror (EEXIST));
g_free (utf8_path);
return FALSE;
}
GQuark
moo_file_error_quark (void)
{
static GQuark quark = 0;
if (quark == 0)
quark = g_quark_from_static_string ("moo-file-error-quark");
return quark;
}
MooFileError
moo_file_error_from_errno (int code)
{
switch (code)
{
case EACCES:
case EPERM:
return MOO_FILE_ERROR_ACCESS_DENIED;
case EEXIST:
return MOO_FILE_ERROR_ALREADY_EXISTS;
#ifndef __WIN32__
case ELOOP:
#endif
case ENAMETOOLONG:
return MOO_FILE_ERROR_BAD_FILENAME;
case ENOENT:
return MOO_FILE_ERROR_NONEXISTENT;
case ENOTDIR:
return MOO_FILE_ERROR_NOT_FOLDER;
case EROFS:
return MOO_FILE_ERROR_READONLY;
case EXDEV:
return MOO_FILE_ERROR_DIFFERENT_FS;
}
return MOO_FILE_ERROR_FAILED;
}
char **
moo_filenames_from_locale (char **files)
{

View File

@ -19,28 +19,47 @@
G_BEGIN_DECLS
gboolean moo_save_file_utf8 (const char *name,
#define MOO_FILE_ERROR (moo_file_error_quark ())
typedef enum
{
MOO_FILE_ERROR_NONEXISTENT,
MOO_FILE_ERROR_NOT_FOLDER,
MOO_FILE_ERROR_BAD_FILENAME,
MOO_FILE_ERROR_FAILED,
MOO_FILE_ERROR_ALREADY_EXISTS,
MOO_FILE_ERROR_ACCESS_DENIED,
MOO_FILE_ERROR_READONLY,
MOO_FILE_ERROR_DIFFERENT_FS,
MOO_FILE_ERROR_NOT_IMPLEMENTED
} MooFileError;
GQuark moo_file_error_quark (void) G_GNUC_CONST;
MooFileError moo_file_error_from_errno (int err_code);
gboolean moo_save_file_utf8 (const char *name,
const char *text,
gssize len,
GError **error);
gboolean moo_rmdir (const char *path,
gboolean recursive);
gboolean moo_mkdir (const char *path,
gboolean moo_rmdir (const char *path,
gboolean recursive,
GError **error);
gboolean moo_mkdir (const char *path,
GError **error);
char **moo_filenames_from_locale (char **files);
char **moo_filenames_from_locale (char **files);
/*
* C library and WinAPI functions wrappers analogous to glib/gstdio.h
*/
int m_unlink (const char *path);
int m_mkdir (const char *path); /* S_IRWXU on unix */
int m_rmdir (const char *path);
int m_remove (const char *path);
gpointer m_fopen (const char *path,
int m_unlink (const char *path);
int m_mkdir (const char *path); /* S_IRWXU on unix */
int m_rmdir (const char *path);
int m_remove (const char *path);
gpointer m_fopen (const char *path,
const char *mode);
int m_rename (const char *old_name,
int m_rename (const char *old_name,
const char *new_name);