file.inc 22 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650
  1. <?php
  2. /* Header file for common file test functions
  3. Following functions are provided :
  4. create_files() : create files with specified contents
  5. delete_files() : delete files
  6. create_links() : crate links of different types
  7. delete_links() : delete links
  8. fill_files() : fill file with specified contents
  9. change_file_perms() : Change permission of files
  10. fill_buffer() : fills buffer with specified contents
  11. compare_self_stat() : compares the first 13 elements of the
  12. stat with the corresponding named key values of
  13. the same stat.
  14. compare_stats() : Compares two stat values
  15. */
  16. const FILE_NOT_FOUND = 2;
  17. /*
  18. Function: bool create_file(string $filename, string $mode = "w");
  19. Description: creates a new file using fopen() call
  20. $filename = Name of the file
  21. $mode = Mode as specified in fopen call, read documentation of fopen() call for more info
  22. Returns:
  23. true on success, false otherwise
  24. */
  25. function create_file($filename, $mode = "w") {
  26. $file_handle = fopen ($filename, $mode);
  27. if ( $file_handle == false )
  28. return false;
  29. fclose($file_handle);
  30. return true;
  31. }
  32. /*
  33. Function : bool fill_buffer(string &$buffer, string $fill_type, int $fill_size);
  34. Description: Fills the $buffer with data as specified with requested size.
  35. $buffer = buffer to be filled
  36. $fill_type:
  37. "text" = fills with string of size $file_size
  38. "numeric" = fills with numeric value of size $file_size
  39. "text_with_new_line" = similar to "text" fill type but writes with new line
  40. "alphanumeric" = fills with alphnumeric values
  41. Returns: true on success, false on invalid fill type
  42. */
  43. function fill_buffer(&$buffer, $fill_type, $fill_size) {
  44. if ( $fill_type == "text" ) {
  45. $data = "text ";
  46. $size_divider = strlen($data);
  47. $add_value = strlen($data);
  48. } else if ( $fill_type == "text_with_new_line" ) {
  49. $data = "line\nline of text\n";
  50. $size_divider = strlen($data);
  51. $add_value = strlen($data);
  52. } else if ( $fill_type == "alphanumeric" ) {
  53. $data = "ab12 ";
  54. $size_divider = strlen($data);
  55. $add_value = strlen($data);
  56. } else if ( $fill_type == "numeric" ) {
  57. $data = 2;
  58. $size_divider = 1;
  59. $add_value = 0;
  60. } else {
  61. // invalid fill type;
  62. return false;
  63. }
  64. $tmp_buff = str_repeat($data, (int)(($fill_size/$size_divider) + $add_value) );
  65. if ( strlen($tmp_buff) > $fill_size ) {
  66. $buffer = substr($tmp_buff, 0, $fill_size);
  67. } else {
  68. $buffer = $tmp_buff;
  69. }
  70. return true;
  71. }
  72. /*
  73. Function : bool fill_file(resource $file_handle, string $fill_type, string $file_size);
  74. Description: Fills the file with data as specified with requested size.
  75. $file_handle = file handle, opened with write options,
  76. $fill_type:
  77. "text" = fills with string of size $file_size
  78. "numeric" = fills with numeric value of size $file_size
  79. "empty" = no fill operation performed, returns true
  80. "text_with_new_line" = similar to "text" fill type but writes with new line
  81. "alphanumeric" = fills with alphnumeric values
  82. Returns: true on success, false on failure & invalid fill type
  83. */
  84. function fill_file($file_handle, $fill_type, $file_size) {
  85. if ( $fill_type == "empty" ) {
  86. // no fill required, return true
  87. return true;
  88. } if ( $fill_type == "text" ) {
  89. $data = "text ";
  90. $size_divider = strlen($data);
  91. $add_value = strlen($data);
  92. } else if ( $fill_type == "text_with_new_line" ) {
  93. $data = "line\nline of text\n";
  94. $size_divider = strlen($data);
  95. $add_value = strlen($data);
  96. } else if ( $fill_type == "alphanumeric" ) {
  97. $data = "ab12 ";
  98. $size_divider = strlen($data);
  99. $add_value = strlen($data);
  100. } else if ( $fill_type == "numeric" ) {
  101. $data = 2;
  102. $size_divider = 1;
  103. $add_value = 0;
  104. } else {
  105. // invalid fill type;
  106. return false;
  107. }
  108. // write in terms of a chunk of 1 K to avoid memory size overflow
  109. $size = $file_size;
  110. $chunk_size = 1024;
  111. if ( $size > $chunk_size ) {
  112. $loop_count = 1;
  113. do {
  114. $loop_count ++;
  115. if ( $size <= $chunk_size ) {
  116. $chunk_size = $size;
  117. }
  118. $num_values = str_repeat($data, (int) (($chunk_size/$size_divider) + $add_value) );
  119. $bytes_written = fwrite($file_handle, $num_values, $chunk_size);
  120. if ( $bytes_written != $chunk_size ) {
  121. return false;
  122. }
  123. $size -= $chunk_size;
  124. } while ( $size > 0 );
  125. } else {
  126. $num_values = str_repeat($data, (int) (($chunk_size/$size_divider) + $add_value) );
  127. $bytes_written = fwrite($file_handle, $num_values, $file_size);
  128. if ( $bytes_written != $file_size ) {
  129. return false;
  130. }
  131. }
  132. // successful, return true
  133. return true;
  134. }
  135. /*
  136. Function: int change_file_perms(string $file_path, int $count = 1, int $perms = 0755,
  137. string $name_prefix = "file",
  138. string $name_suffix = 1, $file_extension = ".tmp");
  139. Description: changes file permission for given file(s).
  140. $file_path = dir path where file exists
  141. $count = no. of files, default is 1
  142. $perms = new permission of the file, similar to $mode args of chmod() call
  143. $name_prefix = common name prefix, default is "file"
  144. $name_suffix = suffix to end the common name given in name_prefix to create
  145. a unique name. default is 1.
  146. $file_extension = default is .tmp
  147. Returns:
  148. Integer, Count of total files permission changed.
  149. */
  150. function change_file_perms($file_path,
  151. $count = 1,
  152. $perms = 0755,
  153. $name_prefix = "file",
  154. $name_suffix = 1,
  155. $file_extension = ".tmp" )
  156. {
  157. $changed = 0;
  158. if( $count <= 0 )
  159. return $changed;
  160. if ( $name_suffix <= 0)
  161. $name_suffix = 1;
  162. for($loop_counter = 1; $loop_counter <= $count; $loop_counter++) {
  163. $filename = $file_path."/".$name_prefix.$name_suffix.$file_extension;
  164. if( chmod($filename, $perms) )
  165. $changed++;
  166. $name_suffix++;
  167. }
  168. return $changed;
  169. }
  170. /*
  171. Function: array create_files( string $file_path,
  172. int $count = 1,
  173. string $content_type = "numeric",
  174. int $permission = 0755,
  175. int $size = 1,
  176. string $mode = "w",
  177. string $name_prefix = "file",
  178. int $name_suffix = 1,
  179. string $flag = "kilobytes"
  180. string $file_extension = ".tmp"
  181. );
  182. Description: Creates given number of files with specified mode and
  183. permissions. File is filled with content of size specified.
  184. $file_path = dir where files will be created
  185. $name_prefix = prefix to be used for names, name is suffix with a
  186. unique numeric value to make the file name unique, default = file
  187. $name_suffix = suffix to be used for the name, default = 1
  188. $count = total no. of files to be created, default = 1
  189. $mode = file open mode as specified in fopen() call. Do not use
  190. modes used for only reading the file. Default = "w"
  191. $permission = An octal number, This should be similar to $mode
  192. specified in chmod() call.
  193. $content_type = Specify type of the content to fill in the file.
  194. "numeric" = fill file with numeric values
  195. "text" = fill file with regular text
  196. "empty" = empty file
  197. "text_with_new_line" = similar to text fill type, but writes with new line char
  198. "alphanumeric" = fill file with alpha numeric text
  199. If improper $content type is specified, file is created as empty
  200. $size = size of the fill in terms of kilobyte, i.e size of the file.
  201. if $flag is specified as "byte", then then given size is taken in bytes
  202. $flag = specify if size has to be treated as no of total bytes or
  203. multiple of KB.
  204. "kilobytes" = take size in terms of multiple of KB
  205. "byte" = take size in terms of bytes
  206. $file_extension = default is .tmp
  207. Returns:
  208. An array with following key value pair:
  209. created => total file created
  210. filled => total files filled
  211. perms_changed => total files permission changed
  212. */
  213. function create_files( $file_path,
  214. $count = 1,
  215. $content_type = "numeric",
  216. $permission = 0755,
  217. $size = 1,
  218. $mode = "w",
  219. $name_prefix = "file",
  220. $name_suffix = 1,
  221. $flag = "kilobytes",
  222. $file_extension = ".tmp"
  223. )
  224. {
  225. $return_value = array('created' => 0, 'filled' => 0, 'perms_changed' => 0);
  226. //ensure that suffix is a +ve integer
  227. if ($name_suffix <= 0) {
  228. $name_suffix = 1;
  229. }
  230. // check for proper size
  231. if ( $size == 0 )
  232. return $return_value;
  233. // prepare the size based on flag
  234. $file_size = $size;
  235. if ( $flag == "kilobytes" ) {
  236. $file_size = $file_size * 1024;
  237. }
  238. $tmp_name_suffix = $name_suffix;
  239. // create the files with specified mode and permission
  240. for($file_created_count = 1; $file_created_count <= $count; $file_created_count ++) {
  241. $filename = $file_path."/".$name_prefix.$tmp_name_suffix.$file_extension;
  242. $status = create_file($filename, $mode);
  243. $tmp_name_suffix++;
  244. if ($status == true) {
  245. $return_value['created']++;
  246. }
  247. else {
  248. return $return_value;
  249. }
  250. }
  251. if ( $content_type == "empty" ) {
  252. $return_value['filled'] = $count;
  253. } else {
  254. // fill the file with specifiec type of data and size
  255. $tmp_name_suffix = $name_suffix;
  256. for($loop_counter = 1; $loop_counter <= $count; $loop_counter ++) {
  257. $filename = $file_path."/".$name_prefix.$tmp_name_suffix.$file_extension;
  258. $file_handle = fopen($filename, $mode);
  259. if($file_handle == false) {
  260. fclose($file_handle);
  261. return $return_value;
  262. } // end of if
  263. // call fill_file() to fill the file
  264. if( fill_file($file_handle, $content_type, $file_size) )
  265. $return_value['filled']++;
  266. fclose($file_handle);
  267. $tmp_name_suffix++;
  268. } // end of for
  269. }
  270. // change all file's permissions
  271. $return_value['perms_changed'] = change_file_perms($file_path, $count, $permission, $name_prefix,
  272. $name_suffix, $file_extension);
  273. return $return_value;
  274. }
  275. /*
  276. Function: function create_links( $file_path,
  277. $filename,
  278. $link_count = 1,
  279. $link_type = "soft",
  280. $link_size = 1024,
  281. $link_name_prefix = "link",
  282. $link_name_suffix = 1,
  283. $link_file_content = "text",
  284. $link_perms = 0755,
  285. $link_file_extension = ".tmp"
  286. );
  287. Description: Creates given number of links with specified mode and
  288. permissions.Link is filled with content of size specified.
  289. $file_path = location of the file and where links need to be created
  290. $link_name_prefix = prefix to be used for names, name is suffix with a
  291. unique numeric value to make the file name unique, default = link
  292. $link_name_suffix = suffix to be used for the name, default = 1
  293. $link_count = total no. of links to be created to given file, default = 1
  294. $link_perms = An octal number, This should be similar to $mode
  295. specified in chmod() call.
  296. $link_file_content = Type of the content to fill in the file.
  297. numeric = fill file with numeric values
  298. text = fill file with regular text
  299. text_with_new_line = same as text but new lines are written
  300. alphanumeric = fill with alphanumeric text
  301. If imporper $content type is specified, file is created as empty
  302. $size = size of the fill in terms of kilobyte, i.e size of the file.
  303. $link_type = type of the link to be created
  304. "soft" = soft link
  305. "hard" = hard link
  306. $filename = file used to create a link on
  307. Returns:
  308. An array with following key value pair:
  309. created => total file created
  310. filled => total files filled, always returned as 1
  311. perms_changed => total files permission changed
  312. */
  313. function create_links($file_path,
  314. $filename,
  315. $link_count = 1,
  316. $link_type = "soft",
  317. $link_size = 1024,
  318. $link_name_prefix = "link",
  319. $link_name_suffix = 1,
  320. $link_file_content = "text",
  321. $link_perms = 0755,
  322. $link_file_extension = ".tmp"
  323. )
  324. {
  325. $return_value = array('created' => 0, 'filled' => 0, 'perms_changed' => 0);
  326. $tmp_name_suffix = $link_name_suffix;
  327. $src_filename = $file_path."/".$filename;
  328. switch( $link_type ) {
  329. default :
  330. case "soft" : // create a soft link
  331. for($link_created_count = 1; $link_created_count <= $link_count; $link_created_count++) {
  332. $linkname = $file_path."/".$link_name_prefix.$tmp_name_suffix.$link_file_extension;
  333. $status = symlink( $src_filename, $linkname);
  334. $tmp_name_suffix++;
  335. if ($status) {
  336. $return_value['created']++;
  337. }
  338. else {
  339. $return_value;
  340. }
  341. }
  342. break;
  343. case "hard" : // create a hard link
  344. for($link_created_count = 1; $link_created_count <= $link_count; $link_created_count++) {
  345. $linkname = $file_path."/".$link_name_prefix.$tmp_name_suffix.$link_file_extension;
  346. $status = link($src_filename, $linkname);
  347. $tmp_name_suffix++;
  348. if ($status) {
  349. $return_value['created']++;
  350. }
  351. else {
  352. $return_value;
  353. }
  354. }
  355. break;
  356. }
  357. if ( $link_file_content == "empty" ) {
  358. $return_value['filled'] = 1;
  359. return $return_value;
  360. }
  361. // fill the file with specific type of data and size
  362. $tmp_name_suffix = $link_name_suffix;
  363. $linkname = $file_path."/".$link_name_prefix.$tmp_name_suffix.$link_file_extension;
  364. $file_handle = fopen($linkname, "w");
  365. if($file_handle == false) {
  366. return $return_value;
  367. } // end of if
  368. // call fill_file() to fill the file
  369. if( fill_file($file_handle, $link_file_content, $link_size) )
  370. $return_value['filled']++;
  371. // close the link
  372. fclose($file_handle);
  373. // change the permission of the link file, only if hard link.
  374. // this is not applicable to soft links
  375. if( $link_type == "hard" ) {
  376. $return_value['perms_changed'] = change_file_perms($file_path,
  377. $link_count,
  378. $link_perms,
  379. $link_name_prefix,
  380. $link_name_suffix,
  381. $link_file_extension );
  382. }
  383. return $return_value;
  384. }
  385. /*
  386. Function: bool delete_file(string $filename);
  387. Description: delete a given file if exists
  388. Returns: true on success
  389. false on failure
  390. FILE_NOT_FOUND if file doesn't exist
  391. */
  392. function delete_file($filename) {
  393. // check if file exists
  394. if ( file_exists($filename) ) {
  395. if ( unlink($filename) )
  396. return true;
  397. else
  398. return false;
  399. }
  400. return FILE_NOT_FOUND;
  401. }
  402. /*
  403. Function: array delete_files(string $file_path, int $count = 1, string $name_prefix = "file",
  404. int name_suffix = 1, $file_extension = ".tmp" );
  405. Description: Deletes given number of files if exists.
  406. $file_path = location of the files
  407. $name_prefix = prefix for the filename, rest of the name is incremental(increment by 1 only)
  408. numeric starting from suffix up to count
  409. $count = number of files to be deleted
  410. $name_suffix = first numeric suffix in the name
  411. Returns: An array with following key/value pair
  412. deleted = Number of files deleted.
  413. notfound = Count of non existing file
  414. failed = Count of failed to delete
  415. */
  416. function delete_files($file_path,
  417. $count = 1,
  418. $name_prefix = "file",
  419. $name_suffix = 1,
  420. $file_extension = ".tmp")
  421. {
  422. $return_value = array ('deleted' => 0, 'notfound' => 0, 'failed' => 0);
  423. if ( $name_suffix < 1 )
  424. $name_suffix = 1;
  425. for($loop_counter = 1; $loop_counter <= $count; $loop_counter++) {
  426. $filename = $file_path."/".$name_prefix.$name_suffix.$file_extension;
  427. $name_suffix++;
  428. $status = delete_file($filename);
  429. if($status == true) {
  430. $return_value['deleted']++;
  431. } else if($status == FILE_NOT_FOUND) {
  432. $return_value['notfound']++;
  433. } else {
  434. $return_value['failed']++;
  435. }
  436. } // end of for
  437. return $return_value;
  438. }
  439. /*
  440. Function: array delete_links( $file_path,
  441. $link_file_count,
  442. $link_name_prefix,
  443. $link_name_suffix,
  444. $link_file_extension );
  445. Description: Deletes given number of links if exists. Uses delete_files() function
  446. $file_path = location of link files
  447. $link_file_count = Number of link files
  448. $link_name_prefix = prefix for the linkname, rest of the name is incremental(increment by 1 only)
  449. numeric starting from $link_name_suffix up to count
  450. $link_name_suffix = first numeric suffix in the name
  451. Returns: An array with following key/value pair
  452. deleted = Number of links deleted.
  453. notfound = Count of non existing link
  454. failed = Count of failed to delete
  455. */
  456. function delete_links($file_path,
  457. $link_file_count = 1,
  458. $link_name_prefix = "link",
  459. $link_name_suffix = 1,
  460. $link_file_extension = ".tmp")
  461. {
  462. // call the delete files to delete links
  463. $return_value = delete_files( $file_path,
  464. $link_file_count,
  465. $link_name_prefix,
  466. $link_name_suffix,
  467. $link_file_extension );
  468. return $return_value;
  469. }
  470. /*
  471. Prototype:
  472. function compare_self_stat( array $stat );
  473. Description:
  474. Compares the each of the first 13 values of the stat array with the
  475. corresponding next 13 values of the same stat for equality
  476. $stat = stat array
  477. Returns: true when all of them match, false otherwise
  478. */
  479. function compare_self_stat( array $stat )
  480. {
  481. //return value
  482. $return_value = true;
  483. // named keys present in a stat
  484. $string_keys = array("dev", "ino", "mode", "nlink", "uid", "gid",
  485. "rdev", "size", "atime", "mtime", "ctime",
  486. "blksize", "blocks");
  487. // first numeric key
  488. $key = 0;
  489. // compare the values in the stat, which are accessed using numeric key with
  490. // values accessed using string keys
  491. foreach($string_keys as $str_key)
  492. {
  493. if($stat[$key] != $stat[$str_key]) {
  494. echo "Error: stat[$key] doesn't match with stat[$str_key]\n";
  495. $flag = false;
  496. $key++;
  497. }
  498. else {
  499. $key++;
  500. }
  501. } // end of foreach
  502. // if the $return_value is false, i.e all the element do not match then
  503. // dump the stat array so that its easy to figure out the error
  504. if ($return_value == false ) {
  505. echo "\n Dumping stat array ...\n";
  506. var_dump($stat);
  507. }
  508. return $return_value;
  509. }// end of compare_self_stat
  510. /*
  511. Prototype:
  512. function compare_stats( array $stat1, array $stat2, array $fields,
  513. [string $op = "==", [ bool $flag = false] ]);
  514. Description:
  515. Compares two stat values, stat value should be obtained by stat/lstat
  516. $stat1 = first stat array
  517. $stat2 = second stat array
  518. $op = type of the comparison to be perform between elements of stat1 and stat2
  519. "!=" compare for not equal
  520. "==" compare for equality
  521. ">" if each element of stat1 is > than stat2
  522. "<" if each element of stat1 is < than stat2
  523. $fields = contains the key of the elements that needs to be compared.
  524. type of the comparison is based on $op argument value
  525. $flag = specify true to dump the stat1 and stat2
  526. */
  527. $all_stat_keys = array(0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12,
  528. "dev", "ino", "mode", "nlink", "uid", "gid",
  529. "rdev", "size", "atime", "mtime", "ctime",
  530. "blksize", "blocks");
  531. function compare_stats($stat1, $stat2, $fields, $op = "==", $flag = false ) {
  532. // dump the stat if requested
  533. if ( $flag == true ) {
  534. var_dump($stat1);
  535. var_dump($stat2);
  536. }
  537. $result = true;
  538. // compare values of given key from each stat array
  539. for($index = 0; $index < count($fields); $index++)
  540. {
  541. switch( $op )
  542. {
  543. case "==":
  544. if ( $stat1[ $fields[$index] ] != $stat2[ $fields[$index] ] ) {
  545. $result = false;
  546. echo "Error: stat1 do not match with stat2 at key value: $fields[$index]\n";
  547. }
  548. break;
  549. case "!=":
  550. if ( $stat1[ $fields[$index] ] != $stat2[ $fields[$index] ] ) {
  551. // do nothing as its not equal, else will take care of if equal
  552. } else {
  553. $result = false;
  554. echo "Error: stat1 equals stat2 at key value: $fields[$index]\n";
  555. }
  556. break;
  557. case ">":
  558. if ( $stat1[ $fields[$index] ] <= $stat2[ $fields[$index] ] ) {
  559. $result = false;
  560. echo "Error: stat1 is not greater than stat2 at key value: $fields[$index]\n";
  561. }
  562. break;
  563. case "<":
  564. if ( $stat1[ $fields[$index] ] >= $stat2[ $fields[$index] ] ) {
  565. $result = false;
  566. echo "Error: stat1 is not lesser than stat2 at key value: $fields[$index]\n";
  567. }
  568. break;
  569. }
  570. }
  571. // if the result is false(i.e values are not as expected),
  572. // dump the stat array so that easy to figure out the error
  573. if ( $result == false ) {
  574. echo "\n Dumping stat array 1...\n";
  575. var_dump($stat1);
  576. echo "\n Dumping stat array 2...\n";
  577. var_dump($stat2);
  578. }
  579. return $result;
  580. }
  581. ?>