AtomicSoft.com Add power to your website!
Home Products Services Support Free
EasyData/SQL
easy-to-use web interface for SQL databases with countless features
AtomicDesk
help desk, FAQ, knowledge base, live support chat - all in one product
Custom programming
Installation
Documentation
HelpDesk
Libraries
Join our newsletter for information on software updates and new releases. You may unsubscribe at any time.



Click here to download plain text source code.

# subroutine name: syswritedata
# Writes in all the data to $datafile.
# Owerwrites file, if already exists
# If error occurs, makes CGI process exit with error
# Returns: nothing
#
# Global variables used:
#    $no_flock - disable flock or not
sub syswritedata {
    my($datafile, $content) = @_;
    # If OS (like Win95) desn't support flock, we have to
    # go unreliable and slow way and create "lock" file
    GetFileLock("$datafile.lock");
    my $existed = -e $datafile;
    use Fcntl qw( O_RDWR O_WRONLY O_CREAT O_TRUNC );
    local *DATA;
    if (sysopen(DATA, $datafile, &O_WRONLY | &O_CREAT)) {
        # Exclusive locking
        flock(DATA, 2) unless ($no_flock);
        truncate(DATA, 0) or die "Can't truncate $datafile. Reason: $!";
        print DATA $content;
        # unlock file
        flock(DATA, 8) unless ($no_flock);
        close(DATA);
        chmod(0666, $datafile) unless ($existed);
        ReleaseFileLock("$datafile.lock");
    } else {
        ReleaseFileLock("$datafile.lock");
        die("Error in subroutine syswritedata: Can't open $datafile. Reason: $!");
    }
}
 
Copyright © 1999-2007 Atomicsoft Ltd. All Rights Reserved.