Ejemplo n.º 1
0
static gboolean
mc_config_new_or_override_file (mc_config_t * mc_config, const gchar * ini_path)
{
    gchar *data, *written_data;
    gsize len, total_written;
    gboolean ret;
    int fd;
    ssize_t cur_written;

    data = g_key_file_to_data (mc_config->handle, &len, NULL);
    if (!exist_file (ini_path)) {
        ret = g_file_set_contents (ini_path, data, len, NULL);
        g_free (data);
        return ret;
    }
    mc_util_make_backup_if_possible (ini_path, "~");

    fd = mc_open (ini_path, O_WRONLY | O_TRUNC | O_SYNC, 0);
    if (fd == -1)
        return FALSE;

    for (written_data = data, total_written = len;
         (cur_written = mc_write (fd, (const void *) written_data, total_written)) > 0;
         written_data += cur_written, total_written -= cur_written);
    mc_close (fd);
    g_free (data);

    if (cur_written == -1) {
        mc_util_restore_from_backup_if_possible (ini_path, "~");
        return FALSE;
    }

    mc_util_unlink_backup_if_possible (ini_path, "~");
    return TRUE;
}
Ejemplo n.º 2
0
static gboolean
mc_config_new_or_override_file (mc_config_t * mc_config, const gchar * ini_path, GError ** mcerror)
{
    gchar *data, *written_data;
    gsize len, total_written;
    gboolean ret;
    int fd;
    ssize_t cur_written;
    vfs_path_t *ini_vpath;

    mc_return_val_if_error (mcerror, FALSE);

    data = g_key_file_to_data (mc_config->handle, &len, NULL);
    if (!exist_file (ini_path))
    {
        ret = g_file_set_contents (ini_path, data, len, mcerror);
        g_free (data);
        return ret;
    }

    mc_util_make_backup_if_possible (ini_path, "~");

    ini_vpath = vfs_path_from_str (ini_path);
    fd = mc_open (ini_vpath, O_WRONLY | O_TRUNC, 0);
    vfs_path_free (ini_vpath);

    if (fd == -1)
    {
        mc_propagate_error (mcerror, 0, "%s", unix_error_string (errno));
        g_free (data);
        return FALSE;
    }

    for (written_data = data, total_written = len;
         (cur_written = mc_write (fd, (const void *) written_data, total_written)) > 0;
         written_data += cur_written, total_written -= cur_written)
        ;
    mc_close (fd);
    g_free (data);

    if (cur_written == -1)
    {
        mc_util_restore_from_backup_if_possible (ini_path, "~");
        mc_propagate_error (mcerror, 0, "%s", unix_error_string (errno));
        return FALSE;
    }

    mc_util_unlink_backup_if_possible (ini_path, "~");
    return TRUE;
}
Ejemplo n.º 3
0
Archivo: util.c Proyecto: JBurant/mc
void
save_file_position (const vfs_path_t * filename_vpath, long line, long column, off_t offset,
                    GArray * bookmarks)
{
    static size_t filepos_max_saved_entries = 0;
    char *fn, *tmp_fn;
    FILE *f, *tmp_f;
    char buf[MC_MAXPATHLEN + 100];
    size_t i;
    const size_t len = vfs_path_len (filename_vpath);
    gboolean src_error = FALSE;

    if (filepos_max_saved_entries == 0)
        filepos_max_saved_entries = mc_config_get_int (mc_main_config, CONFIG_APP_SECTION,
                                                       "filepos_max_saved_entries", 1024);

    fn = mc_config_get_full_path (MC_FILEPOS_FILE);
    if (fn == NULL)
        goto early_error;

    mc_util_make_backup_if_possible (fn, TMP_SUFFIX);

    /* open file */
    f = fopen (fn, "w");
    if (f == NULL)
        goto open_target_error;

    tmp_fn = g_strdup_printf ("%s" TMP_SUFFIX, fn);
    tmp_f = fopen (tmp_fn, "r");
    if (tmp_f == NULL)
    {
        src_error = TRUE;
        goto open_source_error;
    }

    /* put the new record */
    if (line != 1 || column != 0 || bookmarks != NULL)
    {
        if (fprintf
            (f, "%s %ld;%ld;%" PRIuMAX, vfs_path_as_str (filename_vpath), line, column,
             (uintmax_t) offset) < 0)
            goto write_position_error;
        if (bookmarks != NULL)
            for (i = 0; i < bookmarks->len && i < MAX_SAVED_BOOKMARKS; i++)
                if (fprintf (f, ";%zu", g_array_index (bookmarks, size_t, i)) < 0)
                    goto write_position_error;

        if (fprintf (f, "\n") < 0)
            goto write_position_error;
    }

    i = 1;
    while (fgets (buf, sizeof (buf), tmp_f) != NULL)
    {
        if (buf[len] == ' ' && strncmp (buf, vfs_path_as_str (filename_vpath), len) == 0
            && strchr (&buf[len + 1], ' ') == NULL)
            continue;

        fprintf (f, "%s", buf);
        if (++i > filepos_max_saved_entries)
            break;
    }

  write_position_error:
    fclose (tmp_f);
  open_source_error:
    g_free (tmp_fn);
    fclose (f);
    if (src_error)
        mc_util_restore_from_backup_if_possible (fn, TMP_SUFFIX);
    else
        mc_util_unlink_backup_if_possible (fn, TMP_SUFFIX);
  open_target_error:
    g_free (fn);
  early_error:
    if (bookmarks != NULL)
        g_array_free (bookmarks, TRUE);
}