/* Copy each item in a feed to the new directory */ static void rssyl_update_format_move_contents(FolderItem *olditem, FolderItem *newitem) { gchar *oldpath, *newpath, *fname, *fpath, *nfpath; GDir *d = NULL; GError *error = NULL; oldpath = _old_rssyl_item_get_path(NULL, olditem); newpath = folder_item_get_path(newitem); if ((d = g_dir_open(oldpath, 0, &error)) == NULL) { debug_print("RSSyl: (FORMAT) couldn't open dir '%s': %s\n", oldpath, error->message); g_error_free(error); return; } debug_print("RSSyl: (FORMAT) moving contents of '%s' to '%s'\n", oldpath, newpath); while ((fname = (gchar *)g_dir_read_name(d)) != NULL) { gboolean migrate_file = to_number(fname) > 0 || strstr(fname, ".claws_") == fname; fpath = g_strconcat(oldpath, G_DIR_SEPARATOR_S, fname, NULL); if (migrate_file && g_file_test(fpath, G_FILE_TEST_IS_REGULAR)) { nfpath = g_strconcat(newpath, G_DIR_SEPARATOR_S, fname, NULL); move_file(fpath, nfpath, FALSE); g_free(nfpath); } g_remove(fpath); g_free(fpath); } g_dir_close(d); g_rmdir(oldpath); g_free(oldpath); g_free(newpath); }
static gint mh_copy_msgs(Folder *folder, FolderItem *dest, MsgInfoList *msglist, GHashTable *relation) { gboolean dest_need_scan = FALSE; gboolean src_need_scan = FALSE; FolderItem *src = NULL; gchar *srcfile; gchar *destfile; FolderItemPrefs *prefs; MsgInfo *msginfo = NULL; MsgInfoList *cur = NULL; gint curnum = 0, total = 0; gchar *srcpath = NULL; gboolean full_fetch = FALSE; time_t last_dest_mtime = (time_t)0; time_t last_src_mtime = (time_t)0; cm_return_val_if_fail(dest != NULL, -1); cm_return_val_if_fail(msglist != NULL, -1); msginfo = (MsgInfo *)msglist->data; cm_return_val_if_fail(msginfo != NULL, -1); if (msginfo->folder == dest) { g_warning("the src folder is identical to the dest.\n"); return -1; } if (msginfo->folder->folder != dest->folder) full_fetch = TRUE; if (FOLDER_TYPE(msginfo->folder->folder) == F_MH) { src = msginfo->folder; } if (dest->last_num < 0) { mh_get_last_num(folder, dest); if (dest->last_num < 0) return -1; } prefs = dest->prefs; srcpath = folder_item_get_path(msginfo->folder); dest_need_scan = mh_scan_required(dest->folder, dest); last_dest_mtime = dest->mtime; if (src) { src_need_scan = mh_scan_required(src->folder, src); last_src_mtime = src->mtime; } total = g_slist_length(msglist); if (total > 100) { if (MSG_IS_MOVE(msginfo->flags)) statusbar_print_all(_("Moving messages...")); else statusbar_print_all(_("Copying messages...")); } for (cur = msglist; cur; cur = cur->next) { msginfo = (MsgInfo *)cur->data; if (!msginfo) { goto err_reset_status; } if (!full_fetch) { srcfile = g_strconcat(srcpath, G_DIR_SEPARATOR_S, itos(msginfo->msgnum), NULL); } else { srcfile = procmsg_get_message_file(msginfo); } if (!srcfile) { goto err_reset_status; } destfile = mh_get_new_msg_filename(dest); if (!destfile) { g_free(srcfile); goto err_reset_status; } if (total > 100) { statusbar_progress_all(curnum, total, 100); if (curnum % 100 == 0) GTK_EVENTS_FLUSH(); curnum++; } debug_print("Copying message %s%c%d to %s ...\n", msginfo->folder->path, G_DIR_SEPARATOR, msginfo->msgnum, dest->path); if (MSG_IS_MOVE(msginfo->flags)) { msginfo->flags.tmp_flags &= ~MSG_MOVE_DONE; if (move_file(srcfile, destfile, TRUE) < 0) { FILE_OP_ERROR(srcfile, "move"); if (copy_file(srcfile, destfile, TRUE) < 0) { FILE_OP_ERROR(srcfile, "copy"); g_free(srcfile); g_free(destfile); goto err_reset_status; } } else { /* say unlinking's not necessary */ msginfo->flags.tmp_flags |= MSG_MOVE_DONE; } } else if (copy_file(srcfile, destfile, TRUE) < 0) { FILE_OP_ERROR(srcfile, "copy"); g_free(srcfile); g_free(destfile); goto err_reset_status; } if (prefs && prefs->enable_folder_chmod && prefs->folder_chmod) { if (chmod(destfile, prefs->folder_chmod) < 0) FILE_OP_ERROR(destfile, "chmod"); } if (relation) { if (g_hash_table_lookup(relation, msginfo) != NULL) g_warning("already in : %p", msginfo); g_hash_table_insert(relation, msginfo, GINT_TO_POINTER(dest->last_num+1)); } g_free(srcfile); g_free(destfile); dest->last_num++; } g_free(srcpath); mh_write_sequences(dest, TRUE); if (dest->mtime == last_dest_mtime && !dest_need_scan) { mh_set_mtime(folder, dest); } if (src && src->mtime == last_src_mtime && !src_need_scan) { mh_set_mtime(folder, src); } if (total > 100) { statusbar_progress_all(0,0,0); statusbar_pop_all(); } return dest->last_num; err_reset_status: g_free(srcpath); mh_write_sequences(dest, TRUE); if (total > 100) { statusbar_progress_all(0,0,0); statusbar_pop_all(); } return -1; }
static void mh_write_sequences(FolderItem *item, gboolean remove_unseen) { gchar *mh_sequences_old, *mh_sequences_new; FILE *mh_sequences_old_fp, *mh_sequences_new_fp; gchar buf[BUFFSIZE]; gchar *path = NULL; gboolean err = FALSE; START_TIMING(""); if (!item) return; path = folder_item_get_path(item); mh_sequences_old = g_strconcat(path, G_DIR_SEPARATOR_S, ".mh_sequences", NULL); mh_sequences_new = g_strconcat(path, G_DIR_SEPARATOR_S, ".mh_sequences.new", NULL); if ((mh_sequences_new_fp = g_fopen(mh_sequences_new, "w+b")) != NULL) { GSList *msglist = folder_item_get_msg_list(item); GSList *cur; MsgInfo *info = NULL; gint start = -1, end = -1; gchar *sequence = g_strdup(""); gint seq_len = 0; msglist = g_slist_sort(msglist, sort_cache_list_by_msgnum); cur = msglist; /* write the unseen sequence if we don't have to scrap it */ if (!remove_unseen) do { info = (MsgInfo *)(cur ? cur->data:NULL); if (info && (MSG_IS_UNREAD(info->flags) || MSG_IS_NEW(info->flags))) { if (start < 0) start = end = info->msgnum; else end = info->msgnum; } else { if (start > 0 && end > 0) { gchar tmp[32]; gint tmp_len = 0; if (start != end) snprintf(tmp, 31, " %d-%d", start, end); else snprintf(tmp, 31, " %d", start); tmp_len = strlen(tmp); sequence = g_realloc(sequence, seq_len+tmp_len+1); strcpy(sequence+seq_len, tmp); seq_len += tmp_len; start = end = -1; } } cur = cur ? cur->next:NULL; } while (cur || (start > 0 && end > 0)); if (sequence && *sequence) { if (fprintf(mh_sequences_new_fp, "%s%s\n", get_unseen_seq_name(), sequence) < 0) err = TRUE; else debug_print("wrote unseen sequence: '%s%s'\n", get_unseen_seq_name(), sequence); } /* rewrite the rest of the file */ if ((mh_sequences_old_fp = g_fopen(mh_sequences_old, "r+b")) != NULL) { while (fgets(buf, sizeof(buf), mh_sequences_old_fp) != NULL) { if (strncmp(buf, get_unseen_seq_name(), strlen(get_unseen_seq_name()))) if (fprintf(mh_sequences_new_fp, "%s", buf) < 0) { err = TRUE; break; } } fclose(mh_sequences_old_fp); } fflush(mh_sequences_new_fp); #if 0 fsync(fileno(mh_sequences_new_fp)); #endif if (fclose(mh_sequences_new_fp) == EOF) err = TRUE; if (!err) { if (g_rename(mh_sequences_new, mh_sequences_old) < 0) FILE_OP_ERROR(mh_sequences_new, "rename"); } g_free(sequence); procmsg_msg_list_free(msglist); } g_free(mh_sequences_old); g_free(mh_sequences_new); g_free(path); END_TIMING(); }
void rssyl_update_comments(RFolderItem *ritem) { FolderItem *item = &ritem->item; FeedItem *fi = NULL; RFetchCtx *ctx = NULL; GDir *dp; const gchar *d; GError *error = NULL; gint num; gchar *path, *msg, *fname; MainWindow *mainwin = mainwindow_get_mainwindow(); g_return_if_fail(ritem != NULL); if( ritem->fetch_comments == FALSE ) return; path = folder_item_get_path(item); g_return_if_fail(path != NULL); debug_print("RSSyl: starting to parse comments, path is '%s'\n", path); if( (dp = g_dir_open(path, 0, &error)) == NULL ) { debug_print("g_dir_open on \"%s\" failed with error %d (%s)\n", path, error->code, error->message); g_error_free(error); g_free(path); return; } ritem->fetching_comments = TRUE; while( (d = g_dir_read_name(dp)) != NULL ) { if (claws_is_exiting()) { g_dir_close(dp); g_free(path); debug_print("RSSyl: bailing out, app is exiting\n"); return; } if( (num = to_number(d)) > 0) { fname = g_strdup_printf("%s%c%s", path, G_DIR_SEPARATOR, d); if (!g_file_test(fname, G_FILE_TEST_IS_REGULAR)) continue; debug_print("RSSyl: starting to parse '%s'\n", d); if( (fi = rssyl_parse_folder_item_file(fname)) != NULL ) { if( feed_item_get_comments_url(fi) && feed_item_get_id(fi) && (ritem->fetch_comments_max_age == -1 || time(NULL) - feed_item_get_date_modified(fi) <= ritem->fetch_comments_max_age*86400)) { msg = g_strdup_printf(_("Updating comments for '%s'..."), feed_item_get_title(fi)); debug_print("RSSyl: updating comments for '%s' (%s)\n", feed_item_get_title(fi), feed_item_get_comments_url(fi)); STATUSBAR_PUSH(mainwin, msg); ctx = rssyl_prep_fetchctx_from_url(feed_item_get_comments_url(fi)); g_return_if_fail(ctx != NULL); feed_set_ssl_verify_peer(ctx->feed, ritem->ssl_verify_peer); rssyl_fetch_feed(ctx, FALSE); if( ctx->success && feed_n_items(ctx->feed) > 0 ) { g_free(ctx->feed->title); ctx->feed->title = g_strdup(ritem->official_title); feed_foreach_item(ctx->feed, rssyl_update_reference_func, feed_item_get_id(fi)); if( !rssyl_parse_feed(ritem, ctx->feed) ) { debug_print("RSSyl: Error processing comments feed\n"); log_error(LOG_PROTOCOL, RSSYL_LOG_ERROR_PROC, ctx->feed->url); } } } STATUSBAR_POP(mainwin); feed_item_free(fi); } g_free(fname); } } g_dir_close(dp); g_free(path); ritem->fetching_comments = FALSE; debug_print("RSSyl: rssyl_update_comments() is done\n"); }
static FolderItem *mh_create_folder(Folder *folder, FolderItem *parent, const gchar *name) { gchar *path, *real_name; gchar *fullpath; FolderItem *new_item; gchar *mh_sequences_filename; FILE *mh_sequences_file; cm_return_val_if_fail(folder != NULL, NULL); cm_return_val_if_fail(parent != NULL, NULL); cm_return_val_if_fail(name != NULL, NULL); path = folder_item_get_path(parent); if (!is_dir_exist(path)) if (make_dir_hier(path) != 0) return NULL; real_name = mh_filename_from_utf8(name); fullpath = g_strconcat(path, G_DIR_SEPARATOR_S, real_name, NULL); g_free(real_name); g_free(path); if (to_number(name) > 0) { MsgInfo *info = folder_item_get_msginfo(parent, to_number(name)); if (info != NULL) { gboolean ok = mh_renumber_msg(info); procmsg_msginfo_free(info); if (!ok) { g_free(fullpath); return NULL; } } } if (make_dir(fullpath) < 0) { g_free(fullpath); return NULL; } g_free(fullpath); if (parent->path) path = g_strconcat(parent->path, G_DIR_SEPARATOR_S, name, NULL); else path = g_strdup(name); new_item = folder_item_new(folder, name, path); folder_item_append(parent, new_item); g_free(path); path = folder_item_get_path(new_item); mh_sequences_filename = g_strconcat(path, G_DIR_SEPARATOR_S, ".mh_sequences", NULL); if ((mh_sequences_file = g_fopen(mh_sequences_filename, "a+b")) != NULL) { fclose(mh_sequences_file); } g_free(mh_sequences_filename); g_free(path); return new_item; }
GSList *news_get_group_list(Folder *folder) { gchar *path, *filename; FILE *fp; GSList *list = NULL; GSList *last = NULL; gchar buf[BUFFSIZE]; cm_return_val_if_fail(folder != NULL, NULL); cm_return_val_if_fail(FOLDER_CLASS(folder) == &news_class, NULL); path = folder_item_get_path(FOLDER_ITEM(folder->node->data)); if (!is_dir_exist(path)) make_dir_hier(path); filename = g_strconcat(path, G_DIR_SEPARATOR_S, NEWSGROUP_LIST, NULL); g_free(path); if ((fp = g_fopen(filename, "rb")) == NULL) { NewsSession *session; gint ok; clist *grouplist = NULL; clistiter *cur; fp = g_fopen(filename, "wb"); if (!fp) { g_free(filename); return NULL; } session = news_session_get(folder); if (!session) { fclose(fp); g_free(filename); return NULL; } ok = nntp_threaded_list(folder, &grouplist); if (ok != NEWSNNTP_NO_ERROR) { if (ok == NEWSNNTP_ERROR_STREAM) { session_destroy(SESSION(session)); REMOTE_FOLDER(folder)->session = NULL; } fclose(fp); g_free(filename); return NULL; } if (grouplist) { for (cur = clist_begin(grouplist); cur; cur = clist_next(cur)) { struct newsnntp_group_info *info = (struct newsnntp_group_info *) clist_content(cur); if (fprintf(fp, "%s %d %d %c\n", info->grp_name, info->grp_last, info->grp_first, info->grp_type) < 0) { log_error(LOG_PROTOCOL, ("Can't write newsgroup list\n")); session_destroy(SESSION(session)); REMOTE_FOLDER(folder)->session = NULL; fclose(fp); g_free(filename); newsnntp_list_free(grouplist); return NULL; } } newsnntp_list_free(grouplist); } if (fclose(fp) == EOF) { log_error(LOG_PROTOCOL, ("Can't write newsgroup list\n")); session_destroy(SESSION(session)); REMOTE_FOLDER(folder)->session = NULL; g_free(filename); return NULL; } if ((fp = g_fopen(filename, "rb")) == NULL) { FILE_OP_ERROR(filename, "fopen"); g_free(filename); return NULL; } } while (fgets(buf, sizeof(buf), fp) != NULL) { gchar *p = buf; gchar *name; gint last_num; gint first_num; gchar type; NewsGroupInfo *ginfo; p = strchr(p, ' '); if (!p) continue; *p = '\0'; p++; name = buf; if (sscanf(p, "%d %d %c", &last_num, &first_num, &type) < 3) continue; ginfo = news_group_info_new(name, first_num, last_num, type); if (!last) last = list = g_slist_append(NULL, ginfo); else { last = g_slist_append(last, ginfo); last = last->next; } } fclose(fp); g_free(filename); list = g_slist_sort(list, (GCompareFunc)news_group_info_compare); return list; }