#include <glib.h>
#include <dirent.h>
#include <sys/stat.h>
+#include <unistd.h>
+#include <string.h>
#include <errno.h>
#undef MEASURE_TIME
return file;
}
-gint mh_add_msg(Folder *folder, FolderItem *dest, const gchar *file)
+gint mh_add_msg(Folder *folder, FolderItem *dest, const gchar *file,
+ gboolean remove_source)
{
gchar *destpath;
gchar *destfile;
destpath = folder_item_get_path(dest);
g_return_val_if_fail(destpath != NULL, -1);
+ if (!is_dir_exist(destpath))
+ make_dir_hier(destpath);
+
destfile = g_strdup_printf("%s%c%d", destpath, G_DIR_SEPARATOR,
dest->last_num + 1);
- if (copy_file(file, destfile) < 0) {
- g_warning(_("can't copy message %s to %s\n"), file, destfile);
- g_free(destfile);
- return -1;
+
+ if (link(file, destfile) < 0) {
+ if (EXDEV == errno) {
+ if (copy_file(file, destfile) < 0) {
+ g_warning(_("can't copy message %s to %s\n"),
+ file, destfile);
+ g_free(destfile);
+ return -1;
+ }
+ } else {
+ FILE_OP_ERROR(file, "link");
+ g_free(destfile);
+ return -1;
+ }
+ }
+
+ if (remove_source) {
+ if (unlink(file) < 0)
+ FILE_OP_ERROR(file, "unlink");
}
g_free(destfile);
gchar *srcfile;
gchar *destfile;
FILE *fp;
+ gint filemode = 0;
+ PrefsFolderItem *prefs;
g_return_val_if_fail(dest != NULL, -1);
g_return_val_if_fail(msginfo != NULL, -1);
if (dest->last_num < 0) return -1;
}
+ prefs = dest->prefs;
+
destdir = folder_item_get_path(dest);
if ((fp = procmsg_open_mark_file(destdir, TRUE)) == NULL)
g_warning(_("Can't open mark file.\n"));
dest->last_num + 1);
g_free(destdir);
- if (is_file_exist(destfile)) {
- g_warning(_("%s already exists."), destfile);
+ if (move_file(srcfile, destfile) < 0) {
g_free(srcfile);
g_free(destfile);
if (fp) fclose(fp);
return -1;
}
- if (rename(srcfile, destfile) < 0) {
- if (EXDEV == errno) {
- if (copy_file(srcfile, destfile) < 0) {
- g_free(srcfile);
- g_free(destfile);
- return -1;
- }
- unlink(srcfile);
- } else {
- FILE_OP_ERROR(srcfile, "rename");
- g_free(srcfile);
- g_free(destfile);
- if (fp) fclose(fp);
- return -1;
- }
+ if (prefs && prefs->enable_folder_chmod && prefs->folder_chmod) {
+ if (chmod(destfile, prefs->folder_chmod) < 0)
+ FILE_OP_ERROR(destfile, "chmod");
+
+ /* for mark file */
+ filemode = prefs->folder_chmod;
+ if (filemode & S_IRGRP) filemode |= S_IWGRP;
+ if (filemode & S_IROTH) filemode |= S_IWOTH;
}
g_free(srcfile);
dest->stype == F_QUEUE ||
dest->stype == F_DRAFT ||
dest->stype == F_TRASH)
- MSG_UNSET_FLAGS(newmsginfo.flags,
- MSG_NEW|MSG_UNREAD|MSG_DELETED);
+ MSG_UNSET_PERM_FLAGS(newmsginfo.flags,
+ MSG_NEW|MSG_UNREAD|MSG_DELETED);
procmsg_write_flags(&newmsginfo, fp);
+
+ if (filemode) {
+#if HAVE_FCHMOD
+ fchmod(fileno(fp), filemode);
+#else
+ markfile = folder_item_get_mark_file(dest);
+ if (markfile) {
+ chmod(markfile, filemode);
+ g_free(markfile);
+ }
+#endif
+ }
+
fclose(fp);
}
FILE *fp;
GSList *cur;
MsgInfo *msginfo;
+ PrefsFolderItem *prefs;
g_return_val_if_fail(dest != NULL, -1);
g_return_val_if_fail(msglist != NULL, -1);
if (dest->last_num < 0) return -1;
}
+ prefs = dest->prefs;
+
destdir = folder_item_get_path(dest);
if ((fp = procmsg_open_mark_file(destdir, TRUE)) == NULL)
g_warning(_("Can't open mark file.\n"));
destfile = g_strdup_printf("%s%c%d", destdir, G_DIR_SEPARATOR,
dest->last_num + 1);
- if (is_file_exist(destfile)) {
- g_warning(_("%s already exists."), destfile);
+ if (move_file(srcfile, destfile) < 0) {
g_free(srcfile);
g_free(destfile);
break;
}
- if (rename(srcfile, destfile) < 0) {
- if (EXDEV == errno) {
- if (copy_file(srcfile, destfile) < 0) {
- g_free(srcfile);
- g_free(destfile);
- break;
- }
- unlink(srcfile);
- } else {
- FILE_OP_ERROR(srcfile, "rename");
- g_free(srcfile);
- g_free(destfile);
- break;
- }
- }
-
g_free(srcfile);
g_free(destfile);
dest->last_num++;
dest->stype == F_QUEUE ||
dest->stype == F_DRAFT ||
dest->stype == F_TRASH)
- MSG_UNSET_FLAGS(newmsginfo.flags,
- MSG_NEW|MSG_UNREAD|MSG_DELETED);
+ MSG_UNSET_PERM_FLAGS
+ (newmsginfo.flags,
+ MSG_NEW|MSG_UNREAD|MSG_DELETED);
procmsg_write_flags(&newmsginfo, fp);
}
gchar *srcfile;
gchar *destfile;
FILE *fp;
+ gint filemode = 0;
+ PrefsFolderItem *prefs;
g_return_val_if_fail(dest != NULL, -1);
g_return_val_if_fail(msginfo != NULL, -1);
if (dest->last_num < 0) return -1;
}
+ prefs = dest->prefs;
+
destdir = folder_item_get_path(dest);
+ if (!is_dir_exist(destdir))
+ make_dir_hier(destdir);
+
if ((fp = procmsg_open_mark_file(destdir, TRUE)) == NULL)
g_warning(_("Can't open mark file.\n"));
return -1;
}
+ if (prefs && prefs->enable_folder_chmod && prefs->folder_chmod) {
+ if (chmod(destfile, prefs->folder_chmod) < 0)
+ FILE_OP_ERROR(destfile, "chmod");
+
+ /* for mark file */
+ filemode = prefs->folder_chmod;
+ if (filemode & S_IRGRP) filemode |= S_IWGRP;
+ if (filemode & S_IROTH) filemode |= S_IWOTH;
+ }
+
g_free(srcfile);
g_free(destfile);
dest->last_num++;
dest->stype == F_QUEUE ||
dest->stype == F_DRAFT ||
dest->stype == F_TRASH)
- MSG_UNSET_FLAGS(newmsginfo.flags,
- MSG_NEW|MSG_UNREAD|MSG_DELETED);
+ MSG_UNSET_PERM_FLAGS(newmsginfo.flags,
+ MSG_NEW|MSG_UNREAD|MSG_DELETED);
procmsg_write_flags(&newmsginfo, fp);
+
+ if (filemode) {
+#if HAVE_FCHMOD
+ fchmod(fileno(fp), filemode);
+#else
+ markfile = folder_item_get_mark_file(dest);
+ if (markfile) {
+ chmod(markfile, filemode);
+ g_free(markfile);
+ }
+#endif
+ }
+
fclose(fp);
}
return dest->last_num;
}
+/*
+gint mh_copy_msg(Folder *folder, FolderItem *dest, MsgInfo *msginfo)
+{
+ Folder * src_folder;
+ gchar * filename;
+ gint num;
+ gchar * destdir;
+ FILE * fp;
+
+ src_folder = msginfo->folder->folder;
+
+ g_return_val_if_fail(src_folder->fetch_msg != NULL, -1);
+
+ filename = src_folder->fetch_msg(src_folder,
+ msginfo->folder,
+ msginfo->msgnum);
+ if (filename == NULL)
+ return -1;
+
+ num = folder->add_msg(folder, dest, filename, FALSE);
+
+ destdir = folder_item_get_path(dest);
+ if ((fp = procmsg_open_mark_file(destdir, TRUE)) == NULL)
+ g_warning(_("Can't open mark file.\n"));
+
+ if (fp) {
+ MsgInfo newmsginfo;
+
+ newmsginfo.msgnum = dest->last_num;
+ newmsginfo.flags = msginfo->flags;
+ if (dest->stype == F_OUTBOX ||
+ dest->stype == F_QUEUE ||
+ dest->stype == F_DRAFT ||
+ dest->stype == F_TRASH)
+ MSG_UNSET_FLAGS(newmsginfo.flags,
+ MSG_NEW|MSG_UNREAD|MSG_DELETED);
+
+ procmsg_write_flags(&newmsginfo, fp);
+ fclose(fp);
+ }
+
+ return num;
+}
+*/
+
gint mh_copy_msgs_with_dest(Folder *folder, FolderItem *dest, GSList *msglist)
{
gchar *destdir;
}
destdir = folder_item_get_path(dest);
+ if (!is_dir_exist(destdir))
+ make_dir_hier(destdir);
+
if ((fp = procmsg_open_mark_file(destdir, TRUE)) == NULL)
g_warning(_("Can't open mark file.\n"));
dest->stype == F_QUEUE ||
dest->stype == F_DRAFT ||
dest->stype == F_TRASH)
- MSG_UNSET_FLAGS(newmsginfo.flags,
- MSG_NEW|MSG_UNREAD|MSG_DELETED);
+ MSG_UNSET_PERM_FLAGS
+ (newmsginfo.flags,
+ MSG_NEW|MSG_UNREAD|MSG_DELETED);
procmsg_write_flags(&newmsginfo, fp);
}
}
gint mh_remove_all_msg(Folder *folder, FolderItem *item)
{
gchar *path;
- DIR *dp;
- struct dirent *d;
+ gint val;
g_return_val_if_fail(item != NULL, -1);
path = folder_item_get_path(item);
g_return_val_if_fail(path != NULL, -1);
- if (change_dir(path) < 0) {
- g_free(path);
- return -1;
- }
+ val = remove_all_numbered_files(path);
g_free(path);
- if ((dp = opendir(".")) == NULL) {
- FILE_OP_ERROR(item->path, "opendir");
- return -1;
- }
-
- while ((d = readdir(dp)) != NULL) {
- if (to_number(d->d_name) < 0) continue;
- if (unlink(d->d_name) < 0)
- FILE_OP_ERROR(d->d_name, "unlink");
- }
-
- closedir(dp);
- return 0;
+ return val;
}
gboolean mh_is_msg_changed(Folder *folder, FolderItem *item, MsgInfo *msginfo)
g_return_val_if_fail(folder != NULL, -1);
- CHDIR_RETURN_VAL_IF_FAIL(g_get_home_dir(), -1);
+ CHDIR_RETURN_VAL_IF_FAIL(get_home_dir(), -1);
rootpath = LOCAL_FOLDER(folder)->rootpath;
MAKE_DIR_IF_NOT_EXIST(rootpath);
CHDIR_RETURN_VAL_IF_FAIL(rootpath, -1);
g_return_val_if_fail(name != NULL, NULL);
path = folder_item_get_path(parent);
+ if (!is_dir_exist(path))
+ make_dir_hier(path);
+
fullpath = g_strconcat(path, G_DIR_SEPARATOR_S, name, NULL);
g_free(path);
g_return_val_if_fail(name != NULL, -1);
oldpath = folder_item_get_path(item);
+ if (!is_dir_exist(oldpath))
+ make_dir_hier(oldpath);
+
dirname = g_dirname(oldpath);
newpath = g_strconcat(dirname, G_DIR_SEPARATOR_S, name, NULL);
g_free(dirname);
{
struct stat s;
MsgInfo *msginfo;
- MsgFlags flags = MSG_NEW|MSG_UNREAD;
+ MsgFlags flags;
+
+ flags.perm_flags = MSG_NEW|MSG_UNREAD;
+ flags.tmp_flags = 0;
g_return_val_if_fail(item != NULL, NULL);
g_return_val_if_fail(file != NULL, NULL);
if (item->stype == F_QUEUE) {
- MSG_SET_FLAGS(flags, MSG_QUEUED);
+ MSG_SET_TMP_FLAGS(flags, MSG_QUEUED);
} else if (item->stype == F_DRAFT) {
- MSG_SET_FLAGS(flags, MSG_DRAFT);
+ MSG_SET_TMP_FLAGS(flags, MSG_DRAFT);
}
msginfo = procheader_parse(file, flags, FALSE);
return msginfo;
}
+static gboolean mh_is_maildir_one(const gchar *path, const gchar *dir)
+{
+ gchar *entry;
+ gboolean result;
+
+ entry = g_strconcat(path, G_DIR_SEPARATOR_S, dir, NULL);
+ result = is_dir_exist(entry);
+ g_free(entry);
+
+ return result;
+}
+
+/*
+ * check whether PATH is a Maildir style mailbox.
+ * This is the case if the 3 subdir: new, cur, tmp are existing.
+ * This functon assumes that entry is an directory
+ */
+static gboolean mh_is_maildir(const gchar *path)
+{
+ return mh_is_maildir_one(path, "new") &&
+ mh_is_maildir_one(path, "cur") &&
+ mh_is_maildir_one(path, "tmp");
+}
+
static void mh_scan_tree_recursive(FolderItem *item)
{
DIR *dp;
if (S_ISDIR(s.st_mode)) {
FolderItem *new_item;
+ if (mh_is_maildir(entry)) {
+ g_free(entry);
+ continue;
+ }
+
new_item = folder_item_new(d->d_name, entry);
folder_item_append(item, new_item);
if (!item->path) {