Tech Note: Safely Going Live with New Code

Charles Johnsonfollow me on twitter
Mon May 12, 2008 at 4:20 pm PDT • Views: 249

Tonight's geeked out post has to do with the difficulty of safely uploading changed web application files. Pretty sexy, huh?

When I make changes to one of the PHP scripts that drive the LGF Blog Engine, the new file has to be uploaded to the LGF web server, of course. The problem comes if someone happens to browse to that file while the upload process is taking place; this can lead to the browser being served corrupted data. The user may see a partial page load, an error, or just a blank screen in that case, and we all know how painful that can be.

The solution to this problem is known as a "file swap." You upload the new file with a different temporary name, then rename the new file to the original name. This works because the Linux 'mv' command (which is used to rename files) does not actually rewrite any data; it simply changes the filename-to-inode mapping, so that the filename points to the new data. If the old file data is being read in the middle of this operation, it's not a problem because the data continues to exist in its old location (for a while); the operating system doesn't reallocate the inode and overwrite that data until all open file handles to it are closed.

In those long-ago days of yesteryear when the LGF Blog Engine was based on a flat file system, I used this swapping technique for almost everything, and solved some serious problems with race conditions that would occasionally wipe out files.

To finish off this short exercise in geekitude, here's a bash shell script I use to automatically rename any recently uploaded temporary files.

(Note: when I upload a temporary file, I use the naming convention 'filename.tmp.php' so that the file is still a valid PHP file; this way if someone just happens to browse to it before it's renamed, they won't see a page full of PHP code. That would be annoying to them and a possible security problem for LGF.)

#!/bin/bash

# Rename LGF temp files to live versions

cd /path/to/weblog/folder/

if stat -t *.tmp.php >/dev/null 2>&1; then
    for tmp in *.tmp.php; do
        mv -i $tmp ${tmp/.tmp/}
    done
fi
Advertisement

131 comments

^ back to top ^

Name:

Pass:

Register Forgot Your Password? Re-send Confirmation (To log in, cookies must be enabled in your browser!)

Turn off ads by subscribing!
For about 33 cents a day, our subscription option turns off all advertisements at LGF!
Read more...


► LGF Headlines

  • Loading...

► Tweeted Articles

  • Loading...

► Tweeted Pages

  • Loading...

► Top 10 Comments

  • Loading...

► Bottom Comments

  • Loading...

► Recent Comments

  • Loading...

► Tools/Info

► Tag Cloud

► Contact

You must have Javascript enabled to use the contact form.
Your email:

Subject:

Message:


Messages may be published in our weblog, unless you request otherwise.
Tech Note:
Using the Contact Form

More Partners

Compare Electricity Prices in your area. Texas Electricity is deregulated; you have the right to choose Texas Electric Rates from among many Texas Electric Companies.

Vicious killer monkey thuggery.

TwitterFacebook
LGF Pages
Recent Pages

researchok
'I Was Looking Forward to a Quiet Old Age': Instead, Etta Shiber, Helped Smuggle Stranded Allied Soldiers To Freedom
4 hours, 50 minutes ago
Views: 64 • Comments: 0
Tweets: 1 • Rating: 0

Daniel Ballard
Late Afternoon Light-Kalanchoe
12 hours, 31 minutes ago
Views: 100 • Comments: 0
Tweets: 0 • Rating: 4

MikeySDCA
Colin Powell Endorsed Same-Sex Marriage Once It Was Safe, More Evidence He's Hardly a Great Leader.
12 hours, 35 minutes ago
Views: 132 • Comments: 1
Tweets: 0 • Rating: 1

Eclectic Infidel
City College of San Francisco Budget Update
13 hours, 23 minutes ago
Views: 120 • Comments: 0
Tweets: 0 • Rating: 0

Michael McBacon
Kansas governor signs 'Shariah bill' to ban Islamic law
17 hours, 54 minutes ago
Views: 231 • Comments: 6
Tweets: 0 • Rating: 5

Aigle
National Geographic Traveler Veers Off Track
1 day, 18 hours ago
Views: 452 • Comments: 7
Tweets: 0 • Rating: -5

MichaelJ
Apple TV Slated to Debut in December?
1 day, 19 hours ago
Views: 227 • Comments: 0
Tweets: 0 • Rating: 1

Ascher
Israeli Who Saved Turk on Everest: You Never Abandon a Friend - Israel News, Ynetnews
1 day, 20 hours ago
Views: 299 • Comments: 1
Tweets: 0 • Rating: 3

Haywood Jabloeme
The Harrassment of Patterico & Its Roots in Left-Wing Activism
1 day, 20 hours ago
Views: 521 • Comments: 2
Tweets: 0 • Rating: 4

Curt
Brian Banks: (Video) Falsely accused of rape speaks out
1 day, 22 hours ago
Views: 274 • Comments: 2
Tweets: 0 • Rating: 5

 Frank says:

You can't write a chord ugly enough to say what you want sometimes, so you have to rely on a giraffe filled with whipped cream. -- On a postcard from Rykodisc