如何使用 Cocoa 查找已安装卷上的可用空间量?

发布于 2024-07-08 13:06:37 字数 440 浏览 9 评论 0原文

我使用以下代码来确定卷上的可用空间。 该文件夹是使用 NSOpenPanel 提供的。 所选项目是已安装卷,返回的路径是 \Volumes\Name

NSDictionary* fileAttributes = [[NSFileManager defaultManager] fileSystemAttributesAtPath:folder];

unsigned long long size = [[fileAttributes objectForKey:NSFileSystemFreeSize] longLongValue];    

有没有更好的方法来使用 Cocoa 确定已安装卷上的可用空间?

更新:这实际上是确定卷上可用空间的最佳方法。 看起来它不起作用,但这是因为该文件夹实际上是 /Volumes 而不是 /Volume/VolumeName

I am using the following code to determine free space on a volume.
The folder was provided using NSOpenPanel. The item selected was a mounted volume and the path returned is \Volumes\Name

NSDictionary* fileAttributes = [[NSFileManager defaultManager] fileSystemAttributesAtPath:folder];

unsigned long long size = [[fileAttributes objectForKey:NSFileSystemFreeSize] longLongValue];    

Is there a better method to determine the free space on a mounted volume using Cocoa?

Update: This is in fact the best way to determine the free space on a volume. It appeared it wasn't working but that was due to the fact that folder was actually /Volumes rather than /Volume/VolumeName

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

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

发布评论

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

评论(2

孤独患者 2024-07-15 13:06:38

statfs 与 df 的结果一致。 理论上 NSFileSystemFreeSize 来自 statfs,所以你的问题不应该存在。

您可能需要按如下方式运行 statfs 作为 NSFileSystemFreeSize 的替代品:

#include <sys/param.h>
#include <sys/mount.h>

int main()
{
    struct statfs buf;

    int retval = statfs("/Volumes/KINGSTON", &buf);

    printf("KINGSTON Retval: %d, fundamental file system block size %ld, total data blocks %d, total in 512 blocks: %ld\n",
            retval, buf.f_bsize, buf.f_blocks, (buf.f_bsize / 512) * buf.f_blocks); 
    printf("Free 512 blocks: %ld\n",  (buf.f_bsize / 512) * buf.f_bfree); 
    exit(0);
}

statfs is consistent with results from df. In theory NSFileSystemFreeSize comes from statfs, so your problem should not exist.

You may want to run statfs as below as a replacement for NSFileSystemFreeSize:

#include <sys/param.h>
#include <sys/mount.h>

int main()
{
    struct statfs buf;

    int retval = statfs("/Volumes/KINGSTON", &buf);

    printf("KINGSTON Retval: %d, fundamental file system block size %ld, total data blocks %d, total in 512 blocks: %ld\n",
            retval, buf.f_bsize, buf.f_blocks, (buf.f_bsize / 512) * buf.f_blocks); 
    printf("Free 512 blocks: %ld\n",  (buf.f_bsize / 512) * buf.f_bfree); 
    exit(0);
}
说不完的你爱 2024-07-15 13:06:37

提供的代码是 Cocoa 中确定卷上可用空间的最佳方法。
只需确保提供给 [NSFileManagerObj fileSystemAttributesAtPath] 的路径包含卷的完整路径。 我删除了最后一个路径组件,以确保传递的是文件夹而不是文件,这导致 /Volumes 被用作文件夹,而无法给出正确的结果。

NSDictionary* fileAttributes = [[NSFileManager defaultManager] fileSystemAttributesAtPath:folder];

unsigned long long size = [[fileAttributes objectForKey:NSFileSystemFreeSize] longLongValue];    

The code provided IS the best way in Cocoa to determine the free space on a volume.
Just make sure that the path provided to [NSFileManagerObj fileSystemAttributesAtPath] includes the full path of the volume. I was deleting the last path component to assure that a folder rather than a file was passed in which resulted in /Volumes being used as the folder which does not give the right results.

NSDictionary* fileAttributes = [[NSFileManager defaultManager] fileSystemAttributesAtPath:folder];

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