1--TEST--
2Test symlink(), linkinfo(), link() and is_link() functions : usage variations - access/update file through hard link
3--FILE--
4<?php
5/* Prototype: bool symlink ( string $target, string $link );
6   Description: creates a symbolic link to the existing target with the specified name link
7
8   Prototype: bool is_link ( string $filename );
9   Description: Tells whether the given file is a symbolic link.
10
11   Prototype: bool link ( string $target, string $link );
12   Description: Create a hard link
13
14   Prototype: int linkinfo ( string $path );
15   Description: Gets information about a link
16*/
17
18/* Variation 4 : Create file and a hard link to the file
19                 Access data of the file through the hard link
20                 Update the file through hard link
21                 Check size of file and hard link
22*/
23$file_path = __DIR__;
24
25echo "*** Accessing and updating data of file through hard link ***\n";
26// Creating file and inserting data into it
27$filename = "$file_path/symlink__link_linkinfo_is_link_variation4.tmp";
28// create temp file
29$file = fopen($filename, "w");
30// fill data into file
31fwrite($file, str_repeat("text", 20) );
32fclose($file);
33
34echo "\n-- Access data of the file through the hard link --\n";
35// create hard link to file
36$linkname = "$file_path/symlink_link_linkinfo_is_link_link_variation4.tmp";
37var_dump( link($filename, $linkname) );
38$data_from_link = file_get_contents($linkname);  // data read from $filename
39var_dump( $data_from_link );
40
41echo "\n-- Check size of hard link and file --\n";
42if( filesize($filename) == filesize($linkname) )
43  echo "\nSize of file and hard link are same\n";
44else
45  echo "\nWarning: Size of file and hard link differ\n";
46
47echo "\n-- Updating file with data through hard link --\n";
48// append link with data
49$fp = fopen($linkname, "a");  // open in append mode
50fwrite($fp, "Hello World");
51fclose($fp);
52
53// now check temp file for data; it should append "Hello World"
54$data_from_file = file_get_contents($filename);
55var_dump( $data_from_file );
56
57echo "\n-- Check size of hard link and file --\n";
58if( filesize($filename) == filesize($linkname) )
59  echo "\nSize of file and hard link are same\n";
60else
61  echo "\nWarning: Size of file and hard link differ\n";
62
63echo "\n-- Updating file with data and check data through hard link --\n";
64// write to temp file
65$file = fopen($filename, "w");
66fwrite($file, "Hello World");
67fclose($file);
68
69// now check link for data; it should echo "Hello World"
70$data_from_link = file_get_contents($linkname);
71var_dump( $data_from_link );
72
73echo "\n-- Check size of hard link and file --\n";
74var_dump( filesize($filename) );
75var_dump( filesize($linkname) );
76if( filesize($filename) == filesize($linkname) )
77  echo "\nSize of file and hard link are same\n";
78else
79  echo "\nWarning: Size of file and hard link differ\n";
80
81// delete the link
82unlink($linkname);
83// delete the temporary file
84unlink($filename);
85
86echo "Done\n";
87?>
88--EXPECT--
89*** Accessing and updating data of file through hard link ***
90
91-- Access data of the file through the hard link --
92bool(true)
93string(80) "texttexttexttexttexttexttexttexttexttexttexttexttexttexttexttexttexttexttexttext"
94
95-- Check size of hard link and file --
96
97Size of file and hard link are same
98
99-- Updating file with data through hard link --
100string(91) "texttexttexttexttexttexttexttexttexttexttexttexttexttexttexttexttexttexttexttextHello World"
101
102-- Check size of hard link and file --
103
104Size of file and hard link are same
105
106-- Updating file with data and check data through hard link --
107string(11) "Hello World"
108
109-- Check size of hard link and file --
110int(11)
111int(11)
112
113Size of file and hard link are same
114Done
115