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
|
/* filehelper.vala
*
* Copyright (C) 2008-2009 Florian Brosch
*
* This library is free software; you can redistribute it and/or
* modify it under the terms of the GNU Lesser General Public
* License as published by the Free Software Foundation; either
* version 2.1 of the License, or (at your option) any later version.
*
* This library 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
* Lesser General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public
* License along with this library; if not, write to the Free Software
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
*
* Author:
* Brosch Florian <flo.brosch@gmail.com>
*/
namespace Valadoc {
/**
* Makes a copy of the file src to dest.
*
* @param src the file to copy
* @param dest the destination path
*/
public bool copy_file (string src, string dest) {
GLib.FileStream fsrc = GLib.FileStream.open (src, "rb");
if (fsrc == null) {
return false;
}
GLib.FileStream fdest = GLib.FileStream.open (dest, "wb");
if (fdest == null) {
return false;
}
for (int c = fsrc.getc() ; !fsrc.eof() ; c = fsrc.getc()) {
fdest.putc ((char)c);
}
return true;
}
/**
* Makes a copy of the directory src to dest.
*
* @param src the directory to copy
* @param dest the destination path
*/
public bool copy_directory (string src, string dest) {
try {
GLib.Dir dir = GLib.Dir.open (src);
for (string? file = dir.read_name (); file != null; file = dir.read_name ()) {
string src_file_path = GLib.Path.build_filename (src, file);
string dest_file_path = GLib.Path.build_filename (dest, file);
if (GLib.FileUtils.test (src_file_path, GLib.FileTest.IS_DIR)) {
GLib.DirUtils.create (dest_file_path, 0755); // mkdir if necessary
if (!copy_directory (src_file_path, dest_file_path)) { // copy directories recursively
return false;
}
} else {
if (!copy_file (src_file_path, dest_file_path)) {
return false;
}
}
}
}
catch (GLib.FileError err) {
return false;
}
return true;
}
/**
* A recursive directory delete function
*
* @param rpath the directory to remove
*/
public bool remove_directory (string rpath) {
try {
GLib.Dir dir = GLib.Dir.open ( rpath );
if (dir == null)
return false;
for (weak string entry = dir.read_name(); entry != null ; entry = dir.read_name()) {
string path = rpath + entry;
bool is_dir = GLib.FileUtils.test (path, GLib.FileTest.IS_DIR);
if (is_dir == true) {
bool tmp = remove_directory (path);
if (tmp == false) {
return false;
}
} else {
int tmp = GLib.FileUtils.unlink (path);
if (tmp > 0) {
return false;
}
}
}
} catch (GLib.FileError err) {
return false;
}
return true;
}
}
|