1--TEST--
2Test symlink(), linkinfo(), link() and is_link() functions : usage variations - access/update file through softlink
3--SKIPIF--
4<?php
5if (PHP_OS_FAMILY === 'Windows') {
6    require_once __DIR__ . '/windows_links/common.inc';
7    skipIfSeCreateSymbolicLinkPrivilegeIsDisabled(__FILE__);
8}
9?>
10--FILE--
11<?php
12/* Prototype: bool symlink ( string $target, string $link );
13   Description: creates a symbolic link to the existing target with the specified name link
14
15   Prototype: bool is_link ( string $filename );
16   Description: Tells whether the given file is a symbolic link.
17
18   Prototype: bool link ( string $target, string $link );
19   Description: Create a hard link
20
21   Prototype: int linkinfo ( string $path );
22   Description: Gets information about a link
23*/
24
25/* Variation 3 : Create file and a soft link to the file
26                 Access data of the file through the soft link
27                 Update the file through soft link
28                 Check size of file and soft link link
29*/
30
31$file_path = __DIR__;
32echo "*** Accessing and updating data of file through soft link ***\n";
33// Creating file and inserting data into it
34$filename = "$file_path/symlink_link_linkinfo_is_link_variation3.tmp";
35
36// create temp file
37$file = fopen($filename, "w");
38
39// create soft link to file
40$linkname = "$file_path/symlink_link_linkinfo_is_link_link_variation3.tmp";
41var_dump( symlink($filename, $linkname) );
42// storing size of symlink in a local variable
43$link_stat = lstat($linkname);  // lstat of link
44$link_size = $link_stat[7];  // size of soft link
45
46// fill data into file
47fwrite($file, str_repeat("text", 20) );
48fclose($file);
49
50echo "\n-- Access data of the file through the soft link --\n";
51$data_from_link = file_get_contents($linkname);  // data read from $filename
52var_dump( $data_from_link );
53
54echo "\n-- Check size of soft link and file --\n";
55var_dump( filesize($filename) );
56var_dump( filesize($linkname) );
57
58// taking lstat of symlink
59$stat = lstat($linkname);
60// checking that size of symlink remains same
61if ($link_size == $stat[7])
62  echo "\nSoft link size remains same \n";
63else
64  echo "\nWarning: Soft link size has changed \n";
65
66echo "\n-- Updating file with data through soft link --\n";
67// append link with data
68$fp = fopen($linkname, "a");  // open in append mode
69fwrite($fp, "Hello World");
70fclose($fp);
71
72// now check temp file for data; it should append "Hello World"
73$data_from_file = file_get_contents($filename);
74var_dump( $data_from_file );
75
76echo "\n-- Check size of soft link and file --\n";
77var_dump( filesize($filename) );
78var_dump( filesize($linkname) );
79
80// taking lstat of symlink
81$stat = lstat($linkname);
82// checking that size of symlink remains same
83if ($link_size == $stat[7])
84  echo "\nSoft link size remains same \n";
85else
86  echo "\nWarning: Soft link size has changed \n";
87
88echo "\n-- Updating file with data and check data through soft link --\n";
89// write to temp file
90$file = fopen($filename, "w");
91fwrite($file, "Hello World");
92fclose($file);
93
94// now check link for data; it should echo "Hello World"
95$data_from_link = file_get_contents($linkname);
96var_dump( $data_from_link );
97
98echo "\n-- Check size of soft link and file --\n";
99var_dump( filesize($filename) );
100var_dump( filesize($linkname) );
101
102// taking lstat of symlink
103$stat = lstat($linkname);
104// checking that size of symlink remains same
105if ($link_size == $stat[7])
106  echo "\nSoft link size remains same \n";
107else
108  echo "\nWarning: Soft link size has changed \n";
109
110// delete the link
111unlink($linkname);
112// delete the temporary file
113unlink($filename);
114
115echo "Done\n";
116?>
117--EXPECT--
118*** Accessing and updating data of file through soft link ***
119bool(true)
120
121-- Access data of the file through the soft link --
122string(80) "texttexttexttexttexttexttexttexttexttexttexttexttexttexttexttexttexttexttexttext"
123
124-- Check size of soft link and file --
125int(80)
126int(80)
127
128Soft link size remains same
129
130-- Updating file with data through soft link --
131string(91) "texttexttexttexttexttexttexttexttexttexttexttexttexttexttexttexttexttexttexttextHello World"
132
133-- Check size of soft link and file --
134int(91)
135int(91)
136
137Soft link size remains same
138
139-- Updating file with data and check data through soft link --
140string(11) "Hello World"
141
142-- Check size of soft link and file --
143int(11)
144int(11)
145
146Soft link size remains same
147Done
148