Back to index

php5  5.3.10
file.inc
Go to the documentation of this file.
00001 <?php
00002 /* Header file for common file test functions 
00003      Following functions are provided :
00004        create_files() : create files with specified contents 
00005        delete_files() : delete files
00006        create_links() : crate links of different types
00007        delete_links() : delete links 
00008        fill_files()   : fill file with specified contents 
00009        change_file_perms() : Change permission of files 
00010        fill_buffer()  : fills buffer with specified contents
00011        compare_self_stat() : compares the first 13 elements of the 
00012                             stat with the corresponding named key values of
00013                             the same stat.
00014        compare_stats() : Compares two stat values
00015 
00016 */ 
00017 
00018 define('file_not_found', 2, 1);
00019 
00020 /* 
00021  Function: bool create_file(string $filename, string $mode = "w");
00022  Description: creates a new file using fopen() call 
00023    $filename = Name of the file 
00024    $mode = Mode as specified in fopen call, read documentation of fopen() call for more info
00025 
00026  Returns: 
00027    true on success, false otherwise
00028 */
00029 function create_file($filename, $mode = "w") {
00030   $file_handle = fopen ($filename, $mode);
00031   if ( $file_handle == false )
00032     return false;
00033   fclose($file_handle);
00034   return true;
00035 }
00036 
00037 /*
00038  Function : bool fill_buffer(string &$buffer, string $fill_type, int $fill_size);
00039  Description: Fills the $buffer with data as specified with requested size.
00040    $buffer = buffer to be filled
00041    $fill_type:
00042        "text" = fills with string of size $file_size
00043        "numeric" = fills with numeric value of size $file_size
00044        "text_with_new_line" = similar to "text" fill type but writes with new line
00045        "alphanumeric" = fills with alphnumeric values
00046  Returns: true on success, false on invalid fill type
00047 */
00048 function fill_buffer(&$buffer, $fill_type, $fill_size) {
00049 
00050   if ( $fill_type == "text" ) {
00051     $data = "text ";
00052     $size_divider = strlen($data);
00053     $add_value = strlen($data);
00054   } else if ( $fill_type == "text_with_new_line" ) {
00055     $data = "line\nline of text\n";
00056     $size_divider = strlen($data);
00057     $add_value = strlen($data);
00058   } else if ( $fill_type == "alphanumeric" ) {
00059     $data = "ab12 ";
00060     $size_divider = strlen($data);
00061     $add_value = strlen($data);
00062   } else if ( $fill_type == "numeric" ) {
00063     $data = 2;
00064     $size_divider = 1;
00065     $add_value = 0;
00066   } else {
00067     // invalid fill type;
00068     return false;
00069   }
00070 
00071   $tmp_buff = str_repeat($data, ($fill_size/$size_divider) + $add_value );
00072 
00073   if ( strlen($tmp_buff) > $fill_size ) {
00074     $buffer = substr($tmp_buff, 0, $fill_size);
00075   } else {
00076     $buffer = $tmp_buff;
00077   }
00078 
00079   return true;
00080 }
00081 
00082 /*
00083  Function : bool fill_file(resource $file_handle, string $fill_type, string $file_size);
00084  Description: Fills the file with data as specified with requested size.
00085    $file_handle = file handle, opened with write options,
00086    $fill_type: 
00087        "text" = fills with string of size $file_size
00088        "numeric" = fills with numeric value of size $file_size
00089        "empty" = no fill operation performed, returns true
00090        "text_with_new_line" = similar to "text" fill type but writes with new line
00091        "alphanumeric" = fills with alphnumeric values 
00092  Returns: true on success, false on failure & invalid fill type
00093 */
00094 
00095 function fill_file($file_handle, $fill_type, $file_size) {
00096    
00097   if ( $fill_type == "empty" ) {
00098     // no fill required, return true
00099     return true;
00100   } if ( $fill_type == "text" ) {
00101     $data = "text ";
00102     $size_divider = strlen($data); 
00103     $add_value = strlen($data);
00104   } else if ( $fill_type == "text_with_new_line" ) {
00105     $data = "line\nline of text\n";
00106     $size_divider = strlen($data);
00107     $add_value = strlen($data);
00108   } else if ( $fill_type == "alphanumeric" ) {
00109     $data = "ab12 ";
00110     $size_divider = strlen($data);
00111     $add_value = strlen($data);
00112   } else if ( $fill_type == "numeric" ) {
00113     $data = 2;
00114     $size_divider = 1;
00115     $add_value = 0;
00116   } else { 
00117     // invalid fill type;
00118     return false;
00119   }
00120 
00121   // write in terms of a chunk of 1 K to avoid memory size overflow
00122   $size = $file_size;
00123   $chunk_size = 1024;
00124   if ( $size > $chunk_size ) {
00125     $loop_count = 1;
00126     do {
00127       $loop_count ++; 
00128       if ( $size <= $chunk_size ) {
00129         $chunk_size = $size;
00130       }
00131       $num_values = str_repeat($data, ($chunk_size/$size_divider) + $add_value );
00132       $bytes_written = fwrite($file_handle, $num_values, $chunk_size);
00133       if ( $bytes_written != $chunk_size ) {
00134             return false;
00135       }
00136       $size -= $chunk_size;
00137     } while ( $size > 0 );
00138   } else {
00139     $num_values = str_repeat($data, ($chunk_size/$size_divider) + $add_value );
00140     $bytes_written = fwrite($file_handle, $num_values, $file_size);
00141     if ( $bytes_written != $file_size ) {
00142       return false;
00143     }
00144   }
00145   
00146   // successful, return true
00147   return true;
00148 }
00149 
00150 /*
00151  Function: int change_file_perms(string $file_path, int $count = 1, int $perms = 0755, 
00152                                  string $name_prefix = "file", 
00153                                  string $name_suffix = 1, $file_extension = ".tmp");
00154  Description: changes file permission for given file(s).
00155    $file_path = dir path where file exists
00156    $count = no. of files, default is 1
00157    $perms = new permission of the file, similar to $mode args of chmod() call
00158    $name_prefix = common name prefix, default is "file"
00159    $name_suffix = suffix to end the common name given in name_prefix to create
00160       a unique name. default is 1. 
00161    $file_extension = default is .tmp
00162  Returns:
00163    Integer, Count of total files permission changed.
00164 */
00165 function change_file_perms($file_path,
00166                            $count = 1, 
00167                            $perms = 0755, 
00168                            $name_prefix = "file", 
00169                            $name_suffix = 1, 
00170                            $file_extension = ".tmp" ) 
00171 {
00172   $changed = 0;
00173 
00174   if( $count <= 0 ) 
00175     return $changed;
00176 
00177   if ( $name_suffix <= 0)
00178     $name_suffix = 1;
00179 
00180   for($loop_counter = 1; $loop_counter <= $count; $loop_counter++) {
00181     $filename = $file_path."/".$name_prefix.$name_suffix.$file_extension;
00182     if( chmod($filename, $perms) )
00183       $changed++;
00184     $name_suffix++;
00185   } 
00186   return $changed;
00187 }
00188 
00189 /* 
00190  Function: array create_files( string $file_path,
00191                                int $count = 1,
00192                                string $content_type = "numeric", 
00193                                int $permission = 0755, 
00194                                int $size = 1,
00195                                string $mode = "w", 
00196                                string $name_prefix = "file", 
00197                                int $name_suffix = 1,
00198                                string $flag = "kilobytes"
00199                                string $file_extension = ".tmp"
00200                              );
00201  Description: Creates given number of files with specified mode and 
00202    permissions. File is filled with content of size specified.
00203    $file_path = dir where files will be created
00204    $name_prefix = prefix to be used for names, name is suffix with a 
00205      unqiue numeric value to make the file name unique, default = file
00206    $name_suffix = suffix to be used for the name, default = 1
00207    $count = total no. of files to be created, default = 1
00208    $mode = file open mode as specified in fopen() call. Do not use 
00209      modes used for only reading the file. Default = "w"
00210    $permission = An octal number, This should be similar to $mode 
00211      specified in chmod() call. 
00212    $content_type = Specify type of the content to fill in the file. 
00213      "numeric" = fill file with numeric vlaues
00214      "text" = fill file with regular text
00215      "empty" = empty file
00216      "text_with_new_line" = similar to text fill type, but writes with new line char
00217      "alphanumeric" = fill file with alpha numeric text
00218      If imporper $content type is specified, file is created as empty
00219    $size = size of the fill in terms of kilobyte, i.e size of the file.
00220            if $flag is specified as "byte", then then given size is taken in bytes
00221    $flag = specifiy if size has to be treated as no of total bytes or 
00222            multiple of KB.
00223      "kilobytes" = take size in terms of multiple of KB
00224      "byte" = take size in terms of bytes 
00225    $file_extension = default is .tmp
00226 
00227  Returns:
00228    An array with following key value pair:
00229      created => total file created
00230      filled => total files filled 
00231      perms_changed => total files permission changed
00232 */
00233 function create_files( $file_path,
00234                        $count = 1,
00235                        $content_type = "numeric",
00236                        $permission = 0755,
00237                        $size = 1,
00238                        $mode = "w",
00239                        $name_prefix = "file",
00240                        $name_suffix = 1,
00241                        $flag = "kilobytes",
00242                        $file_extension = ".tmp"
00243                      )
00244 {
00245   $return_value = array('created' => 0, 'filled' => 0, 'perms_changed' => 0);
00246 
00247   //ensure that suffix is a +ve integer
00248   if ($name_suffix <= 0) {
00249     $name_suffix = 1;
00250   }
00251 
00252   // check for proper size
00253   if ( $size == 0 )
00254     return $return_value;
00255 
00256   // prepare the size based on flag 
00257   $file_size = $size;
00258   if ( $flag == "kilobytes" ) {
00259     $file_size = $file_size * 1024;
00260   }
00261 
00262   $tmp_name_suffix = $name_suffix;
00263   // create the files with specified mode and permission
00264   for($file_created_count = 1; $file_created_count <= $count; $file_created_count ++) {
00265     $filename = $file_path."/".$name_prefix.$tmp_name_suffix.$file_extension;
00266 
00267     $status = create_file($filename, $mode);
00268 
00269     $tmp_name_suffix++;
00270 
00271     if ($status == true) {
00272       $return_value['created']++;
00273     }
00274     else {
00275       return $return_value;
00276     }
00277   }
00278 
00279   if ( $content_type == "empty" ) {
00280     $return_value['filled'] = $count;
00281   } else {
00282     // fill the file with specifiec type of data and size
00283     $tmp_name_suffix = $name_suffix;
00284     for($loop_counter = 1; $loop_counter <= $count; $loop_counter ++) {
00285       $filename = $file_path."/".$name_prefix.$tmp_name_suffix.$file_extension;
00286       $file_handle = fopen($filename, $mode);
00287       if($file_handle == false) {
00288         fclose($file_handle);
00289         return $return_value;
00290       } // end of if
00291  
00292       // call fill_file() to fill the file 
00293       if( fill_file($file_handle, $content_type, $file_size) )
00294         $return_value['filled']++;
00295     
00296       fclose($file_handle);
00297 
00298       $tmp_name_suffix++;
00299     } // end of for
00300   }
00301 
00302   // change all file's permissions
00303   $return_value['perms_changed'] = change_file_perms($file_path, $count, $permission, $name_prefix, 
00304                                                      $name_suffix, $file_extension);
00305   
00306   return $return_value;
00307 }
00308 
00309 
00310 /*
00311  Function: function create_links( $file_path,
00312                      $filename,
00313                        $link_count = 1,
00314                        $link_type = "soft",
00315                        $link_size = 1024,
00316                        $link_name_prefix = "link",
00317                        $link_name_suffix = 1,
00318                        $link_file_content = "text",
00319                        $link_perms = 0755,
00320                        $link_file_extension = ".tmp"
00321                      );
00322 
00323  Description: Creates given number of links with specified mode and
00324    permissions.Link is filled with content of size specified.
00325    $file_path = location of the file and where links need to be created
00326    $link_name_prefix = prefix to be used for names, name is suffix with a
00327      unique numeric value to make the file name unique, default = link
00328    $link_name_suffix = suffix to be used for the name, default = 1
00329    $link_count = total no. of links to be created to given file, default = 1
00330    $link_perms = An octal number, This should be similar to $mode
00331      specified in chmod() call.
00332    $link_file_content = Type of the content to fill in the file.
00333      numeric = fill file with numeric vlaues
00334      text = fill file with regular text
00335      text_with_new_line = same as text but new lines are written
00336      alphanumeric = fill with alphanumeric text
00337      If imporper $content type is specified, file is created as empty
00338    $size = size of the fill in terms of kilobyte, i.e size of the file.
00339    $link_type = type of the link to be created 
00340       "soft" = soft link 
00341       "hard" = hard link
00342    $filename = file used to create a link on
00343 
00344  Returns:
00345    An array with following key value pair:
00346      created => total file created
00347      filled => total files filled, always returned as 1
00348      perms_changed => total files permission changed
00349 */
00350 function create_links($file_path,
00351                       $filename,
00352                       $link_count = 1,
00353                       $link_type = "soft",
00354                       $link_size = 1024,
00355                       $link_name_prefix = "link",
00356                       $link_name_suffix = 1,
00357                       $link_file_content = "text",
00358                       $link_perms = 0755,
00359                       $link_file_extension = ".tmp"
00360                      )
00361 {
00362   $return_value = array('created' => 0, 'filled' => 0, 'perms_changed' => 0);
00363   $tmp_name_suffix = $link_name_suffix;
00364   $src_filename = $file_path."/".$filename;
00365   switch( $link_type ) {
00366     default :
00367     case "soft" :  // create a soft link
00368       for($link_created_count = 1; $link_created_count <= $link_count; $link_created_count++) {
00369         $linkname = $file_path."/".$link_name_prefix.$tmp_name_suffix.$link_file_extension;
00370         $status = symlink( $src_filename, $linkname);
00371         $tmp_name_suffix++;
00372         if ($status) {
00373           $return_value['created']++;
00374         }
00375         else {
00376           $return_value;
00377         }
00378       }
00379       break;
00380 
00381     case "hard" :  // create a hard link
00382       for($link_created_count = 1; $link_created_count <= $link_count; $link_created_count++) {
00383         $linkname = $file_path."/".$link_name_prefix.$tmp_name_suffix.$link_file_extension;
00384         $status = link($src_filename, $linkname);
00385         $tmp_name_suffix++;
00386         if ($status) {
00387           $return_value['created']++;
00388         }
00389         else {
00390           $return_value;
00391         }
00392       }
00393       break;
00394   }
00395   
00396   if ( $link_file_content == "empty" ) {
00397     $return_value['filled'] = 1;
00398     return $return_value;
00399   }
00400   
00401   // fill the file with specific type of data and size
00402   $tmp_name_suffix = $link_name_suffix;
00403   $linkname = $file_path."/".$link_name_prefix.$tmp_name_suffix.$link_file_extension;
00404   $file_handle = fopen($linkname, "w");
00405   if($file_handle == false) {
00406     return $return_value;
00407   } // end of if
00408  
00409   // call fill_file() to fill the file 
00410   if( fill_file($file_handle, $link_file_content, $link_size) )
00411     $return_value['filled']++;
00412 
00413   // close the link
00414   fclose($file_handle);
00415 
00416   // change the permission of the link file, only if hard link.
00417   // this is not applicable to soft links
00418   if( $link_type == "hard" ) { 
00419     $return_value['perms_changed'] = change_file_perms($file_path, 
00420                                                         $link_count, 
00421                                                         $link_perms, 
00422                                                         $link_name_prefix, 
00423                                                         $link_name_suffix,
00424                                                         $link_file_extension );
00425   }
00426 
00427   return $return_value;
00428 }
00429 
00430 /* 
00431  Function: bool delete_file(string $filename);
00432  Description: delete a given file if exists 
00433  Returns: true on success  
00434           false on failure
00435           file_not_found if file doesn't exist
00436 */
00437 function delete_file($filename) {
00438   // check if file exists
00439   if ( file_exists($filename) ) {
00440     if ( unlink($filename) )
00441       return true;
00442     else
00443       return false;
00444   }
00445   return file_not_found;
00446 }
00447 
00448 /*
00449  Function: array delete_files(string $file_path, int $count = 1, string $name_prefix = "file", 
00450                               int name_suffix = 1, $file_extension = ".tmp" );
00451  Description: Deletes given number of files if exists.
00452    $file_path = location of the files
00453    $name_prefix = prefix for the filename, rest of the name is incremental(increment by 1 only)
00454      numeric starting from suffix upto count
00455    $count = number of files to be deleted
00456    $name_suffix = first numeric suffix in the name
00457  Returns: An array with following key/value pair
00458    deleted = Number of files deleted.
00459    notfound = Count of non existing file
00460    failed = Count of failed to delete
00461 */
00462 function delete_files($file_path,
00463                       $count = 1, 
00464                       $name_prefix = "file", 
00465                       $name_suffix = 1, 
00466                       $file_extension = ".tmp") 
00467 {
00468   $return_value = array ('deleted' => 0, 'notfound' => 0, 'failed' => 0);
00469 
00470   if ( $name_suffix < 1 )
00471     $name_suffix = 1; 
00472   for($loop_counter = 1; $loop_counter <= $count; $loop_counter++) {
00473     $filename = $file_path."/".$name_prefix.$name_suffix.$file_extension;
00474     $name_suffix++;
00475     $status = delete_file($filename);
00476     if($status == true) {
00477       $return_value['deleted']++;
00478     } else if($status == file_not_found) {
00479       $return_value['notfound']++;
00480     } else {
00481       $return_value['failed']++;
00482     }
00483     
00484   } // end of for
00485   return $return_value;
00486 }
00487 
00488 /*
00489  Function: array delete_links( $file_path, 
00490                                $link_file_count, 
00491                                $link_name_prefix, 
00492                                $link_name_suffix, 
00493                                $link_file_extension );
00494  Description: Deletes given number of links if exists. Uses delete_files() function
00495    $file_path = location of link files 
00496    $link_file_count = Number of link files 
00497    $link_name_prefix = prefix for the linkname, rest of the name is incremental(increment by 1 only)
00498      numeric starting from $link_name_suffix upto count
00499    $link_name_suffix = first numeric suffix in the name
00500 
00501  Returns: An array with following key/value pair
00502    deleted = Number of links deleted.
00503    notfound = Count of non existing link
00504    failed = Count of failed to delete
00505 */
00506 function delete_links($file_path,
00507                       $link_file_count = 1, 
00508                       $link_name_prefix = "link", 
00509                       $link_name_suffix = 1, 
00510                       $link_file_extension = ".tmp") 
00511 {
00512    // call the delete files to delete links 
00513    $return_value = delete_files( $file_path, 
00514                                  $link_file_count, 
00515                                  $link_name_prefix, 
00516                                  $link_name_suffix, 
00517                                  $link_file_extension );
00518    return $return_value;
00519 }
00520 
00521 
00522 
00523 /*
00524  Prototype:
00525   function compare_self_stat( array $stat );
00526  Description:
00527   Compares the each of the first 13 values of the stat array with the 
00528   corresponding next 13 values of the same stat for equality
00529   $stat = stat array
00530 
00531  Retuns: true when all of them match, false otherwise
00532 */
00533 function compare_self_stat( array $stat )
00534 {
00535   //return value
00536   $return_value = true;
00537 
00538   // named keys present in a stat
00539   $string_keys = array("dev", "ino", "mode", "nlink", "uid", "gid", 
00540                        "rdev", "size", "atime", "mtime", "ctime",
00541                        "blksize", "blocks");
00542 
00543   // first numeric key
00544   $key = 0;
00545 
00546   // compare the values in the stat, which are accessed using numeric key with
00547   // values accessed using string keys
00548   foreach($string_keys as $str_key)
00549   {
00550     if($stat[$key] != $stat[$str_key]) {
00551       echo "Error: stat[$key] doesn't match with stat[$str_key]\n";
00552       $flag = false;
00553       $key++;
00554     }
00555     else {
00556       $key++;
00557     }
00558   } // end of foreach
00559 
00560   // if the $return_value is false, i.e all the element do not match then 
00561   // dump the stat array so that its easy to figure out the error
00562   if ($return_value == false ) {
00563     echo "\n Dumping stat array ...\n";
00564     var_dump($stat);
00565   }
00566 
00567   return $return_value;
00568 }// end of compare_self_stat
00569 
00570 /*
00571 Prototype:
00572   function compare_stats( array $stat1, array $stat2, array $fields, 
00573                           [string $op = "==", [ bool $flag = false] ]);
00574 Description:
00575   Compares two stat values, stat value should be obtained by stat/lstat
00576   $stat1 = first stat array
00577   $stat2 = second stat array
00578   $op = type of the comparision to be perform between elements of stat1 and stat2
00579     "!=" compare for not equal
00580     "==" compare for equality
00581     ">"  if each element of stat1 is > than stat2
00582     "<"  if each element of stat1 is < than stat2
00583   $fields = contains the key of the elements that needs to be compared. 
00584             type of the comparision is based on $op argument value
00585   $flag = specify true to dump the stat1 and stat2 
00586 */
00587 
00588 $all_stat_keys = array(0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 
00589                        "dev", "ino", "mode", "nlink", "uid", "gid",
00590                        "rdev", "size", "atime", "mtime", "ctime",
00591                        "blksize", "blocks");
00592 
00593 function compare_stats($stat1, $stat2, $fields, $op = "==", $flag = false ) {
00594   // dump the stat if requested
00595   if ( $flag == true ) {
00596     var_dump($stat1);
00597     var_dump($stat2);
00598   }
00599 
00600   $result = true;
00601 
00602   // compare values of given key from each stat array 
00603   for($index = 0; $index < count($fields); $index++) 
00604   {
00605     switch( $op )
00606     {
00607        case "==":
00608          if ( $stat1[ $fields[$index] ] != $stat2[ $fields[$index] ] ) {
00609            $result = false;
00610            echo "Error: stat1 do not match with stat2 at key value: $fields[$index]\n";
00611          }
00612          break;
00613 
00614        case "!=":
00615          if ( $stat1[ $fields[$index] ] != $stat2[ $fields[$index] ] ) {
00616            // do nothing as its not equal, else will take care of if equal
00617          } else {
00618            $result = false;
00619            echo "Error: stat1 equals stat2 at key value: $fields[$index]\n";
00620          }
00621          break;
00622 
00623        case ">":
00624          if ( $stat1[ $fields[$index] ] <= $stat2[ $fields[$index] ] ) {
00625            $result = false;
00626            echo "Error: stat1 is not greater than stat2 at key value: $fields[$index]\n";
00627          }
00628          break;
00629 
00630        case "<":
00631          if ( $stat1[ $fields[$index] ] >= $stat2[ $fields[$index] ] ) {
00632            $result = false;
00633            echo "Error: stat1 is not lesser than stat2 at key value: $fields[$index]\n";
00634          }
00635          break;
00636     }
00637   }
00638   // if the result is false(i.e values are not as expected), 
00639   // dump the stat array so that easy to figure out the error
00640   if ( $result == false ) {
00641     echo "\n Dumping stat array 1...\n";
00642     var_dump($stat1);
00643     echo "\n Dumping stat array 2...\n";
00644     var_dump($stat2);
00645   }
00646 
00647   return $result;
00648 }
00649 
00650 ?>