34 public const int64
KB = 1000;
35 public const int64
MB = 1000 *
KB;
36 public const int64
GB = 1000 *
MB;
37 public const int64
TB = 1000 *
GB;
38 public const int64
KiB = 1024;
46 return File.new_for_path(file_path).get_parent().get_path();
50 return File.new_for_path(file_path).get_basename();
54 return GLib.Path.build_path(
"/", path1, path2);
58 if (path.has_suffix(
"/")){
59 return path[0:path.length - 1];
70 return (FileUtils.test(file_path, GLib.FileTest.EXISTS)
71 && !FileUtils.test(file_path, GLib.FileTest.IS_DIR));
79 var file = File.new_for_path (file_path);
80 if (file.query_exists ()) {
86 log_error(
_(
"Failed to delete file") +
": %s".printf(file_path));
98 File file = File.new_for_path(file_path);
99 FileInputStream inStream = file.read();
100 BufferedInputStream bis =
new BufferedInputStream.sized(inStream, (
size_t) (1 *
MiB));
101 while((symbol = (
char) bis.read_byte()) != -1) {
110 log_error(
_(
"Failed to read file") +
": %s".printf(file_path));
123 GLib.FileUtils.get_contents (file_path, out txt, out size);
128 log_error(
_(
"Failed to read file") +
": %s".printf(file_path));
139 GLib.FileUtils.get_data(file_path, out content);
142 string[] parsed = {
""};
144 foreach (uint8
byte in content) {
146 if(
byte == delimiter) {
152 parsed[element] += ((char) byte).to_string();
159 log_error(
_(
"Failed to read file") +
": %s".printf(file_path));
173 var file = File.new_for_path (file_path);
174 if (file.query_exists ()) {
178 var file_stream = file.create (FileCreateFlags.NONE);
179 var data_stream =
new DataOutputStream (file_stream);
180 data_stream.put_string (contents);
186 log_error(
_(
"Failed to write file") +
": %s".printf(file_path));
191 public bool file_copy (
string src_file,
string dest_file){
193 var file_src = File.new_for_path (src_file);
194 if (file_src.query_exists()) {
195 var file_dest = File.new_for_path (dest_file);
196 file_src.copy(file_dest,FileCopyFlags.OVERWRITE,
null,
null);
202 log_error(
_(
"Failed to copy file") +
": '%s', '%s'".printf(src_file, dest_file));
208 public void file_move (
string src_file,
string dest_file){
210 var file_src = File.new_for_path (src_file);
211 if (file_src.query_exists()) {
212 var file_dest = File.new_for_path (dest_file);
213 file_src.move(file_dest,FileCopyFlags.OVERWRITE,
null,
null);
216 log_error(
_(
"File not found") +
": '%s'".printf(src_file));
221 log_error(
_(
"Failed to move file") +
": '%s', '%s'".printf(src_file, dest_file));
228 var file = File.new_for_path (file_path);
230 if (file.query_exists()) {
232 var info = file.query_info(
"%s".printf(FileAttribute.STANDARD_TYPE), FileQueryInfoFlags.NOFOLLOW_SYMLINKS);
234 var file_type = info.get_file_type();
236 return (file_type == FileType.SYMBOLIC_LINK);
248 string dst_file = src_file +
".gz";
252 string std_out, std_err;
253 exec_sync(cmd, out std_out, out std_err);
260 if (file_path.has_prefix(
"/")){
263 else if (!file_path.contains(
"/")){
264 return GLib.Environment.find_program_in_path(file_path);
266 else if (file_path.has_prefix(
"./")){
267 return path_combine(GLib.Environment.get_current_dir(), file_path[2:file_path.length]);
269 else if (file_path.has_prefix(
"../")){
273 return path_combine(GLib.Environment.get_current_dir(), file_path);
282 File file = File.parse_name (file_path);
283 if (file.query_exists()) {
284 info = file.query_info(
"%s".printf(FileAttribute.STANDARD_SYMLINK_TARGET), 0);
285 return info.get_symlink_target();
299 return ( FileUtils.test(dir_path, GLib.FileTest.EXISTS) && FileUtils.test(dir_path, GLib.FileTest.IS_DIR));
302 public bool dir_create (
string dir_path,
bool show_message =
false){
307 var dir = File.parse_name (dir_path);
309 if (dir.query_exists () ==
false) {
311 bool ok = dir.make_directory_with_parents (
null);
315 log_msg(
_(
"Created directory") +
": %s".printf(dir_path));
318 log_error(
_(
"Failed to create directory") +
": %s".printf(dir_path));
327 log_error(
_(
"Failed to create directory") +
": %s".printf(dir_path));
334 File dirf = File.new_for_path(path);
336 if (dirf.query_file_type(FileQueryInfoFlags.NONE) == FileType.DIRECTORY) {
337 return dirf.delete();
340 if(ioe.matches(IOError.quark(), IOError.NOT_FOUND)) {
349 File f = File.new_for_path(dir);
350 if(f.query_exists()) {
352 FileEnumerator enumerator = f.enumerate_children(FileAttribute.STANDARD_NAME, FileQueryInfoFlags.NOFOLLOW_SYMLINKS);
354 while ((info = enumerator.next_file()) !=
null) {
355 string name = info.get_name();
356 if(info.get_file_type() == FileType.DIRECTORY) {
359 if(name ==
"." || name ==
"..") {
367 File file = File.new_for_path(dir +
"/" + name);
373 log_error(
"Can not enumerate folder %s".printf(dir));
380 public static bool dir_delete(
string dir_path,
bool show_message =
false) {
388 log_msg(
_(
"Deleted directory") +
": %s".printf(dir_path));
391 log_error(
_(
"Failed to delete directory") +
": %s".printf(dir_path));
403 bool is_empty =
true;
404 var dir = File.parse_name (dir_path);
405 if (dir.query_exists()) {
407 var enu = dir.enumerate_children (FileAttribute.STANDARD_NAME, 0);
408 while ((info = enu.next_file()) !=
null) {
423 var list =
new Gee.ArrayList<
string>();
427 File f_home = File.new_for_path (path);
428 FileEnumerator enumerator = f_home.enumerate_children (
"%s".printf(FileAttribute.STANDARD_NAME), 0);
430 while ((file = enumerator.next_file ()) !=
null) {
431 string name = file.get_name();
440 CompareDataFunc<string> entry_compare = (a, b) => {
443 list.sort((owned) entry_compare);
451 uint64 size,
bool binary_units =
false,
452 string unit =
"",
bool show_units =
true,
int decimals = 1){
454 uint64 unit_k = binary_units ? 1024 : 1000;
455 uint64 unit_m = binary_units ? 1024 * unit_k : 1000 * unit_k;
456 uint64 unit_g = binary_units ? 1024 * unit_m : 1000 * unit_m;
457 uint64 unit_t = binary_units ? 1024 * unit_g : 1000 * unit_g;
463 if ((size > unit_t) && ((unit.length == 0) || (unit ==
"t"))){
464 txt += (
"%%'0.%df".printf(decimals)).printf(size / (1.0 * unit_t));
466 txt +=
" %sB".printf(binary_units ?
"Ti" :
"T");
469 else if ((size > unit_g) && ((unit.length == 0) || (unit ==
"g"))){
470 txt += (
"%%'0.%df".printf(decimals)).printf(size / (1.0 * unit_g));
472 txt +=
" %sB".printf(binary_units ?
"Gi" :
"G");
475 else if ((size > unit_m) && ((unit.length == 0) || (unit ==
"m"))){
476 txt += (
"%%'0.%df".printf(decimals)).printf(size / (1.0 * unit_m));
478 txt +=
" %sB".printf(binary_units ?
"Mi" :
"M");
481 else if ((size > unit_k) && ((unit.length == 0) || (unit ==
"k"))){
482 txt += (
"%%'0.%df".printf(decimals)).printf(size / (1.0 * unit_k));
484 txt +=
" %sB".printf(binary_units ?
"Ki" :
"K");
488 txt +=
"%'0lu".printf(size);
501 return file_path.replace(
"'",
"'\\''");
Gee.ArrayList< string > dir_list_names(string path)
bool file_exists(string file_path)
string escape_single_quote(string file_path)
string remove_trailing_slash(string path)
string file_get_symlink_target(string file_path)
string? file_read(string file_path)
string file_basename(string file_path)
string file_parent(string file_path)
string?[] file_read_array(string file_path, char delimiter='\n')
bool file_write(string file_path, string contents)
string file_resolve_executable_path(string file_path)
bool file_is_symlink(string file_path)
string path_combine(string path1, string path2)
bool file_copy(string src_file, string dest_file)
string format_file_size(uint64 size, bool binary_units=false, string unit="", bool show_units=true, int decimals=1)
bool file_delete(string file_path)
static bool dir_delete(string dir_path, bool show_message=false)
int64? file_line_count(string file_path)
bool dir_is_empty(string dir_path)
bool dir_create(string dir_path, bool show_message=false)
static bool dir_empty_delete(string path)
bool dir_exists(string dir_path)
void file_move(string src_file, string dest_file)
static bool dir_delete_recursive(string dir)
bool file_gzip(string src_file)
void log_error(string message, bool highlight=false, bool is_warning=false)
void log_msg(string message, bool highlight=false)
int exec_sync(string cmd, out string? std_out=null, out string? std_err=null)