Exemple #1
0
int32_t
gf_store_rename_tmppath (gf_store_handle_t *shandle)
{
        int32_t         ret = -1;
        char            tmppath[PATH_MAX] = {0,};

        GF_VALIDATE_OR_GOTO ("store", shandle, out);
        GF_VALIDATE_OR_GOTO ("store", shandle->path, out);

        ret = fsync (shandle->tmp_fd);
        if (ret) {
                gf_log (THIS->name, GF_LOG_ERROR, "Failed to fsync %s, "
                        "error: %s", shandle->path, strerror (errno));
                goto out;
        }
        snprintf (tmppath, sizeof (tmppath), "%s.tmp", shandle->path);
        ret = rename (tmppath, shandle->path);
        if (ret) {
                gf_log (THIS->name, GF_LOG_ERROR, "Failed to rename %s to %s, "
                        "error: %s", tmppath, shandle->path, strerror (errno));
                goto out;
        }

        ret = gf_store_sync_direntry (tmppath);
out:
        if (shandle && shandle->tmp_fd >= 0) {
                close (shandle->tmp_fd);
                shandle->tmp_fd = -1;
        }
        return ret;
}
int32_t
gf_store_handle_new (char *path, gf_store_handle_t **handle)
{
        int32_t                 ret = -1;
        gf_store_handle_t *shandle = NULL;
        int                     fd = -1;
        char                    *spath = NULL;

        shandle = GF_CALLOC (1, sizeof (*shandle), gf_common_mt_store_handle_t);
        if (!shandle)
                goto out;

        spath = gf_strdup (path);

        if (!spath)
                goto out;

        fd = open (path, O_RDWR | O_CREAT | O_APPEND, 0600);
        if (fd <= 0) {
                gf_log ("", GF_LOG_ERROR, "Failed to open file: %s, error: %s",
                        path, strerror (errno));
                goto out;
        }

        ret = gf_store_sync_direntry (spath);
        if (ret)
                goto out;

        shandle->path = spath;
        shandle->locked = F_ULOCK;
        *handle = shandle;

        ret = 0;
out:
        if (fd > 0)
                close (fd);

        if (ret == -1) {
                GF_FREE (spath);
                GF_FREE (shandle);
        }

        gf_log ("", GF_LOG_DEBUG, "Returning %d", ret);
        return ret;
}
int32_t
gf_store_rename_tmppath (gf_store_handle_t *shandle)
{
        int32_t         ret = -1;
        char            tmppath[PATH_MAX] = {0,};

        GF_ASSERT (shandle);
        GF_ASSERT (shandle->path);

        snprintf (tmppath, sizeof (tmppath), "%s.tmp", shandle->path);
        ret = rename (tmppath, shandle->path);
        if (ret) {
                gf_log (THIS->name, GF_LOG_ERROR, "Failed to rename %s to %s, "
                        "error: %s", tmppath, shandle->path, strerror (errno));
                goto out;
        }

        ret = gf_store_sync_direntry (tmppath);
out:
        return ret;
}