1--TEST--
2Test unlink() function : variation: various valid and invalid paths
3--CREDITS--
4Dave Kelsey <d_kelsey@uk.ibm.com>
5--SKIPIF--
6<?php
7if (substr(PHP_OS, 0, 3) == 'WIN') {
8    die('skip.. Not for Windows');
9}
10?>
11--FILE--
12<?php
13/* Prototype  : bool unlink(string filename[, context context])
14 * Description: Delete a file
15 * Source code: ext/standard/file.c
16 * Alias to functions:
17 */
18
19echo "*** Testing unlink() : variation ***\n";
20
21$workDir = "unlinkVar8.tmp";
22$tmpFile = "file.tmp";
23$fileToLinkTo = $workDir.'/'."linkme.tmp";
24
25mkdir($workDir);
26$cwd = getcwd();
27touch($fileToLinkTo);
28
29$files = array(
30             // relative
31             $workDir.'/'.$tmpFile,
32             './'.$workDir.'/'.$tmpFile,
33             $workDir.'/../'.$workDir.'/'.$tmpFile,
34
35             // relative bad path
36             $workDir.'/../BADDIR/'.$tmpFile,
37             'BADDIR/'.$tmpFile,
38
39             //absolute
40             $cwd.'/'.$workDir.'/'.$tmpFile,
41             $cwd.'/./'.$workDir.'/'.$tmpFile,
42             $cwd.'/'.$workDir.'/../'.$workDir.'/'.$tmpFile,
43
44             //absolute bad path
45             $cwd.'/BADDIR/'.$tmpFile,
46
47             //trailing separators
48             $workDir.'/'.$tmpFile.'/',
49             $cwd.'/'.$workDir.'/'.$tmpFile.'/',
50
51             // multiple separators
52             $workDir.'//'.$tmpFile,
53             $cwd.'//'.$workDir.'//'.$tmpFile,
54
55             );
56
57
58foreach($files as $fileToUnlink) {
59   test_realfile($workDir.'/'.$tmpFile, $fileToUnlink);
60   test_link($workDir.'/'.$tmpFile, $fileToLinkTo, $fileToUnlink, true);  //soft link
61   test_link($workDir.'/'.$tmpFile, $fileToLinkTo, $fileToUnlink, false); //hard link
62}
63
64unlink($fileToLinkTo);
65rmdir($workDir);
66
67function test_realfile($file, $tounlink) {
68   touch($file);
69   echo "-- removing $tounlink --\n";
70   $res = unlink($tounlink);
71   if ($res === true) {
72      if (file_exists($tounlink) === false) {
73      	echo "file removed\n";
74      }
75      else {
76        echo "FAILED: file not removed\n";
77      }
78   }
79   else {
80      unlink($file);
81   }
82}
83
84function test_link($linkedfile, $toLinkTo, $tounlink, $softlink) {
85   if ($softlink == true) {
86   	  symlink($toLinkTo, $linkedfile);
87   	  $msg = "soft link";
88   }
89   else {
90   	  link($toLinkTo, $linkedfile);
91   	  $msg = "hard link";
92   }
93   echo "-- unlinking $msg $tounlink --\n";
94   $res = unlink($tounlink);
95   if ($res === true) {
96      if (file_exists($tounlink) === false) {
97      	echo "file unlinked\n";
98      }
99      else {
100        echo "FAILED: file not unlinked\n";
101      }
102   }
103   else {
104      unlink($linkedfile);
105   }
106}
107
108
109?>
110===DONE===
111--EXPECTF--
112*** Testing unlink() : variation ***
113-- removing unlinkVar8.tmp/file.tmp --
114file removed
115-- unlinking soft link unlinkVar8.tmp/file.tmp --
116file unlinked
117-- unlinking hard link unlinkVar8.tmp/file.tmp --
118file unlinked
119-- removing ./unlinkVar8.tmp/file.tmp --
120file removed
121-- unlinking soft link ./unlinkVar8.tmp/file.tmp --
122file unlinked
123-- unlinking hard link ./unlinkVar8.tmp/file.tmp --
124file unlinked
125-- removing unlinkVar8.tmp/../unlinkVar8.tmp/file.tmp --
126file removed
127-- unlinking soft link unlinkVar8.tmp/../unlinkVar8.tmp/file.tmp --
128file unlinked
129-- unlinking hard link unlinkVar8.tmp/../unlinkVar8.tmp/file.tmp --
130file unlinked
131-- removing unlinkVar8.tmp/../BADDIR/file.tmp --
132
133Warning: unlink(unlinkVar8.tmp/../BADDIR/file.tmp): No such file or directory in %s on line %d
134-- unlinking soft link unlinkVar8.tmp/../BADDIR/file.tmp --
135
136Warning: unlink(unlinkVar8.tmp/../BADDIR/file.tmp): No such file or directory in %s on line %d
137-- unlinking hard link unlinkVar8.tmp/../BADDIR/file.tmp --
138
139Warning: unlink(unlinkVar8.tmp/../BADDIR/file.tmp): No such file or directory in %s on line %d
140-- removing BADDIR/file.tmp --
141
142Warning: unlink(BADDIR/file.tmp): No such file or directory in %s on line %d
143-- unlinking soft link BADDIR/file.tmp --
144
145Warning: unlink(BADDIR/file.tmp): No such file or directory in %s on line %d
146-- unlinking hard link BADDIR/file.tmp --
147
148Warning: unlink(BADDIR/file.tmp): No such file or directory in %s on line %d
149-- removing /%s/unlinkVar8.tmp/file.tmp --
150file removed
151-- unlinking soft link /%s/unlinkVar8.tmp/file.tmp --
152file unlinked
153-- unlinking hard link /%s/unlinkVar8.tmp/file.tmp --
154file unlinked
155-- removing /%s/./unlinkVar8.tmp/file.tmp --
156file removed
157-- unlinking soft link /%s/./unlinkVar8.tmp/file.tmp --
158file unlinked
159-- unlinking hard link /%s/./unlinkVar8.tmp/file.tmp --
160file unlinked
161-- removing /%s/unlinkVar8.tmp/../unlinkVar8.tmp/file.tmp --
162file removed
163-- unlinking soft link /%s/unlinkVar8.tmp/../unlinkVar8.tmp/file.tmp --
164file unlinked
165-- unlinking hard link /%s/unlinkVar8.tmp/../unlinkVar8.tmp/file.tmp --
166file unlinked
167-- removing /%s/BADDIR/file.tmp --
168
169Warning: unlink(/%s/BADDIR/file.tmp): No such file or directory in %s on line %d
170-- unlinking soft link /%s/BADDIR/file.tmp --
171
172Warning: unlink(/%s/BADDIR/file.tmp): No such file or directory in %s on line %d
173-- unlinking hard link /%s/BADDIR/file.tmp --
174
175Warning: unlink(/%s/BADDIR/file.tmp): No such file or directory in %s on line %d
176-- removing unlinkVar8.tmp/file.tmp/ --
177
178Warning: unlink(unlinkVar8.tmp/file.tmp/): Not a directory in %s on line %d
179-- unlinking soft link unlinkVar8.tmp/file.tmp/ --
180
181Warning: unlink(unlinkVar8.tmp/file.tmp/): %s directory in %s on line %d
182-- unlinking hard link unlinkVar8.tmp/file.tmp/ --
183
184Warning: unlink(unlinkVar8.tmp/file.tmp/): Not a directory in %s on line %d
185-- removing /%s/unlinkVar8.tmp/file.tmp/ --
186
187Warning: unlink(/%s/unlinkVar8.tmp/file.tmp/): Not a directory in %s on line %d
188-- unlinking soft link /%s/unlinkVar8.tmp/file.tmp/ --
189
190Warning: unlink(/%s/unlinkVar8.tmp/file.tmp/): %s directory in %s on line %d
191-- unlinking hard link /%s/unlinkVar8.tmp/file.tmp/ --
192
193Warning: unlink(/%s/unlinkVar8.tmp/file.tmp/): Not a directory in %s on line %d
194-- removing unlinkVar8.tmp//file.tmp --
195file removed
196-- unlinking soft link unlinkVar8.tmp//file.tmp --
197file unlinked
198-- unlinking hard link unlinkVar8.tmp//file.tmp --
199file unlinked
200-- removing /%s//unlinkVar8.tmp//file.tmp --
201file removed
202-- unlinking soft link /%s//unlinkVar8.tmp//file.tmp --
203file unlinked
204-- unlinking hard link /%s//unlinkVar8.tmp//file.tmp --
205file unlinked
206===DONE===
207