使用 Glib 库函数将文件从源复制到目标

发布于 2024-10-06 22:52:00 字数 29 浏览 3 评论 0原文

如何使用 Glib 库将文件从源复制到目标。

How do i copy a file from a source to destination using Glib libraries.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

梦醒时光 2024-10-13 22:52:01

我们必须使用 gio 库。您可以从以下链接获取文档

http://library.gnome.org/devel/gio /稳定/

We have to use gio library. You can get the documentation from the following link

http://library.gnome.org/devel/gio/stable/

瑕疵 2024-10-13 22:52:01

这是一个老问题,但这是我快速编写的一些代码供自己使用。它利用 mmap()/memcpy() 并且是为 64 位平台编写的。请随意使用它,无论它适合什么。

gboolean
ak_common_copy_file (const gchar *src, const gchar *dst, gboolean overwrite)
{
    g_return_val_if_fail (src != NULL, FALSE);
    g_return_val_if_fail (dst != NULL, FALSE);

    if (sizeof (gpointer) != 8) {
        g_error (G_STRLOC ": Not a 64-bit platform.");
        return FALSE;
    }

    struct stat st;

    /* check source file */
    if (!g_file_test (src, G_FILE_TEST_EXISTS)) {
        g_critical (G_STRLOC ": File '%s' not found.", src);
        return FALSE;
    }
    else if (!g_file_test (src, G_FILE_TEST_IS_REGULAR)) {
        g_critical (G_STRLOC ": '%s' not a regular file.", src);
        return FALSE;
    }

    /* check the destination */
    if (g_file_test (dst, G_FILE_TEST_EXISTS)) {

        if (!overwrite) {
            g_critical (G_STRLOC ": Destination file '%s' exists and overwrite is disabled.", dst);
            return FALSE;
        }

        /* remove dest fileif overwrite is enabled */
        if (unlink (dst) != 0) {
            g_critical (G_STRLOC ": %s.", g_strerror (errno));
            return FALSE;
        }
    }

    /* open the input file */
    gint src_fd = open (src, O_RDONLY);
    if (src_fd < 0) {
        g_critical (G_STRLOC ": Unable to open '%s' for reading. %s.", src, g_strerror (errno));
        return FALSE;
    }


    gint dst_fd = -1;
    gpointer mm_in = NULL;
    gpointer mm_out = NULL;

    gboolean ret = FALSE;

    /***** do "goto done;" from here on any errors *****/

    if (fstat (src_fd, &st) != 0) {
        g_critical (G_STRLOC ": %s.", g_strerror (errno));
        goto done;
    }

    /* open the output file */
    dst_fd = open (dst, O_RDWR | O_TRUNC | O_CREAT, 0644);
    if (dst_fd < 0) {
        g_critical (G_STRLOC ": Unable to create '%s' for writing. %s.", dst, g_strerror (errno));
        goto done;
    }

    /* allocate space for the output file */
    if (posix_fallocate (dst_fd, 0, st.st_size)) {
        g_critical (G_STRLOC ": Unable to allocate space for the output file.");
        goto done;
    }

    /***** mmap files and do the copy *****/

    /* map file to memory */
    mm_in = mmap (NULL, st.st_size, PROT_READ, MAP_SHARED, src_fd, 0);
    if (mm_in == MAP_FAILED) {
        g_critical (G_STRLOC ": %s.", g_strerror (errno));
        goto done;
    }

    /* map file to memory */
    mm_out = mmap (NULL, st.st_size, PROT_WRITE, MAP_SHARED, dst_fd, 0);
    if (mm_out == MAP_FAILED) {
        g_critical (G_STRLOC ": %s.", g_strerror (errno));
        goto done;
    }

    /* write the content */
    memcpy (mm_out, mm_in, st.st_size);

    ret = TRUE;

done:

    /* cleanup */
    if (mm_in) {
        munmap (mm_in, st.st_size);
        mm_in = NULL;
    }

    if (mm_out) {
        munmap (mm_out, st.st_size);
        mm_out = NULL;
    }

    if (dst_fd >= 0) {
        close (dst_fd);
        dst_fd = -1;
    }

    if (src_fd >= 0) {
        close (src_fd);
        src_fd = -1;
    }

    return ret;
}

It is an old question but here is some code that I wrote quickly for my own use. It utilizes mmap()/memcpy() and it was written for 64-bit platform. Feel free to use it for what ever it fits.

gboolean
ak_common_copy_file (const gchar *src, const gchar *dst, gboolean overwrite)
{
    g_return_val_if_fail (src != NULL, FALSE);
    g_return_val_if_fail (dst != NULL, FALSE);

    if (sizeof (gpointer) != 8) {
        g_error (G_STRLOC ": Not a 64-bit platform.");
        return FALSE;
    }

    struct stat st;

    /* check source file */
    if (!g_file_test (src, G_FILE_TEST_EXISTS)) {
        g_critical (G_STRLOC ": File '%s' not found.", src);
        return FALSE;
    }
    else if (!g_file_test (src, G_FILE_TEST_IS_REGULAR)) {
        g_critical (G_STRLOC ": '%s' not a regular file.", src);
        return FALSE;
    }

    /* check the destination */
    if (g_file_test (dst, G_FILE_TEST_EXISTS)) {

        if (!overwrite) {
            g_critical (G_STRLOC ": Destination file '%s' exists and overwrite is disabled.", dst);
            return FALSE;
        }

        /* remove dest fileif overwrite is enabled */
        if (unlink (dst) != 0) {
            g_critical (G_STRLOC ": %s.", g_strerror (errno));
            return FALSE;
        }
    }

    /* open the input file */
    gint src_fd = open (src, O_RDONLY);
    if (src_fd < 0) {
        g_critical (G_STRLOC ": Unable to open '%s' for reading. %s.", src, g_strerror (errno));
        return FALSE;
    }


    gint dst_fd = -1;
    gpointer mm_in = NULL;
    gpointer mm_out = NULL;

    gboolean ret = FALSE;

    /***** do "goto done;" from here on any errors *****/

    if (fstat (src_fd, &st) != 0) {
        g_critical (G_STRLOC ": %s.", g_strerror (errno));
        goto done;
    }

    /* open the output file */
    dst_fd = open (dst, O_RDWR | O_TRUNC | O_CREAT, 0644);
    if (dst_fd < 0) {
        g_critical (G_STRLOC ": Unable to create '%s' for writing. %s.", dst, g_strerror (errno));
        goto done;
    }

    /* allocate space for the output file */
    if (posix_fallocate (dst_fd, 0, st.st_size)) {
        g_critical (G_STRLOC ": Unable to allocate space for the output file.");
        goto done;
    }

    /***** mmap files and do the copy *****/

    /* map file to memory */
    mm_in = mmap (NULL, st.st_size, PROT_READ, MAP_SHARED, src_fd, 0);
    if (mm_in == MAP_FAILED) {
        g_critical (G_STRLOC ": %s.", g_strerror (errno));
        goto done;
    }

    /* map file to memory */
    mm_out = mmap (NULL, st.st_size, PROT_WRITE, MAP_SHARED, dst_fd, 0);
    if (mm_out == MAP_FAILED) {
        g_critical (G_STRLOC ": %s.", g_strerror (errno));
        goto done;
    }

    /* write the content */
    memcpy (mm_out, mm_in, st.st_size);

    ret = TRUE;

done:

    /* cleanup */
    if (mm_in) {
        munmap (mm_in, st.st_size);
        mm_in = NULL;
    }

    if (mm_out) {
        munmap (mm_out, st.st_size);
        mm_out = NULL;
    }

    if (dst_fd >= 0) {
        close (dst_fd);
        dst_fd = -1;
    }

    if (src_fd >= 0) {
        close (src_fd);
        src_fd = -1;
    }

    return ret;
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文