Repository navigation
Expand file tree
/
Copy pathdf.c
More file actions
122 lines (104 loc) · 3.55 KB
/
Copy pathdf.c
File metadata and controls
122 lines (104 loc) · 3.55 KB
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
/*
* megatools - Mega.nz client library and tools
* Copyright (C) 2013 Ondřej Jirman <megous@megous.com>
*
* 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, write to the Free Software Foundation, Inc.,
* 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
*/
#include "tools.h"
static gboolean opt_human;
static gboolean opt_mb;
static gboolean opt_gb;
static gboolean opt_total;
static gboolean opt_free;
static gboolean opt_used;
static GOptionEntry entries[] =
{
{ "human", 'h', 0, G_OPTION_ARG_NONE, &opt_human, "Use human readable formatting", NULL },
{ "mb", '\0', 0, G_OPTION_ARG_NONE, &opt_mb, "Show numbers in MiB", NULL },
{ "gb", '\0', 0, G_OPTION_ARG_NONE, &opt_gb, "Show numbers in GiB", NULL },
{ "total", '\0', 0, G_OPTION_ARG_NONE, &opt_total, "Show only total available space", NULL },
{ "used", '\0', 0, G_OPTION_ARG_NONE, &opt_used, "Show only used space", NULL },
{ "free", '\0', 0, G_OPTION_ARG_NONE, &opt_free, "Show only available free space", NULL },
{ NULL }
};
static gchar* format_size(guint64 size)
{
if (opt_human)
return g_format_size_full(size, G_FORMAT_SIZE_IEC_UNITS);
else if (opt_mb)
size /= 1024 * 1024;
else if (opt_gb)
size /= 1024 * 1024 * 1024;
return g_strdup_printf("%" G_GUINT64_FORMAT, size);
}
int main(int ac, char* av[])
{
GError *local_err = NULL;
mega_session* s;
tool_init(&ac, &av, "- display mega.nz storage quotas/usage", entries);
if (opt_total || opt_free || opt_used)
{
gint opts_used = 0;
opts_used += opt_total ? 1 : 0;
opts_used += opt_free ? 1 : 0;
opts_used += opt_used ? 1 : 0;
if (opt_used > 1)
{
g_printerr("ERROR: Options conflict, you should use either --total, --used, or --free.\n");
return 1;
}
}
if (opt_human || opt_mb || opt_gb)
{
gint opts_used = 0;
opts_used += opt_human ? 1 : 0;
opts_used += opt_mb ? 1 : 0;
opts_used += opt_gb ? 1 : 0;
if (opt_used > 1)
{
g_printerr("ERROR: Options conflict, you should use either --human, --mb, or --gb.\n");
return 1;
}
}
s = tool_start_session();
if (!s)
return 1;
mega_user_quota* q = mega_session_user_quota(s, &local_err);
if (!q)
{
g_printerr("ERROR: Can't determine disk usage: %s\n", local_err->message);
g_clear_error(&local_err);
goto err;
}
guint64 free = q->total >= q->used ? q->total - q->used : 0;
if (opt_total)
g_print("%s\n", format_size(q->total));
else if (opt_used)
g_print("%s\n", format_size(q->used));
else if (opt_free)
g_print("%s\n", format_size(free));
else
{
g_print("Total: %s\n", format_size(q->total));
g_print("Used: %s\n", format_size(q->used));
g_print("Free: %s\n", format_size(free));
}
g_free(q);
tool_fini(s);
return 0;
err:
tool_fini(s);
return 1;
}