source: mainline/uspace/app/df/df.c@ 8ffedd8

lfn serial ticket/834-toolchain-update topic/msim-upgrade topic/simplify-dev-export
Last change on this file since 8ffedd8 was 35b7d86e, checked in by Jakub Jermar <jakub@…>, 9 years ago

Remove VFS_IN_MTAB_GET

The mountpoints in VFS don't know their path anymore and it does not
make much sense to maintain this global mount table when tasks can have
different roots.

  • Property mode set to 100644
File size: 5.3 KB
Line 
1/*
2 * Copyright (c) 2013 Manuele Conti
3 * All rights reserved.
4 *
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions
7 * are met:
8 *
9 * - Redistributions of source code must retain the above copyright
10 * notice, this list of conditions and the following disclaimer.
11 * - Redistributions in binary form must reproduce the above copyright
12 * notice, this list of conditions and the following disclaimer in the
13 * documentation and/or other materials provided with the distribution.
14 * - The name of the author may not be used to endorse or promote products
15 * derived from this software without specific prior written permission.
16 *
17 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
18 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
19 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
20 * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
21 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
22 * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
23 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
24 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
25 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
26 * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
27 */
28
29/** @addtogroup df
30 * @brief Df utility.
31 * @{
32 */
33/**
34 * @file
35 */
36
37#include <stdio.h>
38#include <stdlib.h>
39#include <unistd.h>
40#include <stdint.h>
41#include <getopt.h>
42#include <sys/statfs.h>
43#include <errno.h>
44#include <adt/list.h>
45#include <vfs/vfs.h>
46#include <vfs/vfs_mtab.h>
47
48#define NAME "df"
49
50#define HEADER_TABLE "Filesystem %4u-blocks Used Available Used%% Mounted on"
51#define HEADER_TABLE_HR "Filesystem Size Used Available Used%% Mounted on"
52
53#define PERCENTAGE(x, tot) (tot ? (100ULL * (x) / (tot)) : 0)
54#define FSBK_TO_BK(x, fsbk, bk) \
55 (((fsbk) != 0 && (fsbk) < (bk)) ? \
56 (unsigned long long) ((x) / ((bk) / (fsbk))) : \
57 (unsigned long long) ((x) * ((fsbk) / (bk))))
58
59static unsigned int unit_size;
60static unsigned int human_readable;
61
62static void size_to_human_readable(char *buf, uint64_t bytes);
63static void print_header(void);
64static void print_statfs(struct statfs *, char *, char *);
65static void print_usage(void);
66
67int main(int argc, char *argv[])
68{
69 int optres, errflg = 0;
70 struct statfs st;
71
72 unit_size = 512;
73 human_readable = 0;
74
75 /******************************************/
76 /* Parse command line options... */
77 /******************************************/
78 while ((optres = getopt(argc, argv, ":uhb:")) != -1) {
79 switch(optres) {
80 case 'u':
81 print_usage();
82 return 0;
83
84 case 'h':
85 human_readable = 1;
86 break;
87
88 case 'b':
89 str_uint32_t(optarg, NULL, 0, 0, &unit_size);
90 break;
91
92 case ':':
93 fprintf(stderr, "Option -%c requires an operand\n",
94 optopt);
95 errflg++;
96 break;
97
98 case '?':
99 fprintf(stderr, "Unrecognized option: -%c\n", optopt);
100 errflg++;
101 break;
102
103 default:
104 fprintf(stderr,
105 "Unknown error while parsing command line options");
106 errflg++;
107 break;
108 }
109 }
110
111 if (optind > argc) {
112 fprintf(stderr, "Too many input parameters\n");
113 errflg++;
114 }
115
116 if (errflg) {
117 print_usage();
118 return 1;
119 }
120
121 LIST_INITIALIZE(mtab_list);
122 vfs_get_mtab_list(&mtab_list);
123
124 print_header();
125 list_foreach(mtab_list, link, mtab_ent_t, mtab_ent) {
126 if (statfs(mtab_ent->mp, &st) == 0) {
127 print_statfs(&st, mtab_ent->fs_name, mtab_ent->mp);
128 } else {
129 fprintf(stderr, "Cannot get information for '%s' (%d).\n",
130 mtab_ent->mp, errno);
131 }
132 }
133
134 putchar('\n');
135 return 0;
136}
137
138static void size_to_human_readable(char *buf, uint64_t bytes)
139{
140 const char *units = "BkMGTPEZY";
141 int i = 0;
142
143 while (bytes >= 1024) {
144 bytes /= 1024;
145 i++;
146 }
147
148 snprintf(buf, 6, "%4llu%c", (unsigned long long) bytes, units[i]);
149}
150
151static void print_header(void)
152{
153 if (human_readable)
154 printf(HEADER_TABLE_HR);
155 else
156 printf(HEADER_TABLE, unit_size);
157
158 putchar('\n');
159}
160
161static void print_statfs(struct statfs *st, char *name, char *mountpoint)
162{
163 uint64_t const used_blocks = st->f_blocks - st->f_bfree;
164 unsigned const perc_used = PERCENTAGE(used_blocks, st->f_blocks);
165
166 printf("%10s", name);
167
168 if (human_readable) {
169 char tmp[1024];
170
171 /* Print size */
172 size_to_human_readable(tmp, st->f_blocks * st->f_bsize);
173 printf(" %14s", tmp);
174
175 /* Number of used blocks */
176 size_to_human_readable(tmp, used_blocks * st->f_bsize);
177 printf(" %14s", tmp);
178
179 /* Number of available blocks */
180 size_to_human_readable(tmp, st->f_bfree * st->f_bsize);
181 printf(" %14s", tmp);
182
183 /* Percentage of used blocks */
184 printf(" %4u%%", perc_used);
185
186 /* Mount point */
187 printf(" %s\n", mountpoint);
188 } else {
189 /* Blocks / Used blocks / Available blocks / Used% / Mounted on */
190 printf(" %15llu %14llu %14llu %4u%% %s\n",
191 FSBK_TO_BK(st->f_blocks, st->f_bsize, unit_size),
192 FSBK_TO_BK(used_blocks, st->f_bsize, unit_size),
193 FSBK_TO_BK(st->f_bfree, st->f_bsize, unit_size),
194 perc_used,
195 mountpoint);
196 }
197
198}
199
200static void print_usage(void)
201{
202 printf("syntax: %s [-u] [-h] [-b <size>] \n", NAME);
203 printf(" u : Show usage.\n");
204 printf(" h : \"Human-readable\" output.\n");
205 printf(" b : Scale block sizes by selected size.\n");
206 printf("\n");
207}
208
209/** @}
210 */
Note: See TracBrowser for help on using the repository browser.