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