Back to index

php5  5.3.10
Enumerations | Functions | Variables
file.inc File Reference

Go to the source code of this file.

Enumerations

enum  file_not_found

Functions

 create_file ($filename, $mode="w")
 fill_buffer (&$buffer, $fill_type, $fill_size)
 fill_file ($file_handle, $fill_type, $file_size)
 change_file_perms ($file_path, $count=1, $perms=0755, $name_prefix="file", $name_suffix=1, $file_extension=".tmp")
 create_files ($file_path, $count=1, $content_type="numeric", $permission=0755, $size=1, $mode="w", $name_prefix="file", $name_suffix=1, $flag="kilobytes", $file_extension=".tmp")
 create_links ($file_path, $filename, $link_count=1, $link_type="soft", $link_size=1024, $link_name_prefix="link", $link_name_suffix=1, $link_file_content="text", $link_perms=0755, $link_file_extension=".tmp")
 delete_file ($filename)
 delete_files ($file_path, $count=1, $name_prefix="file", $name_suffix=1, $file_extension=".tmp")
 delete_links ($file_path, $link_file_count=1, $link_name_prefix="link", $link_name_suffix=1, $link_file_extension=".tmp")
 compare_self_stat (array $stat)
 compare_stats ($stat1, $stat2, $fields, $op="==", $flag=false)

Variables

 $all_stat_keys

Enumeration Type Documentation

Definition at line 18 of file file.inc.


Function Documentation

change_file_perms ( file_path,
count = 1,
perms = 0755,
name_prefix = "file",
name_suffix = 1,
file_extension = ".tmp" 
)

Definition at line 165 of file file.inc.

{
  $changed = 0;

  if( $count <= 0 ) 
    return $changed;

  if ( $name_suffix <= 0)
    $name_suffix = 1;

  for($loop_counter = 1; $loop_counter <= $count; $loop_counter++) {
    $filename = $file_path."/".$name_prefix.$name_suffix.$file_extension;
    if( chmod($filename, $perms) )
      $changed++;
    $name_suffix++;
  } 
  return $changed;
}

Here is the caller graph for this function:

compare_self_stat ( array $  stat)

Definition at line 533 of file file.inc.

{
  //return value
  $return_value = true;

  // named keys present in a stat
  $string_keys = array("dev", "ino", "mode", "nlink", "uid", "gid", 
                       "rdev", "size", "atime", "mtime", "ctime",
                       "blksize", "blocks");

  // first numeric key
  $key = 0;

  // compare the values in the stat, which are accessed using numeric key with
  // values accessed using string keys
  foreach($string_keys as $str_key)
  {
    if($stat[$key] != $stat[$str_key]) {
      echo "Error: stat[$key] doesn't match with stat[$str_key]\n";
      $flag = false;
      $key++;
    }
    else {
      $key++;
    }
  } // end of foreach

  // if the $return_value is false, i.e all the element do not match then 
  // dump the stat array so that its easy to figure out the error
  if ($return_value == false ) {
    echo "\n Dumping stat array ...\n";
    var_dump($stat);
  }

  return $return_value;
}// end of compare_self_stat
compare_stats ( stat1,
stat2,
fields,
op = "==",
flag = false 
)

Definition at line 593 of file file.inc.

                                                                            {
  // dump the stat if requested
  if ( $flag == true ) {
    var_dump($stat1);
    var_dump($stat2);
  }

  $result = true;

  // compare values of given key from each stat array 
  for($index = 0; $index < count($fields); $index++) 
  {
    switch( $op )
    {
       case "==":
         if ( $stat1[ $fields[$index] ] != $stat2[ $fields[$index] ] ) {
           $result = false;
           echo "Error: stat1 do not match with stat2 at key value: $fields[$index]\n";
         }
         break;

       case "!=":
         if ( $stat1[ $fields[$index] ] != $stat2[ $fields[$index] ] ) {
           // do nothing as its not equal, else will take care of if equal
         } else {
           $result = false;
           echo "Error: stat1 equals stat2 at key value: $fields[$index]\n";
         }
         break;

       case ">":
         if ( $stat1[ $fields[$index] ] <= $stat2[ $fields[$index] ] ) {
           $result = false;
           echo "Error: stat1 is not greater than stat2 at key value: $fields[$index]\n";
         }
         break;

       case "<":
         if ( $stat1[ $fields[$index] ] >= $stat2[ $fields[$index] ] ) {
           $result = false;
           echo "Error: stat1 is not lesser than stat2 at key value: $fields[$index]\n";
         }
         break;
    }
  }
  // if the result is false(i.e values are not as expected), 
  // dump the stat array so that easy to figure out the error
  if ( $result == false ) {
    echo "\n Dumping stat array 1...\n";
    var_dump($stat1);
    echo "\n Dumping stat array 2...\n";
    var_dump($stat2);
  }

  return $result;
}
create_file ( filename,
mode = "w" 
)

Definition at line 29 of file file.inc.

                                             {
  $file_handle = fopen ($filename, $mode);
  if ( $file_handle == false )
    return false;
  fclose($file_handle);
  return true;
}

Here is the caller graph for this function:

create_files ( file_path,
count = 1,
content_type = "numeric",
permission = 0755,
size = 1,
mode = "w",
name_prefix = "file",
name_suffix = 1,
flag = "kilobytes",
file_extension = ".tmp" 
)

Definition at line 233 of file file.inc.

{
  $return_value = array('created' => 0, 'filled' => 0, 'perms_changed' => 0);

  //ensure that suffix is a +ve integer
  if ($name_suffix <= 0) {
    $name_suffix = 1;
  }

  // check for proper size
  if ( $size == 0 )
    return $return_value;

  // prepare the size based on flag 
  $file_size = $size;
  if ( $flag == "kilobytes" ) {
    $file_size = $file_size * 1024;
  }

  $tmp_name_suffix = $name_suffix;
  // create the files with specified mode and permission
  for($file_created_count = 1; $file_created_count <= $count; $file_created_count ++) {
    $filename = $file_path."/".$name_prefix.$tmp_name_suffix.$file_extension;

    $status = create_file($filename, $mode);

    $tmp_name_suffix++;

    if ($status == true) {
      $return_value['created']++;
    }
    else {
      return $return_value;
    }
  }

  if ( $content_type == "empty" ) {
    $return_value['filled'] = $count;
  } else {
    // fill the file with specifiec type of data and size
    $tmp_name_suffix = $name_suffix;
    for($loop_counter = 1; $loop_counter <= $count; $loop_counter ++) {
      $filename = $file_path."/".$name_prefix.$tmp_name_suffix.$file_extension;
      $file_handle = fopen($filename, $mode);
      if($file_handle == false) {
        fclose($file_handle);
        return $return_value;
      } // end of if
 
      // call fill_file() to fill the file 
      if( fill_file($file_handle, $content_type, $file_size) )
        $return_value['filled']++;
    
      fclose($file_handle);

      $tmp_name_suffix++;
    } // end of for
  }

  // change all file's permissions
  $return_value['perms_changed'] = change_file_perms($file_path, $count, $permission, $name_prefix, 
                                                     $name_suffix, $file_extension);
  
  return $return_value;
}

Here is the call graph for this function:

create_links ( file_path,
filename,
link_count = 1,
link_type = "soft",
link_size = 1024,
link_name_prefix = "link",
link_name_suffix = 1,
link_file_content = "text",
link_perms = 0755,
link_file_extension = ".tmp" 
)

Definition at line 350 of file file.inc.

{
  $return_value = array('created' => 0, 'filled' => 0, 'perms_changed' => 0);
  $tmp_name_suffix = $link_name_suffix;
  $src_filename = $file_path."/".$filename;
  switch( $link_type ) {
    default :
    case "soft" :  // create a soft link
      for($link_created_count = 1; $link_created_count <= $link_count; $link_created_count++) {
        $linkname = $file_path."/".$link_name_prefix.$tmp_name_suffix.$link_file_extension;
        $status = symlink( $src_filename, $linkname);
        $tmp_name_suffix++;
        if ($status) {
          $return_value['created']++;
        }
        else {
          $return_value;
        }
      }
      break;

    case "hard" :  // create a hard link
      for($link_created_count = 1; $link_created_count <= $link_count; $link_created_count++) {
        $linkname = $file_path."/".$link_name_prefix.$tmp_name_suffix.$link_file_extension;
        $status = link($src_filename, $linkname);
        $tmp_name_suffix++;
        if ($status) {
          $return_value['created']++;
        }
        else {
          $return_value;
        }
      }
      break;
  }
  
  if ( $link_file_content == "empty" ) {
    $return_value['filled'] = 1;
    return $return_value;
  }
  
  // fill the file with specific type of data and size
  $tmp_name_suffix = $link_name_suffix;
  $linkname = $file_path."/".$link_name_prefix.$tmp_name_suffix.$link_file_extension;
  $file_handle = fopen($linkname, "w");
  if($file_handle == false) {
    return $return_value;
  } // end of if
 
  // call fill_file() to fill the file 
  if( fill_file($file_handle, $link_file_content, $link_size) )
    $return_value['filled']++;

  // close the link
  fclose($file_handle);

  // change the permission of the link file, only if hard link.
  // this is not applicable to soft links
  if( $link_type == "hard" ) { 
    $return_value['perms_changed'] = change_file_perms($file_path, 
                                                        $link_count, 
                                                        $link_perms, 
                                                        $link_name_prefix, 
                                                        $link_name_suffix,
                                                        $link_file_extension );
  }

  return $return_value;
}

Here is the call graph for this function:

delete_file ( filename)

Definition at line 437 of file file.inc.

                                {
  // check if file exists
  if ( file_exists($filename) ) {
    if ( unlink($filename) )
      return true;
    else
      return false;
  }
  return file_not_found;
}

Here is the caller graph for this function:

delete_files ( file_path,
count = 1,
name_prefix = "file",
name_suffix = 1,
file_extension = ".tmp" 
)

Definition at line 462 of file file.inc.

{
  $return_value = array ('deleted' => 0, 'notfound' => 0, 'failed' => 0);

  if ( $name_suffix < 1 )
    $name_suffix = 1; 
  for($loop_counter = 1; $loop_counter <= $count; $loop_counter++) {
    $filename = $file_path."/".$name_prefix.$name_suffix.$file_extension;
    $name_suffix++;
    $status = delete_file($filename);
    if($status == true) {
      $return_value['deleted']++;
    } else if($status == file_not_found) {
      $return_value['notfound']++;
    } else {
      $return_value['failed']++;
    }
    
  } // end of for
  return $return_value;
}

Here is the call graph for this function:

Here is the caller graph for this function:

delete_links ( file_path,
link_file_count = 1,
link_name_prefix = "link",
link_name_suffix = 1,
link_file_extension = ".tmp" 
)

Definition at line 506 of file file.inc.

{
   // call the delete files to delete links 
   $return_value = delete_files( $file_path, 
                                 $link_file_count, 
                                 $link_name_prefix, 
                                 $link_name_suffix, 
                                 $link_file_extension );
   return $return_value;
}

Here is the call graph for this function:

fill_buffer ( &$  buffer,
fill_type,
fill_size 
)

Definition at line 48 of file file.inc.

                                                       {

  if ( $fill_type == "text" ) {
    $data = "text ";
    $size_divider = strlen($data);
    $add_value = strlen($data);
  } else if ( $fill_type == "text_with_new_line" ) {
    $data = "line\nline of text\n";
    $size_divider = strlen($data);
    $add_value = strlen($data);
  } else if ( $fill_type == "alphanumeric" ) {
    $data = "ab12 ";
    $size_divider = strlen($data);
    $add_value = strlen($data);
  } else if ( $fill_type == "numeric" ) {
    $data = 2;
    $size_divider = 1;
    $add_value = 0;
  } else {
    // invalid fill type;
    return false;
  }

  $tmp_buff = str_repeat($data, ($fill_size/$size_divider) + $add_value );

  if ( strlen($tmp_buff) > $fill_size ) {
    $buffer = substr($tmp_buff, 0, $fill_size);
  } else {
    $buffer = $tmp_buff;
  }

  return true;
}

Here is the caller graph for this function:

fill_file ( file_handle,
fill_type,
file_size 
)

Definition at line 95 of file file.inc.

                                                         {
   
  if ( $fill_type == "empty" ) {
    // no fill required, return true
    return true;
  } if ( $fill_type == "text" ) {
    $data = "text ";
    $size_divider = strlen($data); 
    $add_value = strlen($data);
  } else if ( $fill_type == "text_with_new_line" ) {
    $data = "line\nline of text\n";
    $size_divider = strlen($data);
    $add_value = strlen($data);
  } else if ( $fill_type == "alphanumeric" ) {
    $data = "ab12 ";
    $size_divider = strlen($data);
    $add_value = strlen($data);
  } else if ( $fill_type == "numeric" ) {
    $data = 2;
    $size_divider = 1;
    $add_value = 0;
  } else { 
    // invalid fill type;
    return false;
  }

  // write in terms of a chunk of 1 K to avoid memory size overflow
  $size = $file_size;
  $chunk_size = 1024;
  if ( $size > $chunk_size ) {
    $loop_count = 1;
    do {
      $loop_count ++; 
      if ( $size <= $chunk_size ) {
        $chunk_size = $size;
      }
      $num_values = str_repeat($data, ($chunk_size/$size_divider) + $add_value );
      $bytes_written = fwrite($file_handle, $num_values, $chunk_size);
      if ( $bytes_written != $chunk_size ) {
            return false;
      }
      $size -= $chunk_size;
    } while ( $size > 0 );
  } else {
    $num_values = str_repeat($data, ($chunk_size/$size_divider) + $add_value );
    $bytes_written = fwrite($file_handle, $num_values, $file_size);
    if ( $bytes_written != $file_size ) {
      return false;
    }
  }
  
  // successful, return true
  return true;
}

Here is the caller graph for this function:


Variable Documentation

$all_stat_keys
Initial value:
 array(0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 
                       "dev", "ino", "mode", "nlink", "uid", "gid",
                       "rdev", "size", "atime", "mtime", "ctime",
                       "blksize", "blocks")

Definition at line 588 of file file.inc.