2023-11-01 21:11:11 +00:00
|
|
|
// This file Copyright © Mnemosyne LLC.
|
2022-02-07 16:25:02 +00:00
|
|
|
// It may be used under GPLv2 (SPDX: GPL-2.0-only), GPLv3 (SPDX: GPL-3.0-only),
|
2022-01-20 18:27:56 +00:00
|
|
|
// or any future license endorsed by Mnemosyne LLC.
|
|
|
|
// License text can be found in the licenses/ folder.
|
2013-07-08 17:07:31 +00:00
|
|
|
|
2023-08-22 02:59:47 +00:00
|
|
|
#include <algorithm>
|
2021-10-17 20:17:18 +00:00
|
|
|
#include <cerrno>
|
2023-11-03 17:03:26 +00:00
|
|
|
#include <cstdint> // int64_t
|
|
|
|
#include <cstdio> // FILE
|
|
|
|
#include <optional>
|
2022-01-13 02:13:58 +00:00
|
|
|
#include <string>
|
2021-12-15 21:25:42 +00:00
|
|
|
#include <string_view>
|
2014-12-13 15:22:39 +00:00
|
|
|
|
2017-04-21 07:40:57 +00:00
|
|
|
#include <event2/util.h> /* evutil_ascii_strcasecmp() */
|
2013-07-08 17:07:31 +00:00
|
|
|
|
2014-07-04 00:00:07 +00:00
|
|
|
#ifndef _WIN32
|
2017-04-19 12:04:45 +00:00
|
|
|
#include <unistd.h> /* getuid() */
|
|
|
|
#include <sys/types.h> /* types needed by quota.h */
|
|
|
|
#if defined(__FreeBSD__) || defined(__OpenBSD__)
|
|
|
|
#include <ufs/ufs/quota.h> /* quotactl() */
|
2017-06-19 10:45:09 +00:00
|
|
|
#elif defined(__DragonFly__)
|
|
|
|
#include <vfs/ufs/quota.h> /* quotactl */
|
2017-04-19 12:04:45 +00:00
|
|
|
#elif defined(__NetBSD__)
|
|
|
|
#include <sys/param.h>
|
|
|
|
#ifndef statfs
|
|
|
|
#define statfs statvfs
|
|
|
|
#endif
|
|
|
|
#elif defined(__sun)
|
|
|
|
#include <sys/fs/ufs_quota.h> /* quotactl */
|
|
|
|
#else
|
|
|
|
#include <sys/quota.h> /* quotactl() */
|
|
|
|
#endif
|
|
|
|
#ifdef HAVE_GETMNTENT
|
|
|
|
#ifdef __sun
|
|
|
|
#include <fcntl.h>
|
|
|
|
#include <stdio.h>
|
|
|
|
#include <sys/mntent.h>
|
|
|
|
#include <sys/mnttab.h>
|
|
|
|
#define _PATH_MOUNTED MNTTAB
|
|
|
|
#else
|
|
|
|
#include <mntent.h>
|
|
|
|
#include <paths.h> /* _PATH_MOUNTED */
|
|
|
|
#endif
|
|
|
|
#else /* BSD derived systems */
|
|
|
|
#include <sys/param.h>
|
|
|
|
#include <sys/ucred.h>
|
|
|
|
#include <sys/mount.h>
|
|
|
|
#endif
|
2013-07-08 17:07:31 +00:00
|
|
|
#endif
|
|
|
|
|
2014-07-03 19:20:12 +00:00
|
|
|
#ifdef __APPLE__
|
2017-04-19 12:04:45 +00:00
|
|
|
#ifndef HAVE_SYS_STATVFS_H
|
|
|
|
#define HAVE_SYS_STATVFS_H
|
|
|
|
#endif
|
|
|
|
#ifndef HAVE_STATVFS
|
|
|
|
#define HAVE_STATVFS
|
|
|
|
#endif
|
2013-07-08 17:07:31 +00:00
|
|
|
#endif
|
|
|
|
|
|
|
|
#ifdef HAVE_SYS_STATVFS_H
|
2017-04-19 12:04:45 +00:00
|
|
|
#include <sys/statvfs.h>
|
2013-07-08 17:07:31 +00:00
|
|
|
#endif
|
|
|
|
|
|
|
|
#ifdef HAVE_XFS_XFS_H
|
2017-04-19 12:04:45 +00:00
|
|
|
#define HAVE_XQM
|
|
|
|
#include <xfs/xqm.h>
|
2013-07-08 17:07:31 +00:00
|
|
|
#endif
|
|
|
|
|
2023-06-29 15:23:00 +00:00
|
|
|
#include "libtransmission/error.h"
|
|
|
|
#include "libtransmission/file.h"
|
2023-04-14 19:33:23 +00:00
|
|
|
#include "libtransmission/tr-macros.h"
|
2023-11-03 17:03:26 +00:00
|
|
|
#include "libtransmission/utils.h" // tr_win32_utf8_to_native
|
2013-07-08 17:07:31 +00:00
|
|
|
|
2023-01-07 18:58:16 +00:00
|
|
|
namespace
|
|
|
|
{
|
2023-06-29 15:23:00 +00:00
|
|
|
struct tr_device_info
|
|
|
|
{
|
|
|
|
std::string path;
|
|
|
|
std::string device;
|
|
|
|
std::string fstype;
|
|
|
|
};
|
|
|
|
|
2014-07-04 00:00:07 +00:00
|
|
|
#ifndef _WIN32
|
2017-04-19 12:04:45 +00:00
|
|
|
|
2023-01-07 18:58:16 +00:00
|
|
|
[[nodiscard]] char const* getdev(std::string_view path)
|
2013-07-08 17:07:31 +00:00
|
|
|
{
|
|
|
|
#ifdef HAVE_GETMNTENT
|
|
|
|
|
|
|
|
#ifdef __sun
|
2017-04-19 12:04:45 +00:00
|
|
|
|
2021-10-29 22:43:25 +00:00
|
|
|
FILE* const fp = fopen(_PATH_MOUNTED, "r");
|
2021-09-15 00:18:09 +00:00
|
|
|
if (fp == nullptr)
|
2017-04-19 12:04:45 +00:00
|
|
|
{
|
2021-09-15 00:18:09 +00:00
|
|
|
return nullptr;
|
2017-04-19 12:04:45 +00:00
|
|
|
}
|
|
|
|
|
2021-10-29 22:43:25 +00:00
|
|
|
struct mnttab mnt;
|
2017-04-22 18:39:24 +00:00
|
|
|
while (getmntent(fp, &mnt) != -1)
|
2017-04-19 12:04:45 +00:00
|
|
|
{
|
2021-11-13 18:09:14 +00:00
|
|
|
if (mnt.mnt_mountp != nullptr && path == mnt.mnt_mountp)
|
2017-04-19 12:04:45 +00:00
|
|
|
{
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
fclose(fp);
|
|
|
|
return mnt.mnt_special;
|
|
|
|
|
2013-07-08 17:07:31 +00:00
|
|
|
#else
|
|
|
|
|
2021-10-29 22:43:25 +00:00
|
|
|
FILE* const fp = setmntent(_PATH_MOUNTED, "r");
|
2021-09-15 00:18:09 +00:00
|
|
|
if (fp == nullptr)
|
2017-04-19 12:04:45 +00:00
|
|
|
{
|
2021-09-15 00:18:09 +00:00
|
|
|
return nullptr;
|
2017-04-19 12:04:45 +00:00
|
|
|
}
|
|
|
|
|
2021-10-29 22:43:25 +00:00
|
|
|
struct mntent const* mnt = nullptr;
|
2021-09-15 00:18:09 +00:00
|
|
|
while ((mnt = getmntent(fp)) != nullptr)
|
2017-04-19 12:04:45 +00:00
|
|
|
{
|
2021-11-13 18:09:14 +00:00
|
|
|
if (mnt->mnt_dir != nullptr && path == mnt->mnt_dir)
|
2017-04-19 12:04:45 +00:00
|
|
|
{
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
2013-07-08 17:07:31 +00:00
|
|
|
|
2017-04-19 12:04:45 +00:00
|
|
|
endmntent(fp);
|
2021-09-15 00:18:09 +00:00
|
|
|
return mnt != nullptr ? mnt->mnt_fsname : nullptr;
|
2013-07-08 17:07:31 +00:00
|
|
|
|
|
|
|
#endif
|
2017-04-19 12:04:45 +00:00
|
|
|
|
2013-07-08 17:07:31 +00:00
|
|
|
#else /* BSD derived systems */
|
|
|
|
|
2021-10-29 22:43:25 +00:00
|
|
|
struct statfs* mnt = nullptr;
|
|
|
|
int const n = getmntinfo(&mnt, MNT_WAIT);
|
2017-04-30 16:25:26 +00:00
|
|
|
if (n == 0)
|
2017-04-19 12:04:45 +00:00
|
|
|
{
|
2021-09-15 00:18:09 +00:00
|
|
|
return nullptr;
|
2017-04-19 12:04:45 +00:00
|
|
|
}
|
2013-07-08 17:07:31 +00:00
|
|
|
|
2017-05-13 22:38:31 +00:00
|
|
|
for (int i = 0; i < n; i++)
|
2017-04-19 12:04:45 +00:00
|
|
|
{
|
2022-05-04 23:54:41 +00:00
|
|
|
if (path == mnt[i].f_mntonname)
|
2017-04-19 12:04:45 +00:00
|
|
|
{
|
2017-05-13 22:38:31 +00:00
|
|
|
return mnt[i].f_mntfromname;
|
2017-04-19 12:04:45 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-09-15 00:18:09 +00:00
|
|
|
return nullptr;
|
2013-07-08 17:07:31 +00:00
|
|
|
|
|
|
|
#endif
|
|
|
|
}
|
|
|
|
|
2023-01-07 18:58:16 +00:00
|
|
|
[[nodiscard]] char const* getfstype(std::string_view device)
|
2013-07-08 17:07:31 +00:00
|
|
|
{
|
|
|
|
#ifdef HAVE_GETMNTENT
|
|
|
|
|
|
|
|
#ifdef __sun
|
2017-04-19 12:04:45 +00:00
|
|
|
|
2021-10-29 22:43:25 +00:00
|
|
|
FILE* const fp = fopen(_PATH_MOUNTED, "r");
|
2021-09-15 00:18:09 +00:00
|
|
|
if (fp == nullptr)
|
2017-04-19 12:04:45 +00:00
|
|
|
{
|
2021-09-15 00:18:09 +00:00
|
|
|
return nullptr;
|
2017-04-19 12:04:45 +00:00
|
|
|
}
|
|
|
|
|
2021-10-29 22:43:25 +00:00
|
|
|
struct mnttab mnt;
|
2017-04-22 18:39:24 +00:00
|
|
|
while (getmntent(fp, &mnt) != -1)
|
2017-04-19 12:04:45 +00:00
|
|
|
{
|
2022-04-05 17:45:26 +00:00
|
|
|
if (device == mnt.mnt_mountp)
|
2017-04-19 12:04:45 +00:00
|
|
|
{
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
fclose(fp);
|
|
|
|
return mnt.mnt_fstype;
|
|
|
|
|
2013-07-08 17:07:31 +00:00
|
|
|
#else
|
|
|
|
|
2021-10-29 22:43:25 +00:00
|
|
|
FILE* const fp = setmntent(_PATH_MOUNTED, "r");
|
2021-09-15 00:18:09 +00:00
|
|
|
if (fp == nullptr)
|
2017-04-19 12:04:45 +00:00
|
|
|
{
|
2021-09-15 00:18:09 +00:00
|
|
|
return nullptr;
|
2017-04-19 12:04:45 +00:00
|
|
|
}
|
|
|
|
|
2021-10-29 22:43:25 +00:00
|
|
|
struct mntent const* mnt = nullptr;
|
2021-09-15 00:18:09 +00:00
|
|
|
while ((mnt = getmntent(fp)) != nullptr)
|
2017-04-19 12:04:45 +00:00
|
|
|
{
|
2022-04-05 17:45:26 +00:00
|
|
|
if (device == mnt->mnt_fsname)
|
2017-04-19 12:04:45 +00:00
|
|
|
{
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
2013-07-08 17:07:31 +00:00
|
|
|
|
2017-04-19 12:04:45 +00:00
|
|
|
endmntent(fp);
|
2021-09-15 00:18:09 +00:00
|
|
|
return mnt != nullptr ? mnt->mnt_type : nullptr;
|
2013-07-08 17:07:31 +00:00
|
|
|
|
|
|
|
#endif
|
2017-04-19 12:04:45 +00:00
|
|
|
|
2013-07-08 17:07:31 +00:00
|
|
|
#else /* BSD derived systems */
|
|
|
|
|
2021-10-29 22:43:25 +00:00
|
|
|
struct statfs* mnt = nullptr;
|
|
|
|
int const n = getmntinfo(&mnt, MNT_WAIT);
|
2017-04-30 16:25:26 +00:00
|
|
|
if (n == 0)
|
2017-04-19 12:04:45 +00:00
|
|
|
{
|
2021-09-15 00:18:09 +00:00
|
|
|
return nullptr;
|
2017-04-19 12:04:45 +00:00
|
|
|
}
|
2013-07-08 17:07:31 +00:00
|
|
|
|
2017-05-13 22:38:31 +00:00
|
|
|
for (int i = 0; i < n; i++)
|
2017-04-19 12:04:45 +00:00
|
|
|
{
|
2022-04-05 17:45:26 +00:00
|
|
|
if (device == mnt[i].f_mntfromname)
|
2017-04-19 12:04:45 +00:00
|
|
|
{
|
2017-05-13 22:38:31 +00:00
|
|
|
return mnt[i].f_fstypename;
|
2017-04-19 12:04:45 +00:00
|
|
|
}
|
|
|
|
}
|
2013-07-08 17:07:31 +00:00
|
|
|
|
2021-09-15 00:18:09 +00:00
|
|
|
return nullptr;
|
2013-07-08 17:07:31 +00:00
|
|
|
|
|
|
|
#endif
|
|
|
|
}
|
|
|
|
|
2023-01-07 18:58:16 +00:00
|
|
|
std::string getblkdev(std::string_view path)
|
2013-07-08 17:07:31 +00:00
|
|
|
{
|
2017-04-19 12:04:45 +00:00
|
|
|
for (;;)
|
2013-07-08 17:07:31 +00:00
|
|
|
{
|
2021-11-18 00:17:09 +00:00
|
|
|
if (auto const* const device = getdev(path); device != nullptr)
|
2017-04-19 12:04:45 +00:00
|
|
|
{
|
2021-11-13 18:09:14 +00:00
|
|
|
return device;
|
2017-04-19 12:04:45 +00:00
|
|
|
}
|
|
|
|
|
2021-11-13 18:09:14 +00:00
|
|
|
auto const pos = path.rfind('/');
|
2022-02-07 04:28:36 +00:00
|
|
|
if (pos == std::string_view::npos)
|
2017-04-19 12:04:45 +00:00
|
|
|
{
|
2021-11-13 18:09:14 +00:00
|
|
|
return {};
|
2017-04-19 12:04:45 +00:00
|
|
|
}
|
2013-07-08 17:07:31 +00:00
|
|
|
|
2021-11-13 18:09:14 +00:00
|
|
|
path = path.substr(0, pos);
|
|
|
|
}
|
2013-07-08 17:07:31 +00:00
|
|
|
}
|
|
|
|
|
2017-04-30 16:25:26 +00:00
|
|
|
#if defined(__NetBSD__) && __NetBSD_Version__ >= 600000000
|
2017-04-19 12:04:45 +00:00
|
|
|
|
2021-09-12 17:41:49 +00:00
|
|
|
extern "C"
|
|
|
|
{
|
2014-04-27 00:02:00 +00:00
|
|
|
#include <quota.h>
|
2021-09-12 17:41:49 +00:00
|
|
|
}
|
2014-04-27 00:02:00 +00:00
|
|
|
|
2023-06-29 15:23:00 +00:00
|
|
|
[[nodiscard]] tr_sys_path_capacity getquota(char const* device)
|
2014-04-27 00:02:00 +00:00
|
|
|
{
|
2017-04-19 12:04:45 +00:00
|
|
|
struct quotahandle* qh;
|
|
|
|
struct quotakey qk;
|
|
|
|
struct quotaval qv;
|
2023-06-29 15:23:00 +00:00
|
|
|
struct tr_sys_path_capacity disk_space = { -1, -1 };
|
2017-04-19 12:04:45 +00:00
|
|
|
|
|
|
|
qh = quota_open(device);
|
|
|
|
|
2021-09-15 00:18:09 +00:00
|
|
|
if (qh == nullptr)
|
2017-04-19 12:04:45 +00:00
|
|
|
{
|
2021-10-14 17:07:16 +00:00
|
|
|
return disk_space;
|
2017-04-19 12:04:45 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
qk.qk_idtype = QUOTA_IDTYPE_USER;
|
|
|
|
qk.qk_id = getuid();
|
|
|
|
qk.qk_objtype = QUOTA_OBJTYPE_BLOCKS;
|
|
|
|
|
|
|
|
if (quota_get(qh, &qk, &qv) == -1)
|
|
|
|
{
|
|
|
|
quota_close(qh);
|
2021-10-14 17:07:16 +00:00
|
|
|
return disk_space;
|
2017-04-19 12:04:45 +00:00
|
|
|
}
|
|
|
|
|
2021-10-29 22:43:25 +00:00
|
|
|
int64_t limit;
|
2017-04-19 12:04:45 +00:00
|
|
|
if (qv.qv_softlimit > 0)
|
|
|
|
{
|
|
|
|
limit = qv.qv_softlimit;
|
|
|
|
}
|
|
|
|
else if (qv.qv_hardlimit > 0)
|
|
|
|
{
|
|
|
|
limit = qv.qv_hardlimit;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
quota_close(qh);
|
2021-10-14 17:07:16 +00:00
|
|
|
return disk_space;
|
2017-04-19 12:04:45 +00:00
|
|
|
}
|
|
|
|
|
2021-10-29 22:43:25 +00:00
|
|
|
int64_t const spaceused = qv.qv_usage;
|
2014-04-27 00:02:00 +00:00
|
|
|
quota_close(qh);
|
|
|
|
|
2021-10-29 22:43:25 +00:00
|
|
|
int64_t const freespace = limit - spaceused;
|
2021-10-14 17:07:16 +00:00
|
|
|
disk_space.free = freespace < 0 ? 0 : freespace;
|
|
|
|
disk_space.total = limit;
|
|
|
|
return disk_space;
|
2014-04-27 00:02:00 +00:00
|
|
|
}
|
2017-04-19 12:04:45 +00:00
|
|
|
|
2014-04-27 00:02:00 +00:00
|
|
|
#else
|
2017-04-19 12:04:45 +00:00
|
|
|
|
2023-06-29 15:23:00 +00:00
|
|
|
[[nodiscard]] tr_sys_path_capacity getquota(char const* device)
|
2013-07-08 17:07:31 +00:00
|
|
|
{
|
2017-06-19 10:45:09 +00:00
|
|
|
#if defined(__DragonFly__)
|
2022-09-23 05:51:15 +00:00
|
|
|
struct ufs_dqblk dq = {};
|
2017-06-19 10:45:09 +00:00
|
|
|
#else
|
2022-09-23 05:51:15 +00:00
|
|
|
struct dqblk dq = {};
|
2017-06-19 10:45:09 +00:00
|
|
|
#endif
|
2023-06-29 15:23:00 +00:00
|
|
|
auto disk_space = tr_sys_path_capacity{ -1, -1 };
|
2013-07-08 17:07:31 +00:00
|
|
|
|
2017-06-19 10:45:09 +00:00
|
|
|
#if defined(__FreeBSD__) || defined(__OpenBSD__) || defined(__DragonFly__) || defined(__APPLE__)
|
2021-08-15 09:41:48 +00:00
|
|
|
if (quotactl(device, QCMD(Q_GETQUOTA, USRQUOTA), getuid(), (caddr_t)&dq) != 0)
|
2013-07-08 17:07:31 +00:00
|
|
|
{
|
2021-10-14 17:07:16 +00:00
|
|
|
return disk_space;
|
2021-08-15 09:41:48 +00:00
|
|
|
}
|
2013-07-08 17:07:31 +00:00
|
|
|
#elif defined(__sun)
|
2017-04-19 12:04:45 +00:00
|
|
|
struct quotctl op;
|
|
|
|
int fd = open(device, O_RDONLY);
|
|
|
|
|
|
|
|
if (fd < 0)
|
2013-07-08 17:07:31 +00:00
|
|
|
{
|
2021-10-14 17:07:16 +00:00
|
|
|
return disk_space;
|
2017-04-19 12:04:45 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
op.op = Q_GETQUOTA;
|
|
|
|
op.uid = getuid();
|
|
|
|
op.addr = (caddr_t)&dq;
|
|
|
|
|
2021-08-15 09:41:48 +00:00
|
|
|
if (ioctl(fd, Q_QUOTACTL, &op) != 0)
|
2017-04-19 12:04:45 +00:00
|
|
|
{
|
|
|
|
close(fd);
|
2021-10-14 17:07:16 +00:00
|
|
|
return disk_space;
|
2021-08-15 09:41:48 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
close(fd);
|
2013-07-08 17:07:31 +00:00
|
|
|
#else
|
2023-01-28 21:26:23 +00:00
|
|
|
if (quotactl(QCMD(Q_GETQUOTA, USRQUOTA), device, getuid(), reinterpret_cast<caddr_t>(&dq)) != 0)
|
2013-07-08 17:07:31 +00:00
|
|
|
{
|
2021-10-14 17:07:16 +00:00
|
|
|
return disk_space;
|
2021-08-15 09:41:48 +00:00
|
|
|
}
|
2013-07-08 17:07:31 +00:00
|
|
|
#endif
|
2021-08-15 09:41:48 +00:00
|
|
|
|
2021-10-29 22:43:25 +00:00
|
|
|
int64_t limit = 0;
|
2021-08-15 09:41:48 +00:00
|
|
|
if (dq.dqb_bsoftlimit > 0)
|
|
|
|
{
|
|
|
|
/* Use soft limit first */
|
|
|
|
limit = dq.dqb_bsoftlimit;
|
|
|
|
}
|
|
|
|
else if (dq.dqb_bhardlimit > 0)
|
|
|
|
{
|
|
|
|
limit = dq.dqb_bhardlimit;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
/* No quota enabled for this user */
|
2021-10-14 17:07:16 +00:00
|
|
|
return disk_space;
|
2021-08-15 09:41:48 +00:00
|
|
|
}
|
2017-04-19 12:04:45 +00:00
|
|
|
|
2017-06-19 10:45:09 +00:00
|
|
|
#if defined(__FreeBSD__) || defined(__OpenBSD__) || defined(__DragonFly__)
|
2021-10-29 22:43:25 +00:00
|
|
|
int64_t const spaceused = (int64_t)dq.dqb_curblocks >> 1;
|
2014-07-03 19:20:12 +00:00
|
|
|
#elif defined(__APPLE__)
|
2021-10-29 22:43:25 +00:00
|
|
|
int64_t const spaceused = (int64_t)dq.dqb_curbytes;
|
2017-04-19 12:04:45 +00:00
|
|
|
#elif defined(__UCLIBC__) && !TR_UCLIBC_CHECK_VERSION(1, 0, 18)
|
2021-10-29 22:43:25 +00:00
|
|
|
int64_t const spaceused = (int64_t)btodb(dq.dqb_curblocks);
|
2014-12-11 20:06:07 +00:00
|
|
|
#elif defined(__sun) || (defined(_LINUX_QUOTA_VERSION) && _LINUX_QUOTA_VERSION < 2)
|
2021-10-29 22:43:25 +00:00
|
|
|
int64_t const spaceused = (int64_t)dq.dqb_curblocks >> 1;
|
2013-07-08 17:07:31 +00:00
|
|
|
#else
|
2021-10-29 22:43:25 +00:00
|
|
|
int64_t const spaceused = btodb(dq.dqb_curspace);
|
2013-07-08 17:07:31 +00:00
|
|
|
#endif
|
2017-04-19 12:04:45 +00:00
|
|
|
|
2021-10-29 22:43:25 +00:00
|
|
|
int64_t const freespace = limit - spaceused;
|
2017-04-19 12:04:45 +00:00
|
|
|
|
2014-07-03 19:20:12 +00:00
|
|
|
#ifdef __APPLE__
|
2023-08-22 02:59:47 +00:00
|
|
|
disk_space.free = std::max(int64_t{ 0 }, freespace);
|
|
|
|
disk_space.total = std::max(int64_t{ 0 }, limit);
|
2021-10-14 17:07:16 +00:00
|
|
|
return disk_space;
|
2013-07-08 17:07:31 +00:00
|
|
|
#else
|
2023-08-22 02:59:47 +00:00
|
|
|
disk_space.free = std::max(int64_t{ 0 }, freespace * 1024);
|
|
|
|
disk_space.total = std::max(int64_t{ 0 }, limit * 1024);
|
2021-10-14 17:07:16 +00:00
|
|
|
return disk_space;
|
2013-07-08 17:07:31 +00:00
|
|
|
#endif
|
|
|
|
}
|
2017-04-19 12:04:45 +00:00
|
|
|
|
2014-04-27 00:02:00 +00:00
|
|
|
#endif
|
2013-07-08 17:07:31 +00:00
|
|
|
|
|
|
|
#ifdef HAVE_XQM
|
2017-04-19 12:04:45 +00:00
|
|
|
|
2023-06-29 15:23:00 +00:00
|
|
|
[[nodiscard]] tr_sys_path_capacity getxfsquota(char const* device)
|
2013-07-08 17:07:31 +00:00
|
|
|
{
|
2023-06-29 15:23:00 +00:00
|
|
|
struct tr_sys_path_capacity disk_space = { -1, -1 };
|
2017-04-19 12:04:45 +00:00
|
|
|
struct fs_disk_quota dq;
|
2013-07-08 17:07:31 +00:00
|
|
|
|
2017-04-19 12:04:45 +00:00
|
|
|
if (quotactl(QCMD(Q_XGETQUOTA, USRQUOTA), device, getuid(), (caddr_t)&dq) == 0)
|
2013-07-08 17:07:31 +00:00
|
|
|
{
|
2021-10-29 22:43:25 +00:00
|
|
|
int64_t limit = 0;
|
2017-04-19 12:04:45 +00:00
|
|
|
if (dq.d_blk_softlimit > 0)
|
2013-07-08 17:07:31 +00:00
|
|
|
{
|
2017-04-19 12:04:45 +00:00
|
|
|
/* Use soft limit first */
|
|
|
|
limit = dq.d_blk_softlimit >> 1;
|
2013-07-08 17:07:31 +00:00
|
|
|
}
|
2017-04-19 12:04:45 +00:00
|
|
|
else if (dq.d_blk_hardlimit > 0)
|
2013-07-08 17:07:31 +00:00
|
|
|
{
|
2017-04-19 12:04:45 +00:00
|
|
|
limit = dq.d_blk_hardlimit >> 1;
|
2013-07-08 17:07:31 +00:00
|
|
|
}
|
2017-04-19 12:04:45 +00:00
|
|
|
else
|
2013-07-08 17:07:31 +00:00
|
|
|
{
|
2017-04-19 12:04:45 +00:00
|
|
|
/* No quota enabled for this user */
|
2021-10-14 17:07:16 +00:00
|
|
|
return disk_space;
|
2013-07-08 17:07:31 +00:00
|
|
|
}
|
|
|
|
|
2021-10-29 22:43:25 +00:00
|
|
|
int64_t freespace = limit - (dq.d_bcount >> 1);
|
2021-10-14 17:07:16 +00:00
|
|
|
freespace = freespace < 0 ? 0 : (freespace * 1024);
|
|
|
|
limit = limit * 1024;
|
|
|
|
disk_space.free = freespace;
|
|
|
|
disk_space.total = limit;
|
|
|
|
return disk_space;
|
2013-07-08 17:07:31 +00:00
|
|
|
}
|
|
|
|
|
2017-04-19 12:04:45 +00:00
|
|
|
/* something went wrong */
|
2021-10-14 17:07:16 +00:00
|
|
|
return disk_space;
|
2013-07-08 17:07:31 +00:00
|
|
|
}
|
2017-04-19 12:04:45 +00:00
|
|
|
|
2013-07-08 17:07:31 +00:00
|
|
|
#endif /* HAVE_XQM */
|
2017-04-19 12:04:45 +00:00
|
|
|
|
2014-07-04 00:00:07 +00:00
|
|
|
#endif /* _WIN32 */
|
2013-07-08 17:07:31 +00:00
|
|
|
|
2023-06-29 15:23:00 +00:00
|
|
|
[[nodiscard]] tr_sys_path_capacity get_quota_space([[maybe_unused]] tr_device_info const& info)
|
2013-07-08 17:07:31 +00:00
|
|
|
{
|
2023-06-29 15:23:00 +00:00
|
|
|
struct tr_sys_path_capacity ret = { -1, -1 };
|
2013-07-08 17:07:31 +00:00
|
|
|
|
2014-07-04 00:00:07 +00:00
|
|
|
#ifndef _WIN32
|
2013-07-08 17:07:31 +00:00
|
|
|
|
2021-11-13 18:09:14 +00:00
|
|
|
if (evutil_ascii_strcasecmp(info.fstype.c_str(), "xfs") == 0)
|
2013-07-08 17:07:31 +00:00
|
|
|
{
|
|
|
|
#ifdef HAVE_XQM
|
2021-11-13 18:09:14 +00:00
|
|
|
ret = getxfsquota(info.device.c_str());
|
2013-07-08 17:07:31 +00:00
|
|
|
#endif
|
|
|
|
}
|
2017-04-19 12:04:45 +00:00
|
|
|
else
|
2013-07-08 17:07:31 +00:00
|
|
|
{
|
2021-11-13 18:09:14 +00:00
|
|
|
ret = getquota(info.device.c_str());
|
2013-07-08 17:07:31 +00:00
|
|
|
}
|
2015-03-19 06:08:06 +00:00
|
|
|
|
2014-07-04 00:00:07 +00:00
|
|
|
#endif /* _WIN32 */
|
2013-07-08 17:07:31 +00:00
|
|
|
|
2017-04-19 12:04:45 +00:00
|
|
|
return ret;
|
2013-07-08 17:07:31 +00:00
|
|
|
}
|
|
|
|
|
2023-06-29 15:23:00 +00:00
|
|
|
[[nodiscard]] tr_sys_path_capacity getDiskSpace(char const* path)
|
2013-07-08 17:07:31 +00:00
|
|
|
{
|
2014-07-04 00:00:07 +00:00
|
|
|
#ifdef _WIN32
|
2013-07-08 17:07:31 +00:00
|
|
|
|
2023-06-29 15:23:00 +00:00
|
|
|
struct tr_sys_path_capacity ret = { -1, -1 };
|
2014-09-21 18:03:13 +00:00
|
|
|
|
2022-08-03 23:07:38 +00:00
|
|
|
if (auto const wide_path = tr_win32_utf8_to_native(path); !std::empty(wide_path))
|
2014-09-21 18:03:13 +00:00
|
|
|
{
|
2017-04-19 12:04:45 +00:00
|
|
|
ULARGE_INTEGER freeBytesAvailable;
|
2021-10-14 17:07:16 +00:00
|
|
|
ULARGE_INTEGER totalBytesAvailable;
|
2014-09-21 18:03:13 +00:00
|
|
|
|
2022-08-03 23:07:38 +00:00
|
|
|
if (GetDiskFreeSpaceExW(wide_path.c_str(), &freeBytesAvailable, &totalBytesAvailable, nullptr))
|
2017-04-19 12:04:45 +00:00
|
|
|
{
|
2021-10-14 17:07:16 +00:00
|
|
|
ret.free = freeBytesAvailable.QuadPart;
|
|
|
|
ret.total = totalBytesAvailable.QuadPart;
|
2017-04-19 12:04:45 +00:00
|
|
|
}
|
2014-09-21 18:03:13 +00:00
|
|
|
}
|
|
|
|
|
2017-04-19 12:04:45 +00:00
|
|
|
return ret;
|
2013-07-08 17:07:31 +00:00
|
|
|
|
|
|
|
#elif defined(HAVE_STATVFS)
|
|
|
|
|
2022-09-23 05:51:15 +00:00
|
|
|
struct statvfs buf = {};
|
2023-06-29 15:23:00 +00:00
|
|
|
return statvfs(path, &buf) != 0 ? (struct tr_sys_path_capacity){ -1, -1 } :
|
|
|
|
(struct tr_sys_path_capacity){ (int64_t)buf.f_bavail * (int64_t)buf.f_frsize,
|
|
|
|
(int64_t)buf.f_blocks * (int64_t)buf.f_frsize };
|
2013-07-08 17:07:31 +00:00
|
|
|
|
|
|
|
#else
|
|
|
|
|
2017-04-19 12:04:45 +00:00
|
|
|
#warning FIXME: not implemented
|
|
|
|
|
2021-10-14 17:07:16 +00:00
|
|
|
return { -1, -1 };
|
2013-07-08 17:07:31 +00:00
|
|
|
|
|
|
|
#endif
|
|
|
|
}
|
|
|
|
|
2021-11-13 18:09:14 +00:00
|
|
|
tr_device_info tr_device_info_create(std::string_view path)
|
2013-07-08 17:07:31 +00:00
|
|
|
{
|
2021-11-13 18:09:14 +00:00
|
|
|
auto out = tr_device_info{};
|
|
|
|
out.path = path;
|
2014-07-04 00:00:07 +00:00
|
|
|
#ifndef _WIN32
|
2021-11-13 18:09:14 +00:00
|
|
|
out.device = getblkdev(out.path);
|
2023-01-04 03:43:54 +00:00
|
|
|
auto const* const fstype = getfstype(out.path);
|
2022-02-07 04:28:36 +00:00
|
|
|
out.fstype = fstype != nullptr ? fstype : "";
|
2013-07-08 17:07:31 +00:00
|
|
|
#endif
|
2021-11-13 18:09:14 +00:00
|
|
|
return out;
|
2013-07-08 17:07:31 +00:00
|
|
|
}
|
|
|
|
|
2023-06-29 15:23:00 +00:00
|
|
|
tr_sys_path_capacity tr_device_info_get_disk_space(struct tr_device_info const& info)
|
2013-07-08 17:07:31 +00:00
|
|
|
{
|
2021-11-13 18:09:14 +00:00
|
|
|
if (std::empty(info.path))
|
2013-07-08 17:07:31 +00:00
|
|
|
{
|
2021-11-13 18:09:14 +00:00
|
|
|
errno = EINVAL;
|
|
|
|
return { -1, -1 };
|
2013-07-08 17:07:31 +00:00
|
|
|
}
|
|
|
|
|
2023-06-29 15:23:00 +00:00
|
|
|
auto space = get_quota_space(info);
|
2013-07-08 17:07:31 +00:00
|
|
|
|
2021-11-13 18:09:14 +00:00
|
|
|
if (space.free < 0 || space.total < 0)
|
2013-07-08 17:07:31 +00:00
|
|
|
{
|
2021-11-13 18:09:14 +00:00
|
|
|
space = getDiskSpace(info.path.c_str());
|
2013-07-08 17:07:31 +00:00
|
|
|
}
|
|
|
|
|
2021-10-14 17:07:16 +00:00
|
|
|
return space;
|
2013-07-08 17:07:31 +00:00
|
|
|
}
|
2023-06-29 15:23:00 +00:00
|
|
|
|
|
|
|
} // namespace
|
|
|
|
|
2023-11-04 16:39:41 +00:00
|
|
|
std::optional<tr_sys_path_capacity> tr_sys_path_get_capacity(std::string_view path, tr_error* error)
|
2023-06-29 15:23:00 +00:00
|
|
|
{
|
2023-11-04 16:39:41 +00:00
|
|
|
auto local_error = tr_error{};
|
|
|
|
if (error == nullptr)
|
|
|
|
{
|
|
|
|
error = &local_error;
|
|
|
|
}
|
|
|
|
|
|
|
|
auto const info = tr_sys_path_get_info(path, 0, &local_error);
|
2023-06-29 15:23:00 +00:00
|
|
|
if (!info)
|
|
|
|
{
|
|
|
|
return {};
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!info->isFolder())
|
|
|
|
{
|
2023-11-04 16:39:41 +00:00
|
|
|
error->set_from_errno(ENOTDIR);
|
2023-06-29 15:23:00 +00:00
|
|
|
return {};
|
|
|
|
}
|
|
|
|
|
|
|
|
auto const device = tr_device_info_create(path);
|
|
|
|
auto capacity = tr_device_info_get_disk_space(device);
|
|
|
|
if (capacity.free < 0 || capacity.total < 0)
|
|
|
|
{
|
2023-11-04 16:39:41 +00:00
|
|
|
error->set_from_errno(EINVAL);
|
2023-06-29 15:23:00 +00:00
|
|
|
return {};
|
|
|
|
}
|
|
|
|
|
|
|
|
return capacity;
|
|
|
|
}
|