| 1 |
/******************************************************************************* |
| 2 |
* |
| 3 |
* Copyright(c) 2008 Jonathon Jongsma |
| 4 |
* |
| 5 |
* This file is part of gtkmm |
| 6 |
* |
| 7 |
* This program is free software; you can redistribute it and/or modify |
| 8 |
* it under the terms of the GNU General Public License as published by |
| 9 |
* the Free Software Foundation; either version 2 of the License, or |
| 10 |
* (at your option) any later version. |
| 11 |
* |
| 12 |
* This program is distributed in the hope that it will be useful, |
| 13 |
* but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 14 |
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 15 |
* GNU General Public License for more details. |
| 16 |
* |
| 17 |
* You should have received a copy of the GNU General Public License |
| 18 |
* along with this program; if not, see <http://www.gnu.org/licenses/> |
| 19 |
* |
| 20 |
*******************************************************************************/ |
| 21 |
#include <giomm.h> |
| 22 |
#include <iostream> |
| 23 |
|
| 24 |
static goffset get_size_recursively(const Glib::RefPtr<Gio::File>& file) |
| 25 |
{ |
| 26 |
goffset total = 0; |
| 27 |
g_return_val_if_fail(file, total); |
| 28 |
|
| 29 |
try |
| 30 |
{ |
| 31 |
auto info = file->query_info(); |
| 32 |
if(info->get_file_type() == Gio::FileType::DIRECTORY) |
| 33 |
{ |
| 34 |
auto children = file->enumerate_children(); |
| 35 |
Glib::RefPtr<Gio::FileInfo> child_info; |
| 36 |
|
| 37 |
while((child_info = children->next_file())) |
| 38 |
{ |
| 39 |
goffset size = get_size_recursively( |
| 40 |
file->get_child(child_info->get_name())); |
| 41 |
total += size; |
| 42 |
} |
| 43 |
|
| 44 |
//std::cout << file->get_path() << ": " << total << std::endl; |
| 45 |
} |
| 46 |
else |
| 47 |
{ |
| 48 |
total = info->get_size(); |
| 49 |
} |
| 50 |
} |
| 51 |
catch(const Glib::Error& error) |
| 52 |
{ |
| 53 |
std::cerr << error.what() << std::endl; |
| 54 |
} |
| 55 |
return total; |
| 56 |
} |
| 57 |
|
| 58 |
int main(int argc, char** argv) |
| 59 |
{ |
| 60 |
std::string root_dir = "."; |
| 61 |
if(argc > 1) |
| 62 |
root_dir = argv[1]; |
| 63 |
|
| 64 |
Gio::init(); |
| 65 |
|
| 66 |
auto file = Gio::File::create_for_path(root_dir); |
| 67 |
std::cout << "Gathering disk usage information for '" << file->get_path() << "'" << std::endl; |
| 68 |
|
| 69 |
const goffset total = get_size_recursively(file); |
| 70 |
std::cout << "Total: " << total << std::endl; |
| 71 |
|
| 72 |
return 0; |
| 73 |
} |