static int open_file_write(struct perf_data *data) { int fd; char sbuf[STRERR_BUFSIZE]; if (check_backup(data)) return -1; fd = open(data->file.path, O_CREAT|O_RDWR|O_TRUNC|O_CLOEXEC, S_IRUSR|S_IWUSR); if (fd < 0) pr_err("failed to open %s : %s\n", data->file.path, str_error_r(errno, sbuf, sizeof(sbuf))); return fd; }
/* Check if host is online and on schedule * called by main function to see if a host is online and on schedule * for backup */ static void ping_host(Host *host, AppData *app_data){ BusBackupType type; GString *mssg; mssg = g_string_new(NULL); gchar *host_name = g_strdup_printf("\"%s\"", host_get_name(host)); g_string_printf(mssg, "Host %-20s: ", host_name); g_free(host_name); if (host_is_on_schedule(host)){ g_string_append_printf(mssg, "on schedule : "); if (host_is_online(host)){ gchar *ip = g_strdup_printf("(%s)", host_get_ip(host)); g_string_append_printf(mssg, "online %-16s : ", ip); g_free(ip); if ((type = check_backup(host)) != BUS_BACKUP_TYPE_NONE){ g_string_append_printf(mssg, "needs %s backup.", (type == BUS_BACKUP_TYPE_FULL) ? "full" : "incr"); queue_backup(host, type, app_data); } else { g_string_append_printf(mssg, "doesn't need a backup."); } } else { g_string_append_printf(mssg, "offline."); } syslog(LOG_INFO, mssg->str); } /* else { g_string_append_printf(mssg, "not on schedule."); } */ g_string_free(mssg, TRUE); }
int read_file (struct wnn_file *wf) { FILE *fp; struct wnn_file_head fh; if (wf->localf == REMOTE) { if (fopen_read_cur (wf->name) == 0) { wnn_errorno = WNN_FILE_READ_ERROR; log_err ("read_file:could not open file %s.", wf->name); return (-1); } fp = NULL; } else { #ifdef WRITE_CHECK check_backup (wf->name); #endif if ((fp = fopen (wf->name, "r")) == NULL) { wnn_errorno = WNN_FILE_READ_ERROR; log_err("read_file:could not open file %s.", wf->name); return (-1); } } if (input_file_header (fp, &fh) == -1) { wnn_errorno = WNN_NOT_A_FILE; goto ERROR_RET; } bcopy ((char *) &(fh.file_uniq), (char *) &(wf->f_uniq), WNN_F_UNIQ_LEN); bcopy ((char *) &(fh.file_uniq_org), (char *) &(wf->f_uniq_org), WNN_F_UNIQ_LEN); strncpy (wf->passwd, fh.file_passwd, WNN_PASSWD_LEN); wf->file_type = fh.file_type; wf->ref_count = 0; switch (fh.file_type) { case WNN_FT_DICT_FILE: wf->area = (char *) readdict (fp); if (wf->area == NULL) goto ERROR_RET; break; case WNN_FT_HINDO_FILE: wf->area = (char *) readhindo (fp); if (wf->area == NULL) goto ERROR_RET; break; case WNN_FT_FUZOKUGO_FILE: wf->area = (char *) fzk_read (fp); if (wf->area == NULL) goto ERROR_RET; break; } vclose (fp); return (0); ERROR_RET: vclose (fp); return (-1); }
int write_file (struct wnn_file *wf, char *n) { FILE *fp; int mode = 3; struct wnn_file_head fh; #ifdef WRITE_CHECK char *tmp = NULL, *backup = NULL; check_backup (n); #endif if ((fp = fopen (n, "r")) != NULL) { /* Old File Exist */ if (input_file_header (fp, &fh) == -1) { wnn_errorno = WNN_NOT_A_FILE; fclose (fp); return (-1); } mode = must_write_file (wf, &(fh.file_uniq)); fclose (fp); if (mode == -1) return -1; } if (mode == 0) { return (0); /* Need Not Write */ } else if (mode == 1 || mode == 3) { /* 3 when the file is not the one to be read. */ #ifdef WRITE_CHECK backup = make_backup_file (n); if ((tmp = make_tmp_file (n, 0, &fp)) == NULL) { delete_tmp_file (backup); #else /* WRITE_CHECK */ if ((fp = fopen (n, "w+")) == NULL) { #endif /* WRITE_CHECK */ wnn_errorno = WNN_FILE_WRITE_ERROR; return (-1); } } else if (mode == 2) { #ifdef WRITE_CHECK backup = make_backup_file (n); if ((tmp = make_tmp_file (n, 1, &fp)) == NULL) { delete_tmp_file (backup); #else /* WRITE_CHECK */ if ((fp = fopen (n, "r+")) == NULL) { /* New File */ #endif /* WRITE_CHECK */ wnn_errorno = WNN_FILE_WRITE_ERROR; return (-1); } } if (write_file_real (wf, fp, mode) == -1) { fclose (fp); #ifdef WRITE_CHECK delete_tmp_file (tmp); delete_tmp_file (backup); #endif /* WRITE_CHECK */ return -1; } fclose (fp); #ifdef WRITE_CHECK move_tmp_to_org (tmp, n, 1); delete_tmp_file (backup); #endif /* WRITE_CHECK */ if ((mode == 1) || (mode == 2)) { clear_dirty_bit (wf); } return (0); } static int write_file_real (struct wnn_file *wf, FILE *fp, int mode /* 1 For All, 2 For only hindo */ ) { struct wnn_file_head fh; if (fp) rewind (fp); bcopy ((char *) &wf->f_uniq, (char *) &(fh.file_uniq), WNN_F_UNIQ_LEN); bcopy ((char *) &wf->f_uniq_org, (char *) &(fh.file_uniq_org), WNN_F_UNIQ_LEN); bcopy (wf->passwd, fh.file_passwd, WNN_PASSWD_LEN); fh.file_type = wf->file_type; if (output_file_header (fp, &fh) == -1) { wnn_errorno = WNN_FILE_WRITE_ERROR; goto ERROR_RET; } switch (fh.file_type) { case WNN_FT_DICT_FILE: { struct JT *jt2; struct JT *jt = (struct JT *) wf->area; if (little_endian () && jt->dirty) { if ((jt2 = copy_dict ((struct JT *) wf->area)) == NULL) goto ERROR_RET; revdic (jt2, 1); if (writedict (jt2, fp) == -1) goto ERROR_RET; jt2 = free_dict (jt2); } else { /* if(writedict(wf->area, fp) == -1)goto ERROR_RET; */ if (mode == 2) { if (write_hindo_of_dict (wf->area, fp) == -1) goto ERROR_RET; } else { if (writedict (wf->area, fp) == -1) goto ERROR_RET; } } } break; case WNN_FT_HINDO_FILE: if (writehindo (wf->area, fp) == -1) goto ERROR_RET; break; case WNN_FT_FUZOKUGO_FILE: wnn_errorno = NOT_SUPPORTED_OPERATION; goto ERROR_RET; } return (0); ERROR_RET: return (-1); } static int writedict (struct JT *jt1, FILE *fp) { if (output_header_jt (fp, jt1) == -1) return (-1); #ifdef WRITE_CHECK if ((vfwrite (jt1->comment, 2, jt1->maxcomment, fp) == -1) || (vfwrite (jt1->hinsi_list, 2, jt1->maxhinsi_list, fp) == -1) || (vfwrite (jt1->hindo, 1, jt1->maxserial, fp) == -1) || (vfwrite (jt1->hinsi, 2, jt1->maxserial, fp) == -1)) return (-1); #ifdef CONVERT_with_SiSheng if (jt1->syurui == CWNN_REV_DICT) /* for Chinese PinYin dic only */ if (vfwrite (jt1->sisheng, 2, jt1->maxserial, fp) == -1) return (-1); #endif /* CONVERT_with_SiSheng */ if ((vfwrite (jt1->kanji, 1, jt1->maxkanji, fp) == -1) || (vfwrite (jt1->table, sizeof (struct uind1), jt1->maxtable, fp) == -1) || (vfwrite (jt1->ri1[D_YOMI], sizeof (struct rind1), jt1->maxri1[D_YOMI], fp) == -1) || (vfwrite (jt1->ri1[D_KANJI], sizeof (struct rind1), jt1->maxri1[D_KANJI], fp) == -1) || (vfwrite (jt1->hontai, 1, jt1->maxhontai, fp) == -1) || (vfwrite (jt1->ri2, sizeof (struct rind2), jt1->maxri2, fp) == -1)) return (-1); #else /* WRITE_CHECK */ vfwrite (jt1->comment, 2, jt1->maxcomment, fp); vfwrite (jt1->hinsi_list, 2, jt1->maxhinsi_list, fp); vfwrite (jt1->hindo, 1, jt1->maxserial, fp); vfwrite (jt1->hinsi, 2, jt1->maxserial, fp); #ifdef CONVERT_with_SiSheng if (jt1->syurui == CWNN_REV_DICT) /* for Chinese PinYin dic only */ vfwrite (jt1->sisheng, 2, jt1->maxserial, fp); #endif /* CONVERT_with_SiSheng */ vfwrite (jt1->kanji, 1, jt1->maxkanji, fp); vfwrite (jt1->table, sizeof (struct uind1), jt1->maxtable, fp); vfwrite (jt1->ri1[D_YOMI], sizeof (struct rind1), jt1->maxri1[D_YOMI], fp); vfwrite (jt1->ri1[D_KANJI], sizeof (struct rind1), jt1->maxri1[D_KANJI], fp); vfwrite (jt1->hontai, 1, jt1->maxhontai, fp); vfwrite (jt1->ri2, sizeof (struct rind2), jt1->maxri2, fp); #endif /* WRITE_CHECK */ return (0); } static int write_hindo_of_dict (struct JT *jt1, FILE *fp) { if (output_header_jt (fp, jt1) == -1) return (-1); #ifdef WRITE_CHECK if ((vfwrite (jt1->comment, 2, jt1->maxcomment, fp) == -1) || (vfwrite (jt1->hindo, 1, jt1->maxserial, fp) == -1)) return (-1); #else /* WRITE_CHECK */ vfwrite (jt1->comment, 2, jt1->maxcomment, fp); vfwrite (jt1->hindo, 1, jt1->maxserial, fp); #endif /* WRITE_CHECK */ return (0); } int discardfile (struct wnn_file *wf) { #ifdef nodef FILE *fp; if (wf->localf == LOCAL) { if ((fp = fopen (wf->name, "r")) == NULL) { log_err ("discardfile:No file %s.", wf->name); return (-1); } fclose (fp); } #endif switch (wf->file_type) { case WNN_FT_DICT_FILE: wf->area = free_dict (wf->area); break; case WNN_FT_HINDO_FILE: wf->area = free_hindo (wf->area); break; case WNN_FT_FUZOKUGO_FILE: /* fzk_discard(wf->area); */ break; } return (0); }