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