2013-07-08 17:07:31 +00:00
|
|
|
/*
|
2014-01-19 01:09:44 +00:00
|
|
|
* This file Copyright (C) 2013-2014 Mnemosyne LLC
|
2013-07-08 17:07:31 +00:00
|
|
|
*
|
2014-01-21 03:10:30 +00:00
|
|
|
* It may be used under the GNU GPL versions 2 or 3
|
2014-01-19 01:09:44 +00:00
|
|
|
* or any future license endorsed by Mnemosyne LLC.
|
2013-07-08 17:07:31 +00:00
|
|
|
*
|
2014-01-19 01:29:38 +00:00
|
|
|
* $Id$
|
2013-07-08 17:07:31 +00:00
|
|
|
*/
|
|
|
|
|
|
|
|
#include <errno.h>
|
|
|
|
#include <stdlib.h>
|
|
|
|
#include <string.h>
|
2014-12-13 15:22:39 +00:00
|
|
|
|
2013-07-08 17:07:31 +00:00
|
|
|
#include <event2/util.h> /* evutil_ascii_strcasecmp () */
|
|
|
|
|
2014-07-04 00:00:07 +00:00
|
|
|
#ifndef _WIN32
|
2014-12-13 15:22:39 +00:00
|
|
|
#include <unistd.h> /* getuid() */
|
2013-07-08 17:07:31 +00:00
|
|
|
#include <sys/types.h> /* types needed by quota.h */
|
2013-07-24 00:02:16 +00:00
|
|
|
#if defined(__FreeBSD__) || defined(__OpenBSD__)
|
2013-07-08 17:07:31 +00:00
|
|
|
#include <ufs/ufs/quota.h> /* quotactl() */
|
2014-04-27 00:02:00 +00:00
|
|
|
#elif defined (__NetBSD__)
|
|
|
|
#include <sys/param.h>
|
|
|
|
#ifndef statfs
|
|
|
|
#define statfs statvfs
|
|
|
|
#endif
|
2013-07-08 17:07:31 +00:00
|
|
|
#elif defined (__sun)
|
|
|
|
#include <sys/fs/ufs_quota.h> /* quotactl */
|
|
|
|
#else
|
|
|
|
#include <sys/quota.h> /* quotactl() */
|
|
|
|
#endif
|
|
|
|
#ifdef HAVE_GETMNTENT
|
|
|
|
#ifdef __sun
|
2013-07-14 23:00:39 +00:00
|
|
|
#include <fcntl.h>
|
2013-07-08 17:07:31 +00:00
|
|
|
#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
|
|
|
|
#endif
|
|
|
|
|
2014-07-03 19:20:12 +00:00
|
|
|
#ifdef __APPLE__
|
2014-12-11 05:11:02 +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
|
|
|
|
#include <sys/statvfs.h>
|
|
|
|
#endif
|
|
|
|
|
|
|
|
#ifdef HAVE_XFS_XFS_H
|
|
|
|
#define HAVE_XQM
|
|
|
|
#include <xfs/xqm.h>
|
|
|
|
#endif
|
|
|
|
|
|
|
|
#include "transmission.h"
|
|
|
|
#include "utils.h"
|
|
|
|
#include "platform-quota.h"
|
|
|
|
|
|
|
|
/***
|
|
|
|
****
|
|
|
|
***/
|
|
|
|
|
2014-07-04 00:00:07 +00:00
|
|
|
#ifndef _WIN32
|
2013-07-08 17:07:31 +00:00
|
|
|
static const char *
|
|
|
|
getdev (const char * path)
|
|
|
|
{
|
|
|
|
#ifdef HAVE_GETMNTENT
|
|
|
|
|
|
|
|
FILE * fp;
|
|
|
|
|
|
|
|
#ifdef __sun
|
2013-07-14 23:00:39 +00:00
|
|
|
struct mnttab mnt;
|
2013-07-08 17:07:31 +00:00
|
|
|
fp = fopen(_PATH_MOUNTED, "r");
|
|
|
|
if (fp == NULL)
|
|
|
|
return NULL;
|
|
|
|
|
2013-07-14 23:00:39 +00:00
|
|
|
while (getmntent(fp, &mnt))
|
|
|
|
if (!tr_strcmp0 (path, mnt.mnt_mountp))
|
2013-07-08 17:07:31 +00:00
|
|
|
break;
|
|
|
|
fclose(fp);
|
2013-07-14 23:00:39 +00:00
|
|
|
return mnt.mnt_special;
|
2013-07-08 17:07:31 +00:00
|
|
|
#else
|
|
|
|
struct mntent * mnt;
|
|
|
|
|
|
|
|
fp = setmntent(_PATH_MOUNTED, "r");
|
|
|
|
if (fp == NULL)
|
|
|
|
return NULL;
|
|
|
|
|
|
|
|
while ((mnt = getmntent(fp)) != NULL)
|
|
|
|
if (!tr_strcmp0 (path, mnt->mnt_dir))
|
|
|
|
break;
|
|
|
|
|
|
|
|
endmntent(fp);
|
|
|
|
return mnt ? mnt->mnt_fsname : NULL;
|
|
|
|
#endif
|
|
|
|
#else /* BSD derived systems */
|
|
|
|
|
|
|
|
int i;
|
|
|
|
int n;
|
|
|
|
struct statfs * mnt;
|
|
|
|
|
|
|
|
n = getmntinfo(&mnt, MNT_WAIT);
|
|
|
|
if (!n)
|
|
|
|
return NULL;
|
|
|
|
|
|
|
|
for (i=0; i<n; i++)
|
|
|
|
if (!tr_strcmp0 (path, mnt[i].f_mntonname))
|
|
|
|
break;
|
|
|
|
|
|
|
|
return (i < n) ? mnt[i].f_mntfromname : NULL;
|
|
|
|
|
|
|
|
#endif
|
|
|
|
}
|
|
|
|
|
|
|
|
static const char *
|
|
|
|
getfstype (const char * device)
|
|
|
|
{
|
|
|
|
|
|
|
|
#ifdef HAVE_GETMNTENT
|
|
|
|
|
|
|
|
FILE * fp;
|
|
|
|
#ifdef __sun
|
2013-07-14 23:00:39 +00:00
|
|
|
struct mnttab mnt;
|
2013-07-08 17:07:31 +00:00
|
|
|
fp = fopen(_PATH_MOUNTED, "r");
|
|
|
|
if (fp == NULL)
|
|
|
|
return NULL;
|
2013-07-14 23:00:39 +00:00
|
|
|
while (getmntent(fp, &mnt))
|
|
|
|
if (!tr_strcmp0 (device, mnt.mnt_mountp))
|
2013-07-08 17:07:31 +00:00
|
|
|
break;
|
|
|
|
fclose(fp);
|
2013-07-14 23:00:39 +00:00
|
|
|
return mnt.mnt_fstype;
|
2013-07-08 17:07:31 +00:00
|
|
|
#else
|
|
|
|
struct mntent *mnt;
|
|
|
|
|
|
|
|
fp = setmntent (_PATH_MOUNTED, "r");
|
|
|
|
if (fp == NULL)
|
|
|
|
return NULL;
|
|
|
|
|
|
|
|
while ((mnt = getmntent (fp)) != NULL)
|
|
|
|
if (!tr_strcmp0 (device, mnt->mnt_fsname))
|
|
|
|
break;
|
|
|
|
|
|
|
|
endmntent(fp);
|
|
|
|
return mnt ? mnt->mnt_type : NULL;
|
|
|
|
#endif
|
|
|
|
#else /* BSD derived systems */
|
|
|
|
|
|
|
|
int i;
|
|
|
|
int n;
|
|
|
|
struct statfs *mnt;
|
|
|
|
|
|
|
|
n = getmntinfo(&mnt, MNT_WAIT);
|
|
|
|
if (!n)
|
|
|
|
return NULL;
|
|
|
|
|
|
|
|
for (i=0; i<n; i++)
|
|
|
|
if (!tr_strcmp0 (device, mnt[i].f_mntfromname))
|
|
|
|
break;
|
|
|
|
|
|
|
|
return (i < n) ? mnt[i].f_fstypename : NULL;
|
|
|
|
|
|
|
|
#endif
|
|
|
|
}
|
|
|
|
|
|
|
|
static const char *
|
|
|
|
getblkdev (const char * path)
|
|
|
|
{
|
|
|
|
char * c;
|
|
|
|
char * dir;
|
|
|
|
const char * device;
|
|
|
|
|
|
|
|
dir = tr_strdup(path);
|
|
|
|
|
|
|
|
for (;;)
|
|
|
|
{
|
|
|
|
device = getdev (dir);
|
|
|
|
if (device != NULL)
|
|
|
|
break;
|
|
|
|
|
|
|
|
c = strrchr (dir, '/');
|
|
|
|
if (c != NULL)
|
|
|
|
*c = '\0';
|
|
|
|
else
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
tr_free (dir);
|
|
|
|
return device;
|
|
|
|
}
|
|
|
|
|
2014-04-27 00:02:00 +00:00
|
|
|
#if defined(__NetBSD__) && (__NetBSD_Version__ >= 600000000)
|
|
|
|
#include <quota.h>
|
|
|
|
|
|
|
|
static int64_t
|
|
|
|
getquota (const char * device)
|
|
|
|
{
|
|
|
|
struct quotahandle *qh;
|
|
|
|
struct quotakey qk;
|
|
|
|
struct quotaval qv;
|
|
|
|
int64_t limit;
|
|
|
|
int64_t freespace;
|
|
|
|
int64_t spaceused;
|
|
|
|
|
|
|
|
qh = quota_open(device);
|
|
|
|
if (qh == NULL) {
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
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);
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
if (qv.qv_softlimit > 0) {
|
|
|
|
limit = qv.qv_softlimit;
|
|
|
|
}
|
|
|
|
else if (qv.qv_hardlimit > 0) {
|
|
|
|
limit = qv.qv_hardlimit;
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
quota_close(qh);
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
spaceused = qv.qv_usage;
|
|
|
|
quota_close(qh);
|
|
|
|
|
|
|
|
freespace = limit - spaceused;
|
|
|
|
return (freespace < 0) ? 0 : freespace;
|
|
|
|
}
|
|
|
|
#else
|
2013-07-08 17:07:31 +00:00
|
|
|
static int64_t
|
|
|
|
getquota (const char * device)
|
|
|
|
{
|
|
|
|
struct dqblk dq;
|
|
|
|
int64_t limit;
|
|
|
|
int64_t freespace;
|
|
|
|
int64_t spaceused;
|
|
|
|
|
2014-07-03 19:20:12 +00:00
|
|
|
#if defined(__FreeBSD__) || defined(__OpenBSD__) || defined(__APPLE__)
|
2013-07-08 17:07:31 +00:00
|
|
|
if (quotactl(device, QCMD(Q_GETQUOTA, USRQUOTA), getuid(), (caddr_t) &dq) == 0)
|
|
|
|
{
|
|
|
|
#elif defined(__sun)
|
2015-01-02 11:15:31 +00:00
|
|
|
struct quotctl op;
|
|
|
|
int fd = open(device, O_RDONLY);
|
|
|
|
if (fd < 0)
|
|
|
|
return -1;
|
|
|
|
op.op = Q_GETQUOTA;
|
|
|
|
op.uid = getuid();
|
|
|
|
op.addr = (caddr_t) &dq;
|
2013-07-08 17:07:31 +00:00
|
|
|
if (ioctl(fd, Q_QUOTACTL, &op) == 0)
|
|
|
|
{
|
|
|
|
close(fd);
|
|
|
|
#else
|
|
|
|
if (quotactl(QCMD(Q_GETQUOTA, USRQUOTA), device, getuid(), (caddr_t) &dq) == 0)
|
|
|
|
{
|
|
|
|
#endif
|
|
|
|
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 */
|
|
|
|
return -1;
|
|
|
|
}
|
2013-07-24 00:02:16 +00:00
|
|
|
#if defined(__FreeBSD__) || defined(__OpenBSD__)
|
2013-07-08 17:07:31 +00:00
|
|
|
spaceused = (int64_t) dq.dqb_curblocks >> 1;
|
2014-07-03 19:20:12 +00:00
|
|
|
#elif defined(__APPLE__)
|
2013-07-08 17:07:31 +00:00
|
|
|
spaceused = (int64_t) dq.dqb_curbytes;
|
|
|
|
#elif defined(__UCLIBC__)
|
|
|
|
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)
|
2013-07-08 17:07:31 +00:00
|
|
|
spaceused = (int64_t) dq.dqb_curblocks >> 1;
|
|
|
|
#else
|
|
|
|
spaceused = btodb(dq.dqb_curspace);
|
|
|
|
#endif
|
|
|
|
freespace = limit - spaceused;
|
2014-07-03 19:20:12 +00:00
|
|
|
#ifdef __APPLE__
|
2013-07-08 17:07:31 +00:00
|
|
|
return (freespace < 0) ? 0 : freespace;
|
|
|
|
#else
|
|
|
|
return (freespace < 0) ? 0 : freespace * 1024;
|
|
|
|
#endif
|
|
|
|
}
|
|
|
|
#if defined(__sun)
|
|
|
|
close(fd);
|
|
|
|
#endif
|
|
|
|
/* something went wrong */
|
|
|
|
return -1;
|
|
|
|
}
|
2014-04-27 00:02:00 +00:00
|
|
|
#endif
|
2013-07-08 17:07:31 +00:00
|
|
|
|
|
|
|
#ifdef HAVE_XQM
|
|
|
|
static int64_t
|
|
|
|
getxfsquota (char * device)
|
|
|
|
{
|
|
|
|
int64_t limit;
|
|
|
|
int64_t freespace;
|
|
|
|
struct fs_disk_quota dq;
|
|
|
|
|
|
|
|
if (quotactl(QCMD(Q_XGETQUOTA, USRQUOTA), device, getuid(), (caddr_t) &dq) == 0)
|
|
|
|
{
|
|
|
|
if (dq.d_blk_softlimit > 0)
|
|
|
|
{
|
|
|
|
/* Use soft limit first */
|
|
|
|
limit = dq.d_blk_softlimit >> 1;
|
|
|
|
}
|
|
|
|
else if (dq.d_blk_hardlimit > 0)
|
|
|
|
{
|
|
|
|
limit = dq.d_blk_hardlimit >> 1;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
/* No quota enabled for this user */
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
|
|
|
freespace = limit - (dq.d_bcount >> 1);
|
|
|
|
return (freespace < 0) ? 0 : freespace * 1024;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* something went wrong */
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
#endif /* HAVE_XQM */
|
2014-07-04 00:00:07 +00:00
|
|
|
#endif /* _WIN32 */
|
2013-07-08 17:07:31 +00:00
|
|
|
|
|
|
|
static int64_t
|
|
|
|
tr_getQuotaFreeSpace (const struct tr_device_info * info)
|
|
|
|
{
|
|
|
|
int64_t ret = -1;
|
|
|
|
|
2014-07-04 00:00:07 +00:00
|
|
|
#ifndef _WIN32
|
2013-07-08 17:07:31 +00:00
|
|
|
|
|
|
|
if (info->fstype && !evutil_ascii_strcasecmp(info->fstype, "xfs"))
|
|
|
|
{
|
|
|
|
#ifdef HAVE_XQM
|
|
|
|
ret = getxfsquota (info->device);
|
|
|
|
#endif
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
ret = getquota (info->device);
|
|
|
|
}
|
2015-03-19 06:08:06 +00:00
|
|
|
|
|
|
|
#else /* _WIN32 */
|
|
|
|
|
|
|
|
(void) info;
|
|
|
|
|
2014-07-04 00:00:07 +00:00
|
|
|
#endif /* _WIN32 */
|
2013-07-08 17:07:31 +00:00
|
|
|
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
|
|
|
static int64_t
|
|
|
|
tr_getDiskFreeSpace (const char * path)
|
|
|
|
{
|
2014-07-04 00:00:07 +00:00
|
|
|
#ifdef _WIN32
|
2013-07-08 17:07:31 +00:00
|
|
|
|
2014-09-21 18:03:13 +00:00
|
|
|
int64_t ret = -1;
|
|
|
|
wchar_t * wide_path;
|
|
|
|
|
|
|
|
wide_path = tr_win32_utf8_to_native (path, -1);
|
|
|
|
|
|
|
|
if (wide_path != NULL)
|
|
|
|
{
|
|
|
|
ULARGE_INTEGER freeBytesAvailable;
|
|
|
|
if (GetDiskFreeSpaceExW (wide_path, &freeBytesAvailable, NULL, NULL))
|
|
|
|
ret = freeBytesAvailable.QuadPart;
|
|
|
|
|
|
|
|
tr_free (wide_path);
|
|
|
|
}
|
|
|
|
|
|
|
|
return ret;
|
2013-07-08 17:07:31 +00:00
|
|
|
|
|
|
|
#elif defined(HAVE_STATVFS)
|
|
|
|
|
|
|
|
struct statvfs buf;
|
|
|
|
return statvfs(path, &buf) ? -1 : (int64_t)buf.f_bavail * (int64_t)buf.f_frsize;
|
|
|
|
|
|
|
|
#else
|
|
|
|
|
|
|
|
#warning FIXME: not implemented
|
|
|
|
return -1;
|
|
|
|
|
|
|
|
#endif
|
|
|
|
}
|
|
|
|
|
|
|
|
struct tr_device_info *
|
|
|
|
tr_device_info_create (const char * path)
|
|
|
|
{
|
|
|
|
struct tr_device_info * info;
|
|
|
|
|
|
|
|
info = tr_new0 (struct tr_device_info, 1);
|
|
|
|
info->path = tr_strdup (path);
|
2014-07-04 00:00:07 +00:00
|
|
|
#ifndef _WIN32
|
2013-07-08 17:07:31 +00:00
|
|
|
info->device = tr_strdup (getblkdev (path));
|
|
|
|
info->fstype = tr_strdup (getfstype (path));
|
|
|
|
#endif
|
|
|
|
|
|
|
|
return info;
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
tr_device_info_free (struct tr_device_info * info)
|
|
|
|
{
|
|
|
|
if (info != NULL)
|
|
|
|
{
|
|
|
|
tr_free (info->fstype);
|
|
|
|
tr_free (info->device);
|
|
|
|
tr_free (info->path);
|
|
|
|
tr_free (info);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
int64_t
|
|
|
|
tr_device_info_get_free_space (const struct tr_device_info * info)
|
|
|
|
{
|
|
|
|
int64_t free_space;
|
|
|
|
|
|
|
|
if ((info == NULL) || (info->path == NULL))
|
|
|
|
{
|
|
|
|
errno = EINVAL;
|
|
|
|
free_space = -1;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
free_space = tr_getQuotaFreeSpace (info);
|
|
|
|
|
|
|
|
if (free_space < 0)
|
|
|
|
free_space = tr_getDiskFreeSpace (info->path);
|
|
|
|
}
|
|
|
|
|
|
|
|
return free_space;
|
|
|
|
}
|
|
|
|
|
|
|
|
/***
|
|
|
|
****
|
|
|
|
***/
|