|
1 const CWD = do_get_cwd(); |
|
2 function checkOS(os) { |
|
3 const nsILocalFile_ = "nsILocalFile" + os; |
|
4 return nsILocalFile_ in Components.interfaces && |
|
5 CWD instanceof Components.interfaces[nsILocalFile_]; |
|
6 } |
|
7 |
|
8 const DIR_TARGET = "target"; |
|
9 const DIR_LINK = "link"; |
|
10 const DIR_LINK_LINK = "link_link"; |
|
11 const FILE_TARGET = "target.txt"; |
|
12 const FILE_LINK = "link.txt"; |
|
13 const FILE_LINK_LINK = "link_link.txt"; |
|
14 |
|
15 const DOES_NOT_EXIST = "doesnotexist"; |
|
16 const DANGLING_LINK = "dangling_link"; |
|
17 const LOOP_LINK = "loop_link"; |
|
18 |
|
19 const isWin = checkOS("Win"); |
|
20 const isMac = checkOS("Mac"); |
|
21 const isUnix = !(isWin || isMac); |
|
22 |
|
23 const nsIFile = Components.interfaces.nsIFile; |
|
24 |
|
25 var process; |
|
26 function createSymLink(from, to) { |
|
27 if (!process) { |
|
28 var ln = Components.classes["@mozilla.org/file/local;1"] |
|
29 .createInstance(Components.interfaces.nsILocalFile); |
|
30 ln.initWithPath("/bin/ln"); |
|
31 |
|
32 process = Components.classes["@mozilla.org/process/util;1"] |
|
33 .createInstance(Components.interfaces.nsIProcess); |
|
34 process.init(ln); |
|
35 } |
|
36 |
|
37 const args = ["-s", from, to]; |
|
38 process.run(true, args, args.length); |
|
39 do_check_eq(process.exitValue, 0); |
|
40 } |
|
41 |
|
42 function makeSymLink(from, toName, relative) { |
|
43 var to = from.parent; |
|
44 to.append(toName); |
|
45 |
|
46 if (relative) { |
|
47 createSymLink(from.leafName, to.path); |
|
48 } |
|
49 else { |
|
50 createSymLink(from.path, to.path); |
|
51 } |
|
52 |
|
53 do_check_true(to.isSymlink()); |
|
54 |
|
55 print("---"); |
|
56 print(from.path); |
|
57 print(to.path); |
|
58 print(to.target); |
|
59 |
|
60 if (from.leafName != DOES_NOT_EXIST && from.isSymlink()) { |
|
61 // XXXjag wish I could set followLinks to false so we'd just get |
|
62 // the symlink's direct target instead of the final target. |
|
63 do_check_eq(from.target, to.target); |
|
64 } |
|
65 else { |
|
66 do_check_eq(from.path, to.target); |
|
67 } |
|
68 |
|
69 return to; |
|
70 } |
|
71 |
|
72 function setupTestDir(testDir, relative) { |
|
73 var targetDir = testDir.clone(); |
|
74 targetDir.append(DIR_TARGET); |
|
75 |
|
76 if (testDir.exists()) { |
|
77 testDir.remove(true); |
|
78 } |
|
79 do_check_true(!testDir.exists()); |
|
80 |
|
81 testDir.create(nsIFile.DIRECTORY_TYPE, 0777); |
|
82 |
|
83 targetDir.create(nsIFile.DIRECTORY_TYPE, 0777); |
|
84 |
|
85 var targetFile = testDir.clone(); |
|
86 targetFile.append(FILE_TARGET); |
|
87 targetFile.create(nsIFile.NORMAL_FILE_TYPE, 0666); |
|
88 |
|
89 var imaginary = testDir.clone(); |
|
90 imaginary.append(DOES_NOT_EXIST); |
|
91 |
|
92 var loop = testDir.clone(); |
|
93 loop.append(LOOP_LINK); |
|
94 |
|
95 var dirLink = makeSymLink(targetDir, DIR_LINK, relative); |
|
96 var fileLink = makeSymLink(targetFile, FILE_LINK, relative); |
|
97 |
|
98 makeSymLink(dirLink, DIR_LINK_LINK, relative); |
|
99 makeSymLink(fileLink, FILE_LINK_LINK, relative); |
|
100 |
|
101 makeSymLink(imaginary, DANGLING_LINK, relative); |
|
102 |
|
103 try { |
|
104 makeSymLink(loop, LOOP_LINK, relative); |
|
105 do_check_true(false); |
|
106 } |
|
107 catch (e) { |
|
108 } |
|
109 } |
|
110 |
|
111 function createSpaces(dirs, files, links) { |
|
112 function longest(a, b) a.length > b.length ? a : b; |
|
113 return dirs.concat(files, links).reduce(longest, "").replace(/./g, " "); |
|
114 } |
|
115 |
|
116 function testSymLinks(testDir, relative) { |
|
117 setupTestDir(testDir, relative); |
|
118 |
|
119 const dirLinks = [DIR_LINK, DIR_LINK_LINK]; |
|
120 const fileLinks = [FILE_LINK, FILE_LINK_LINK]; |
|
121 const otherLinks = [DANGLING_LINK, LOOP_LINK]; |
|
122 const dirs = [DIR_TARGET].concat(dirLinks); |
|
123 const files = [FILE_TARGET].concat(fileLinks); |
|
124 const links = otherLinks.concat(dirLinks, fileLinks); |
|
125 |
|
126 const spaces = createSpaces(dirs, files, links); |
|
127 const bools = {false: " false", true: " true "}; |
|
128 print(spaces + " dir file symlink"); |
|
129 var dirEntries = testDir.directoryEntries; |
|
130 while (dirEntries.hasMoreElements()) { |
|
131 const file = dirEntries.getNext().QueryInterface(nsIFile); |
|
132 const name = file.leafName; |
|
133 print(name + spaces.substring(name.length) + bools[file.isDirectory()] + |
|
134 bools[file.isFile()] + bools[file.isSymlink()]); |
|
135 do_check_eq(file.isDirectory(), dirs.indexOf(name) != -1); |
|
136 do_check_eq(file.isFile(), files.indexOf(name) != -1); |
|
137 do_check_eq(file.isSymlink(), links.indexOf(name) != -1); |
|
138 } |
|
139 } |
|
140 |
|
141 function run_test() { |
|
142 // Skip this test on Windows |
|
143 if (isWin) |
|
144 return; |
|
145 |
|
146 var testDir = CWD; |
|
147 testDir.append("test_symlinks"); |
|
148 |
|
149 testSymLinks(testDir, false); |
|
150 testSymLinks(testDir, true); |
|
151 } |