[66366470] | 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
|
---|
[120d5bc] | 30 | * @brief Print amounts of free and used disk space.
|
---|
[66366470] | 31 | * @{
|
---|
| 32 | */
|
---|
| 33 | /**
|
---|
| 34 | * @file
|
---|
| 35 | */
|
---|
| 36 |
|
---|
[120d5bc] | 37 | #include <cap.h>
|
---|
| 38 | #include <stdbool.h>
|
---|
[66366470] | 39 | #include <stdio.h>
|
---|
| 40 | #include <stdlib.h>
|
---|
[e8f0158] | 41 | #include <stdint.h>
|
---|
[9af1c61] | 42 | #include <str_error.h>
|
---|
[be99d30] | 43 | #include <getopt.h>
|
---|
[66366470] | 44 | #include <errno.h>
|
---|
| 45 | #include <adt/list.h>
|
---|
| 46 | #include <vfs/vfs.h>
|
---|
[35b7d86e] | 47 | #include <vfs/vfs_mtab.h>
|
---|
[66366470] | 48 |
|
---|
| 49 | #define NAME "df"
|
---|
| 50 |
|
---|
[120d5bc] | 51 | #define HEADER_TABLE "Filesystem Size Used Available Used%% Mounted on"
|
---|
| 52 | #define HEADER_TABLE_BLK "Filesystem Blk. Size Total Used Available Used%% Mounted on"
|
---|
[66366470] | 53 |
|
---|
[737227c7] | 54 | #define PERCENTAGE(x, tot) (tot ? (100ULL * (x) / (tot)) : 0)
|
---|
[be99d30] | 55 |
|
---|
[120d5bc] | 56 | static bool display_blocks;
|
---|
[f4cbf9dd] | 57 |
|
---|
[b7fd2a0] | 58 | static errno_t size_to_human_readable(uint64_t, size_t, char **);
|
---|
[3432ddb] | 59 | static void print_header(void);
|
---|
[39330200] | 60 | static errno_t print_statfs(vfs_statfs_t *, char *, char *);
|
---|
[be99d30] | 61 | static void print_usage(void);
|
---|
[66366470] | 62 |
|
---|
| 63 | int main(int argc, char *argv[])
|
---|
| 64 | {
|
---|
[be99d30] | 65 | int optres, errflg = 0;
|
---|
[39330200] | 66 | vfs_statfs_t st;
|
---|
[b7fd2a0] | 67 | errno_t rc;
|
---|
[332513a] | 68 |
|
---|
[120d5bc] | 69 | display_blocks = false;
|
---|
[f4cbf9dd] | 70 |
|
---|
[120d5bc] | 71 | /* Parse command-line options */
|
---|
| 72 | while ((optres = getopt(argc, argv, ":ubh")) != -1) {
|
---|
[be99d30] | 73 | switch(optres) {
|
---|
[120d5bc] | 74 | case 'h':
|
---|
[523a4b6] | 75 | print_usage();
|
---|
| 76 | return 0;
|
---|
| 77 |
|
---|
[f4cbf9dd] | 78 | case 'b':
|
---|
[120d5bc] | 79 | display_blocks = true;
|
---|
[f4cbf9dd] | 80 | break;
|
---|
[a35b458] | 81 |
|
---|
[332513a] | 82 | case ':':
|
---|
[737227c7] | 83 | fprintf(stderr, "Option -%c requires an operand\n",
|
---|
| 84 | optopt);
|
---|
[be99d30] | 85 | errflg++;
|
---|
| 86 | break;
|
---|
| 87 |
|
---|
| 88 | case '?':
|
---|
| 89 | fprintf(stderr, "Unrecognized option: -%c\n", optopt);
|
---|
| 90 | errflg++;
|
---|
| 91 | break;
|
---|
| 92 |
|
---|
| 93 | default:
|
---|
[737227c7] | 94 | fprintf(stderr,
|
---|
| 95 | "Unknown error while parsing command line options");
|
---|
[be99d30] | 96 | errflg++;
|
---|
| 97 | break;
|
---|
| 98 | }
|
---|
| 99 | }
|
---|
[66366470] | 100 |
|
---|
[be99d30] | 101 | if (optind > argc) {
|
---|
[737227c7] | 102 | fprintf(stderr, "Too many input parameters\n");
|
---|
[be99d30] | 103 | errflg++;
|
---|
| 104 | }
|
---|
[332513a] | 105 |
|
---|
[be99d30] | 106 | if (errflg) {
|
---|
| 107 | print_usage();
|
---|
| 108 | return 1;
|
---|
| 109 | }
|
---|
[332513a] | 110 |
|
---|
[66366470] | 111 | LIST_INITIALIZE(mtab_list);
|
---|
[6afc9d7] | 112 | vfs_get_mtab_list(&mtab_list);
|
---|
[737227c7] | 113 |
|
---|
[3432ddb] | 114 | print_header();
|
---|
[e1ec5a2] | 115 | list_foreach(mtab_list, link, mtab_ent_t, mtab_ent) {
|
---|
[b5b5d84] | 116 | if (vfs_statfs_path(mtab_ent->mp, &st) == 0) {
|
---|
[120d5bc] | 117 | rc = print_statfs(&st, mtab_ent->fs_name, mtab_ent->mp);
|
---|
| 118 | if (rc != EOK)
|
---|
| 119 | return 1;
|
---|
[6afc9d7] | 120 | } else {
|
---|
[9af1c61] | 121 | fprintf(stderr, "Cannot get information for '%s' (%s).\n",
|
---|
| 122 | mtab_ent->mp, str_error(errno));
|
---|
[6afc9d7] | 123 | }
|
---|
[66366470] | 124 | }
|
---|
[737227c7] | 125 |
|
---|
[332513a] | 126 | putchar('\n');
|
---|
[66366470] | 127 | return 0;
|
---|
| 128 | }
|
---|
| 129 |
|
---|
[b7fd2a0] | 130 | static errno_t size_to_human_readable(uint64_t nblocks, size_t block_size, char **rptr)
|
---|
[3432ddb] | 131 | {
|
---|
[120d5bc] | 132 | cap_spec_t cap;
|
---|
[3432ddb] | 133 |
|
---|
[120d5bc] | 134 | cap_from_blocks(nblocks, block_size, &cap);
|
---|
| 135 | cap_simplify(&cap);
|
---|
| 136 | return cap_format(&cap, rptr);
|
---|
[3432ddb] | 137 | }
|
---|
| 138 |
|
---|
| 139 | static void print_header(void)
|
---|
| 140 | {
|
---|
[120d5bc] | 141 | if (!display_blocks)
|
---|
| 142 | printf(HEADER_TABLE);
|
---|
[332513a] | 143 | else
|
---|
[120d5bc] | 144 | printf(HEADER_TABLE_BLK);
|
---|
[737227c7] | 145 |
|
---|
[3432ddb] | 146 | putchar('\n');
|
---|
| 147 | }
|
---|
| 148 |
|
---|
[39330200] | 149 | static errno_t print_statfs(vfs_statfs_t *st, char *name, char *mountpoint)
|
---|
[be99d30] | 150 | {
|
---|
[737227c7] | 151 | uint64_t const used_blocks = st->f_blocks - st->f_bfree;
|
---|
| 152 | unsigned const perc_used = PERCENTAGE(used_blocks, st->f_blocks);
|
---|
[120d5bc] | 153 | char *str;
|
---|
[b7fd2a0] | 154 | errno_t rc;
|
---|
[737227c7] | 155 |
|
---|
[be99d30] | 156 | printf("%10s", name);
|
---|
[332513a] | 157 |
|
---|
[120d5bc] | 158 | if (!display_blocks) {
|
---|
[737227c7] | 159 | /* Print size */
|
---|
[120d5bc] | 160 | rc = size_to_human_readable(st->f_blocks, st->f_bsize, &str);
|
---|
| 161 | if (rc != EOK)
|
---|
| 162 | goto error;
|
---|
| 163 | printf(" %14s", str);
|
---|
| 164 | free(str);
|
---|
[737227c7] | 165 |
|
---|
| 166 | /* Number of used blocks */
|
---|
[120d5bc] | 167 | rc = size_to_human_readable(used_blocks, st->f_bsize, &str);
|
---|
| 168 | if (rc != EOK)
|
---|
| 169 | goto error;
|
---|
| 170 | printf(" %14s", str);
|
---|
| 171 | free(str);
|
---|
[737227c7] | 172 |
|
---|
| 173 | /* Number of available blocks */
|
---|
[120d5bc] | 174 | rc = size_to_human_readable(st->f_bfree, st->f_bsize, &str);
|
---|
| 175 | if (rc != EOK)
|
---|
| 176 | goto error;
|
---|
| 177 | printf(" %14s", str);
|
---|
| 178 | free(str);
|
---|
[737227c7] | 179 |
|
---|
| 180 | /* Percentage of used blocks */
|
---|
[651c8db] | 181 | printf(" %4u%%", perc_used);
|
---|
[737227c7] | 182 |
|
---|
| 183 | /* Mount point */
|
---|
| 184 | printf(" %s\n", mountpoint);
|
---|
| 185 | } else {
|
---|
[120d5bc] | 186 | /* Block size / Blocks / Used blocks / Available blocks / Used% / Mounted on */
|
---|
[1b20da0] | 187 | printf(" %10" PRIu32 " %9" PRIu64 " %11" PRIu64 " %11" PRIu64 " %4u%% %s\n",
|
---|
[120d5bc] | 188 | st->f_bsize, st->f_blocks, used_blocks, st->f_bfree,
|
---|
| 189 | perc_used, mountpoint);
|
---|
[3432ddb] | 190 | }
|
---|
[332513a] | 191 |
|
---|
[120d5bc] | 192 | return EOK;
|
---|
| 193 | error:
|
---|
| 194 | printf("\nError: Out of memory.\n");
|
---|
| 195 | return ENOMEM;
|
---|
[be99d30] | 196 | }
|
---|
| 197 |
|
---|
| 198 | static void print_usage(void)
|
---|
| 199 | {
|
---|
[120d5bc] | 200 | printf("Syntax: %s [<options>] \n", NAME);
|
---|
| 201 | printf("Options:\n");
|
---|
| 202 | printf(" -h Print help\n");
|
---|
| 203 | printf(" -b Print exact block sizes and numbers\n");
|
---|
[be99d30] | 204 | }
|
---|
| 205 |
|
---|
[66366470] | 206 | /** @}
|
---|
| 207 | */
|
---|