[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 |
|
---|
[c24b0dcb] | 37 | #include <capa.h>
|
---|
[120d5bc] | 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 */
|
---|
[b2dca036] | 72 | while ((optres = getopt(argc, argv, "ubh")) != -1) {
|
---|
[1433ecda] | 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 |
|
---|
[be99d30] | 82 | case '?':
|
---|
| 83 | fprintf(stderr, "Unrecognized option: -%c\n", optopt);
|
---|
| 84 | errflg++;
|
---|
| 85 | break;
|
---|
| 86 |
|
---|
| 87 | default:
|
---|
[737227c7] | 88 | fprintf(stderr,
|
---|
| 89 | "Unknown error while parsing command line options");
|
---|
[be99d30] | 90 | errflg++;
|
---|
| 91 | break;
|
---|
| 92 | }
|
---|
| 93 | }
|
---|
[66366470] | 94 |
|
---|
[be99d30] | 95 | if (optind > argc) {
|
---|
[737227c7] | 96 | fprintf(stderr, "Too many input parameters\n");
|
---|
[be99d30] | 97 | errflg++;
|
---|
| 98 | }
|
---|
[332513a] | 99 |
|
---|
[be99d30] | 100 | if (errflg) {
|
---|
| 101 | print_usage();
|
---|
| 102 | return 1;
|
---|
| 103 | }
|
---|
[332513a] | 104 |
|
---|
[66366470] | 105 | LIST_INITIALIZE(mtab_list);
|
---|
[6afc9d7] | 106 | vfs_get_mtab_list(&mtab_list);
|
---|
[737227c7] | 107 |
|
---|
[3432ddb] | 108 | print_header();
|
---|
[e1ec5a2] | 109 | list_foreach(mtab_list, link, mtab_ent_t, mtab_ent) {
|
---|
[b5b5d84] | 110 | if (vfs_statfs_path(mtab_ent->mp, &st) == 0) {
|
---|
[120d5bc] | 111 | rc = print_statfs(&st, mtab_ent->fs_name, mtab_ent->mp);
|
---|
| 112 | if (rc != EOK)
|
---|
| 113 | return 1;
|
---|
[6afc9d7] | 114 | } else {
|
---|
[9af1c61] | 115 | fprintf(stderr, "Cannot get information for '%s' (%s).\n",
|
---|
| 116 | mtab_ent->mp, str_error(errno));
|
---|
[6afc9d7] | 117 | }
|
---|
[66366470] | 118 | }
|
---|
[737227c7] | 119 |
|
---|
[332513a] | 120 | putchar('\n');
|
---|
[66366470] | 121 | return 0;
|
---|
| 122 | }
|
---|
| 123 |
|
---|
[b7fd2a0] | 124 | static errno_t size_to_human_readable(uint64_t nblocks, size_t block_size, char **rptr)
|
---|
[3432ddb] | 125 | {
|
---|
[c24b0dcb] | 126 | capa_spec_t capa;
|
---|
[3432ddb] | 127 |
|
---|
[c24b0dcb] | 128 | capa_from_blocks(nblocks, block_size, &capa);
|
---|
| 129 | capa_simplify(&capa);
|
---|
| 130 | return capa_format(&capa, rptr);
|
---|
[3432ddb] | 131 | }
|
---|
| 132 |
|
---|
| 133 | static void print_header(void)
|
---|
| 134 | {
|
---|
[120d5bc] | 135 | if (!display_blocks)
|
---|
| 136 | printf(HEADER_TABLE);
|
---|
[332513a] | 137 | else
|
---|
[120d5bc] | 138 | printf(HEADER_TABLE_BLK);
|
---|
[737227c7] | 139 |
|
---|
[3432ddb] | 140 | putchar('\n');
|
---|
| 141 | }
|
---|
| 142 |
|
---|
[39330200] | 143 | static errno_t print_statfs(vfs_statfs_t *st, char *name, char *mountpoint)
|
---|
[be99d30] | 144 | {
|
---|
[737227c7] | 145 | uint64_t const used_blocks = st->f_blocks - st->f_bfree;
|
---|
| 146 | unsigned const perc_used = PERCENTAGE(used_blocks, st->f_blocks);
|
---|
[120d5bc] | 147 | char *str;
|
---|
[b7fd2a0] | 148 | errno_t rc;
|
---|
[737227c7] | 149 |
|
---|
[be99d30] | 150 | printf("%10s", name);
|
---|
[332513a] | 151 |
|
---|
[120d5bc] | 152 | if (!display_blocks) {
|
---|
[737227c7] | 153 | /* Print size */
|
---|
[120d5bc] | 154 | rc = size_to_human_readable(st->f_blocks, st->f_bsize, &str);
|
---|
| 155 | if (rc != EOK)
|
---|
| 156 | goto error;
|
---|
| 157 | printf(" %14s", str);
|
---|
| 158 | free(str);
|
---|
[737227c7] | 159 |
|
---|
| 160 | /* Number of used blocks */
|
---|
[120d5bc] | 161 | rc = size_to_human_readable(used_blocks, st->f_bsize, &str);
|
---|
| 162 | if (rc != EOK)
|
---|
| 163 | goto error;
|
---|
| 164 | printf(" %14s", str);
|
---|
| 165 | free(str);
|
---|
[737227c7] | 166 |
|
---|
| 167 | /* Number of available blocks */
|
---|
[120d5bc] | 168 | rc = size_to_human_readable(st->f_bfree, st->f_bsize, &str);
|
---|
| 169 | if (rc != EOK)
|
---|
| 170 | goto error;
|
---|
| 171 | printf(" %14s", str);
|
---|
| 172 | free(str);
|
---|
[737227c7] | 173 |
|
---|
| 174 | /* Percentage of used blocks */
|
---|
[651c8db] | 175 | printf(" %4u%%", perc_used);
|
---|
[737227c7] | 176 |
|
---|
| 177 | /* Mount point */
|
---|
| 178 | printf(" %s\n", mountpoint);
|
---|
| 179 | } else {
|
---|
[120d5bc] | 180 | /* Block size / Blocks / Used blocks / Available blocks / Used% / Mounted on */
|
---|
[1b20da0] | 181 | printf(" %10" PRIu32 " %9" PRIu64 " %11" PRIu64 " %11" PRIu64 " %4u%% %s\n",
|
---|
[120d5bc] | 182 | st->f_bsize, st->f_blocks, used_blocks, st->f_bfree,
|
---|
| 183 | perc_used, mountpoint);
|
---|
[3432ddb] | 184 | }
|
---|
[332513a] | 185 |
|
---|
[120d5bc] | 186 | return EOK;
|
---|
| 187 | error:
|
---|
| 188 | printf("\nError: Out of memory.\n");
|
---|
| 189 | return ENOMEM;
|
---|
[be99d30] | 190 | }
|
---|
| 191 |
|
---|
| 192 | static void print_usage(void)
|
---|
| 193 | {
|
---|
[120d5bc] | 194 | printf("Syntax: %s [<options>] \n", NAME);
|
---|
| 195 | printf("Options:\n");
|
---|
| 196 | printf(" -h Print help\n");
|
---|
| 197 | printf(" -b Print exact block sizes and numbers\n");
|
---|
[be99d30] | 198 | }
|
---|
| 199 |
|
---|
[66366470] | 200 | /** @}
|
---|
| 201 | */
|
---|