linux: Add constants from program_invocation_name(3)
[vala-gnome.git] / libvaladoc / filehelper.vala
blobc9ff9c137cf6c7fbe4ba4418e83e5fd5440e7fce
1 /* filehelper.vala
3 * Copyright (C) 2008-2009 Florian Brosch
5 * This library is free software; you can redistribute it and/or
6 * modify it under the terms of the GNU Lesser General Public
7 * License as published by the Free Software Foundation; either
8 * version 2.1 of the License, or (at your option) any later version.
10 * This library is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13 * Lesser General Public License for more details.
15 * You should have received a copy of the GNU Lesser General Public
16 * License along with this library; if not, write to the Free Software
17 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
19 * Author:
20 * Brosch Florian <flo.brosch@gmail.com>
24 namespace Valadoc {
25 [CCode (cprefix = "", cname = "PACKAGE_ICONDIR")]
26 public extern const string icons_dir;
28 /**
29 * Makes a copy of the file src to dest.
31 * @param src the file to copy
32 * @param dest the destination path
34 public bool copy_file (string src, string dest) {
35 GLib.FileStream fsrc = GLib.FileStream.open (src, "rb");
36 if (fsrc == null) {
37 return false;
40 GLib.FileStream fdest = GLib.FileStream.open (dest, "wb");
41 if (fdest == null) {
42 return false;
45 for (int c = fsrc.getc() ; !fsrc.eof() ; c = fsrc.getc()) {
46 fdest.putc ((char)c);
49 return true;
52 /**
53 * Makes a copy of the directory src to dest.
55 * @param src the directory to copy
56 * @param dest the destination path
58 public bool copy_directory (string src, string dest) {
59 try {
60 GLib.Dir dir = GLib.Dir.open (src);
61 for (string? file = dir.read_name (); file != null; file = dir.read_name ()) {
62 string src_file_path = GLib.Path.build_filename (src, file);
63 string dest_file_path = GLib.Path.build_filename (dest, file);
64 if (GLib.FileUtils.test (src_file_path, GLib.FileTest.IS_DIR)) {
65 GLib.DirUtils.create (dest_file_path, 0755); // mkdir if necessary
66 if (!copy_directory (src_file_path, dest_file_path)) { // copy directories recursively
67 return false;
69 } else {
70 if (!copy_file (src_file_path, dest_file_path)) {
71 return false;
76 catch (GLib.FileError err) {
77 return false;
79 return true;
82 /**
83 * A recursive directory delete function
85 * @param rpath the directory to remove
87 public bool remove_directory (string rpath) {
88 try {
89 GLib.Dir dir = GLib.Dir.open ( rpath );
90 if (dir == null)
91 return false;
93 for (weak string entry = dir.read_name(); entry != null ; entry = dir.read_name()) {
94 string path = rpath + entry;
96 bool is_dir = GLib.FileUtils.test (path, GLib.FileTest.IS_DIR);
97 if (is_dir == true) {
98 bool tmp = remove_directory (path);
99 if (tmp == false) {
100 return false;
102 } else {
103 int tmp = GLib.FileUtils.unlink (path);
104 if (tmp > 0) {
105 return false;
109 } catch (GLib.FileError err) {
110 return false;
113 return true;