Debian Codegraph @

extra/haxelib_src/src/haxelib/client/FsUtils.hx

raw
1 /*
2 * Copyright (C)2005-2017 Haxe Foundation
3 *
4 * Permission is hereby granted, free of charge, to any person obtaining a
5 * copy of this software and associated documentation files (the "Software"),
6 * to deal in the Software without restriction, including without limitation
7 * the rights to use, copy, modify, merge, publish, distribute, sublicense,
8 * and/or sell copies of the Software, and to permit persons to whom the
9 * Software is furnished to do so, subject to the following conditions:
10 *
11 * The above copyright notice and this permission notice shall be included in
12 * all copies or substantial portions of the Software.
13 *
14 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
15 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
16 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
17 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
18 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
19 * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
20 * DEALINGS IN THE SOFTWARE.
21 */
22 package haxelib.client;
23
24 import haxe.io.Path;
25 import sys.FileSystem;
26 using StringTools;
27
28 class FsUtils {
29 static var IS_WINDOWS = (Sys.systemName() == "Windows");
30
31 /**
32 recursively follow symlink
33 TODO: this method does not (yet) work on Windows
34 */
35 public static function realPath(path:String):String {
36 var proc = new sys.io.Process('readlink', [path.endsWith("\n") ? path.substr(0, path.length-1) : path]);
37 var ret = switch (proc.stdout.readAll().toString()) {
38 case "": //it is not a symlink
39 path;
40 case targetPath:
41 if (targetPath.startsWith("/")) {
42 realPath(targetPath);
43 } else {
44 realPath(new Path(path).dir + "/" + targetPath);
45 }
46 }
47 proc.close();
48 return ret;
49 }
50
51 public static function isSamePath(a:String, b:String):Bool {
52 a = Path.normalize(a);
53 b = Path.normalize(b);
54 if (IS_WINDOWS) { // paths are case-insensitive on Windows
55 a = a.toLowerCase();
56 b = b.toLowerCase();
57 }
58 return a == b;
59 }
60
61 public static function safeDir(dir:String, checkWritable = false):Bool {
62 if (FileSystem.exists(dir)) {
63 if (!FileSystem.isDirectory(dir)) {
64 try {
65 // if this call is successful then 'dir' it is not a file but a symlink to a directory
66 FileSystem.readDirectory(dir);
67 } catch (ex:Dynamic) {
68 throw 'A file is preventing the required directory $dir to be created';
69 }
70 }
71 if (checkWritable) {
72 var checkFile = dir+"/haxelib_writecheck.txt";
73 try {
74 sys.io.File.saveContent(checkFile, "This is a temporary file created by Haxelib to check if directory is writable. You can safely delete it!");
75 } catch (_:Dynamic) {
76 throw '$dir exists but is not writeable, chmod it';
77 }
78 FileSystem.deleteFile(checkFile);
79 }
80 return false;
81 } else {
82 try {
83 FileSystem.createDirectory(dir);
84 return true;
85 } catch (_:Dynamic) {
86 throw 'You don\'t have enough user rights to create the directory $dir';
87 }
88 }
89 }
90
91 public static function deleteRec(dir:String):Bool {
92 if (!FileSystem.exists(dir))
93 return false;
94 for (p in FileSystem.readDirectory(dir)) {
95 var path = Path.join([dir, p]);
96
97 if (isBrokenSymlink(path)) {
98 safeDelete(path);
99 } else if (FileSystem.isDirectory(path)) {
100 if (!IS_WINDOWS) {
101 // try to delete it as a file first - in case of path
102 // being a symlink, it will success
103 if (!safeDelete(path))
104 deleteRec(path);
105 } else {
106 deleteRec(path);
107 }
108 } else {
109 safeDelete(path);
110 }
111 }
112 FileSystem.deleteDirectory(dir);
113 return true;
114 }
115
116 static function safeDelete(file:String):Bool {
117 try {
118 FileSystem.deleteFile(file);
119 return true;
120 } catch (e:Dynamic) {
121 if (IS_WINDOWS) {
122 try {
123 Sys.command("attrib", ["-R", file]);
124 FileSystem.deleteFile(file);
125 return true;
126 } catch (_:Dynamic) {
127 }
128 }
129 return false;
130 }
131 }
132
133 static function isBrokenSymlink(path:String):Bool {
134 // TODO: figure out what this method actually does :)
135 var errors = 0;
136 try FileSystem.isDirectory(path) catch (error:String) if (error == "std@sys_file_type") errors++;
137 try FileSystem.fullPath(path) catch (error:String) if (error == "std@file_full_path") errors++;
138 return errors == 2;
139 }
140 }