【问题标题】:How to determine if 2 URIs (paths) belong to the same partition or volume如何确定 2 个 URI(路径)是否属于同一个分区或卷
【发布时间】:2019-10-23 08:45:12
【问题描述】:

问题分两步。在 C++14(或 C)中,在 Linux 和 Windows 下。

  1. 有没有办法识别 (UUID) 存储给定 URI(文件或目录)的磁盘分区(或 docker 卷)?

  2. 有没有办法知道 2 个 URI(目录文件)是否存储在同一个磁盘分区(或 docker 卷)上?

(为了避免 X Y 问题:)真正的目标是能够用最好的方法移动文件:

  • 如果它们在同一个分区(或 Docker 卷)上,则轻移动,
  • 内容复制(如果它们位于不同的分区)。

我有这些曲目:statvfs、GetVolumeInformationByHandleW。

【问题讨论】:

  • "真正的目标是能够用最好的方法移动文件" - 然后让操作系统为您处理。在 Windows 上,您可以将 MoveFileEx() 与 MOVEFILE_COPY_ALLOWED 标志一起使用。它将在同一个驱动器上进行有效的移动,并在驱动器之间进行复制+删除。

标签: c++ linux windows c++14


【解决方案1】:

幸运的是,这很简单:

#include <sys/types.h>
#include <sys/stat.h>
#include <unistd.h>
struct stat A, B;

stat( "/etc/fstab", &A );
stat( "/boot/grub2/grub.cfg", &B );

if( A.st_dev == B.st_dev ) {
    /* Files are on same device, shallow processing is OK */
} else {
    /* Files are on different devices, bytes gotta be moved */
}

【讨论】:

    猜你喜欢
    • 2014-01-01
    • 2012-02-09
    • 2010-11-29
    • 1970-01-01
    • 2017-04-03
    • 1970-01-01
    • 2014-01-04
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多