예제 #1
0
파일: nmsgtool.c 프로젝트: hstern/nmsg
static void
io_close(struct nmsg_io_close_event *ce) {
	struct kickfile *kf;

	if (ctx.debug >= 5) {
		fprintf(stderr, "entering io_close()\n");
		fprintf(stderr, "%s: ce->io_type = %u\n", __func__, ce->io_type);
		fprintf(stderr, "%s: ce->close_type = %u\n", __func__, ce->close_type);
		fprintf(stderr, "%s: ce->user = %p\n", __func__, ce->user);
		if (ce->io_type == nmsg_io_io_type_input) {
			fprintf(stderr, "%s: ce->input_type = %u\n", __func__, ce->input_type);
			fprintf(stderr, "%s: ce->input = %p\n", __func__, ce->input);
		} else if (ce->io_type == nmsg_io_io_type_output) {
			fprintf(stderr, "%s: ce->output_type = %u\n", __func__, ce->output_type);
			fprintf(stderr, "%s: ce->output = %p\n", __func__, ce->output);
		}
	}

	if (ce->user != NULL && ce->user != (void *) -1 &&
	    ce->io_type == nmsg_io_io_type_output &&
	    ce->output_type == nmsg_output_type_stream)
	{
		nmsg_output_close(ce->output);

		kf = (struct kickfile *) ce->user;
		kickfile_exec(kf);
		if (ce->close_type == nmsg_io_close_type_eof) {
			if (ctx.debug >= 2)
				fprintf(stderr, "%s: closed output: %s\n",
					argv_program, kf->basename);
			kickfile_destroy(&kf);
		} else {
			kickfile_rotate(kf);
			*(ce->output) = nmsg_output_open_file(
				open_wfile(kf->tmpname), NMSG_WBUFSZ_MAX);
			setup_nmsg_output(&ctx, *(ce->output));
			if (ctx.debug >= 2)
				fprintf(stderr,
					"%s: reopened nmsg file output: %s\n",
					argv_program, kf->curname);
		}
	} else if (ce->user != NULL && ce->user != (void *) -1 &&
		   ce->io_type == nmsg_io_io_type_output &&
		   ce->output_type == nmsg_output_type_pres)
	{
		nmsg_output_close(ce->output);

		kf = (struct kickfile *) ce->user;
		kickfile_exec(kf);
		if (ce->close_type == nmsg_io_close_type_eof) {
			if (ctx.debug >= 2)
				fprintf(stderr, "%s: closed output: %s\n",
					argv_program, kf->basename);
			kickfile_destroy(&kf);
		} else {
			kickfile_rotate(kf);
			*(ce->output) = nmsg_output_open_pres(
				open_wfile(kf->tmpname));
			setup_nmsg_output(&ctx, *(ce->output));
			if (ctx.debug >= 2)
				fprintf(stderr,
					"%s: reopened pres file output: %s\n",
					argv_program, kf->curname);
		}
	} else if (ce->io_type == nmsg_io_io_type_input) {
		if ((ce->user == NULL || ce->close_type == nmsg_io_close_type_eof) &&
		     ce->input != NULL)
		{
			if (ctx.debug >= 5) {
				fprintf(stderr, "%s: closing input %p\n", __func__, ce->input);
			}
			nmsg_input_close(ce->input);
		}
	} else if (ce->io_type == nmsg_io_io_type_output) {
		if ((ce->user == NULL || ce->close_type == nmsg_io_close_type_eof) &&
		     ce->output != NULL)
		{
			if (ctx.debug >= 5) {
				fprintf(stderr, "%s: closing output %p\n", __func__, ce->output);
			}
			nmsg_output_close(ce->output);
		}
	} else {
		/* should never be reached */
		assert(0);
	}
}
예제 #2
0
int
save_prefs_as(gchar *filename, int do_backup) {
  FILE *f, *f_auth;
  char *tmpfname, *tmpfname_auth;
  char *filename_auth = g_strdup_printf("%s.auth", filename);
  if (do_backup) {
    tmpfname = g_strdup_printf("%s_wmccc_tmp", filename);
    tmpfname_auth = g_strdup_printf("%s_wmccc_tmp.auth", filename);
  } else {
    tmpfname = strdup(filename);
    tmpfname_auth = strdup(filename_auth);
  }


  f = open_wfile(tmpfname);f_auth = open_wfile(tmpfname_auth);
  if (f == NULL) {
    char *errmsg = g_strdup_printf("Can't save '%s' : %s", tmpfname, strerror(errno));
    quick_message(errmsg);
    g_free(errmsg);
    return -1;
  } else if (f_auth == NULL) {
    char *errmsg = g_strdup_printf("Can't save '%s' : %s", tmpfname_auth, strerror(errno));
    quick_message(errmsg);
    g_free(errmsg);
    return -1;
  } else {
    char *backup[4], *backup_auth[4];
    int i;
    fprintf(f, "### -*- mode: wmccoptions -*-\n### edited by wmccc -- look for *.wmccc.*.bak for backups\n");
    prefs_write_to_file(Prefs, f);
    fclose(f);
    fprintf(f_auth, "### -*- mode: wmccoptions -*-\n#\n");
    auth_prefs_write_to_file(Prefs, f_auth);
    fclose(f_auth);
    if (do_backup) {
      backup[0] = filename; backup_auth[0] = filename_auth;
      backup[1] = g_strdup_printf("%s.wmccc.bak", filename);
      backup[2] = g_strdup_printf("%s.wmccc.2.bak", filename);
      backup[3] = g_strdup_printf("%s.wmccc.3.bak", filename);
      backup_auth[1] = g_strdup_printf("%s.wmccc.auth.bak", filename);
      backup_auth[2] = g_strdup_printf("%s.wmccc.auth.2.bak", filename);
      backup_auth[3] = g_strdup_printf("%s.wmccc.auth.3.bak", filename);
      for (i=3; i >= 1; i--) {
	rename(backup[i-1], backup[i]);
	rename(backup_auth[i-1], backup_auth[i]);
      }
    
      if (rename(tmpfname, filename) == -1) {
	char *errmsg = g_strdup_printf("Couldn't rename '%s' to '%s' : %s", tmpfname, filename, strerror(errno));
	quick_message(errmsg);
	g_free(errmsg);
	rename(backup[1], backup[0]);
      }
      if (rename(tmpfname_auth, filename_auth) == -1) {
	char *errmsg = g_strdup_printf("Couldn't rename '%s' to '%s' : %s", tmpfname_auth, filename_auth, strerror(errno));
	quick_message(errmsg);
	g_free(errmsg);
	rename(backup_auth[1], backup_auth[0]);
      }
      for (i=1; i < 4; i++) {
	g_free(backup[i]);
	g_free(backup_auth[i]);
      }
    }
  }
  g_free(tmpfname);
  g_free(tmpfname_auth);
  g_free(filename_auth);
  return 0;
}