summaryrefslogtreecommitdiffstats
path: root/lib/libalpm/diskspace.c
blob: a5beb84ff4dcf602c8cbf5f4fad43f545a043cb6 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
/*
 *  diskspace.c
 *
 *  Copyright (c) 2010 Pacman Development Team <pacman-dev@archlinux.org>
 *
 *  This program is free software; you can redistribute it and/or modify
 *  it under the terms of the GNU General Public License as published by
 *  the Free Software Foundation; either version 2 of the License, or
 *  (at your option) any later version.
 *
 *  This program is distributed in the hope that it will be useful,
 *  but WITHOUT ANY WARRANTY; without even the implied warranty of
 *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 *  GNU General Public License for more details.
 *
 *  You should have received a copy of the GNU General Public License
 *  along with this program.  If not, see <http://www.gnu.org/licenses/>.
 */

#include "config.h"

#if defined HAVE_GETMNTENT
#include <mntent.h>
#include <sys/statvfs.h>
#elif defined HAVE_GETMNTINFO_STATFS
#include <sys/param.h>
#include <sys/mount.h>
#if HAVE_SYS_UCRED_H
#include <sys/ucred.h>
#endif
#elif defined HAVE_GETMNTINFO_STATVFS
#include <sys/types.h>
#include <sys/statvfs.h>
#endif

/* libalpm */
#include "diskspace.h"
#include "alpm_list.h"
#include "util.h"
#include "log.h"

static alpm_list_t *mount_point_list()
{
	alpm_list_t *mount_points = NULL;
	alpm_mountpoint_t *mp;

#if defined HAVE_GETMNTENT
	struct mntent *mnt;
	FILE *fp;
	struct statvfs fsp;

	fp = setmntent(MOUNTED, "r");

	if (fp == NULL) {
		return NULL;
	}

	while((mnt = getmntent (fp))) {
		if(statvfs(mnt->mnt_dir, &fsp) != 0) {
			_alpm_log(PM_LOG_WARNING, "could not get filesystem information for %s\n", mnt->mnt_dir);
			continue;
		}

		MALLOC(mp, sizeof(alpm_mountpoint_t), RET_ERR(PM_ERR_MEMORY, NULL));
		mp->mount_dir = strdup(mnt->mnt_dir);

		MALLOC(mp->fsp, sizeof(struct statvfs), RET_ERR(PM_ERR_MEMORY, NULL));
		memcpy((void *)(mp->fsp), (void *)(&fsp), sizeof(struct statvfs));

		mp->blocks_needed = 0;
		mp->max_blocks_needed = 0;
		mp->used = 0;

		mount_points = alpm_list_add(mount_points, mp);
	}

	endmntent(fp);
#elif defined HAVE_GETMNTINFO_STATFS
	int entries;
	struct statfs *fsp;

	entries = getmntinfo(&fsp, MNT_NOWAIT);

	if (entries < 0) {
		return NULL;
	}

	for(; entries-- > 0; fsp++) {
		MALLOC(mp, sizeof(alpm_mountpoint_t), RET_ERR(PM_ERR_MEMORY, NULL));
		mp->mount_dir = strdup(fsp->f_mntonname);

		MALLOC(mp->fsp, sizeof(struct statfs), RET_ERR(PM_ERR_MEMORY, NULL));
		memcpy((void *)(mp->fsp), (void *)fsp, sizeof(struct statfs));

		mp->blocks_needed = 0;
		mp->max_blocks_needed = 0;

		mount_points = alpm_list_add(mount_points, mp);
	}
#elif defined HAVE_GETMNTINFO_STATVFS
	int entries;
	struct statvfs *fsp;

	entries = getmntinfo(&fsp, MNT_NOWAIT);

	if (entries < 0) {
		return NULL;
	}

	for (; entries-- > 0; fsp++) {
		MALLOC(mp, sizeof(alpm_mountpoint_t), RET_ERR(PM_ERR_MEMORY, NULL));
		mp->mount_dir = strdup(fsp->f_mntonname);

		MALLOC(mp->fsp, sizeof(struct statvfs), RET_ERR(PM_ERR_MEMORY, NULL));
		memcpy((void *)(mp->fsp), (void *)fsp, sizeof(struct statvfs));

		mp->blocks_needed = 0;
		mp->max_blocks_needed = 0;

		mount_points = alpm_list_add(mount_points, mp);
	}
#endif

	return(mount_points);
}

int _alpm_check_diskspace(pmtrans_t *trans, pmdb_t *db)
{
	alpm_list_t *mount_points;

	mount_points = mount_point_list();
	if(mount_points == NULL) {
		_alpm_log(PM_LOG_ERROR, _("count not determine filesystem mount points"));
		return -1;
	}

	for(i = mount_points; i; i = alpm_list_next(i)) {
		alpm_mountpoint_t *data = i->data;
		FREE(data->mount_dir);
		FREE(data->fsp);
	}
	FREELIST(mount_points);

	return 0;
}

/* vim: set ts=2 sw=2 noet: */