Making directories executable and files read and writeable

I had a problem with my rsync backups. The problem was that the first time I ran it everything worked fine. The second time it ran (and all subsequent times) I got back the phone book of error messages, because the first time I’d run rsync it had copied in a whole heap of read-only files, and then when I ran it again it wasn’t able to overwrite those read-only files. At least I think that was what was happening. So I added the following to my backup script:

  find . -type d -exec chmod u+x {} \;
  if [ "$?" -ne "0" ]; then
    echo "Cannot chmod directories in '$PWD'.";
    exit 1;
  fi
  find . -type f -exec chmod u+rw {} \;
  if [ "$?" -ne "0" ]; then
    echo "Cannot chmod files in '$PWD'.";
    exit 1;
  fi

This code runs after rsync and processes the files and directories that have been synchronised. That is, it processes the copy of the data, not the data I copied from.

For the copy of the data I want to make sure that the owner of the files can read and write them and that the owner of the directories can execute them. So that’s what the above code does.

Using PHP output buffering to read a file

There’s a function in PHP called readfile which will read a file and send its contents to stdout. That can be handy, but it’s not much good to you if you want to read the content of the file into a string.

There’s a neat trick using PHP’s output buffering that enables you to read the content of a file into a string without printing anything on stdout, and it goes like this:

  // start an output buffer
  ob_start();
  // print the file to the output buffer
  readfile( $file_path );
  // get the contents of the output buffer
  $content = ob_get_contents();
  // cancel the output buffer
  ob_end_clean();

Reading binary files in C++

I learned how to read binary files in C++ today. My function (which creates an MD5 hash of a file) ended up looking a little different to the example that I learned from:

  const int BUFFER_SIZE = 1024;

  int length;
  char buffer[ BUFFER_SIZE ];

  MD5_CTX ctx;
  MD5Init( &ctx );

  ifstream is( path.c_str(), ios::binary );

  while ( is.good() ) {

    is.read( (char *)buffer, BUFFER_SIZE );

    streamsize count = is.gcount();

    MD5Update( &ctx, (unsigned char *)buffer, count );

  }

  if ( is.eof() ) {

    // it's ok, we're at the end of the file

  }
  else if ( is.bad() ) {

    // bad bit is set
    cout << "Bad bit is set while reading '" << path << "'." << endl;

    cout << strerror( errno ) << endl;

    exit ( 1 );

  }
  else if ( is.fail() ) {

    cout << "Fail bit is set while reading '" << path << "'." << endl;

    cout << strerror( errno ) << endl;

    exit( 1 );

  }

  is.close();