--- loncom/LONCAPA.pm 2008/11/20 15:19:33 1.27 +++ loncom/LONCAPA.pm 2011/05/14 16:12:53 1.31 @@ -1,7 +1,7 @@ # The LearningOnline Network # Base routines # -# $Id: LONCAPA.pm,v 1.27 2008/11/20 15:19:33 jms Exp $ +# $Id: LONCAPA.pm,v 1.31 2011/05/14 16:12:53 raeburn Exp $ # # Copyright Michigan State University Board of Trustees # @@ -44,6 +44,7 @@ my $loncapa_max_wait_time = 13; use vars qw($match_domain $match_not_domain $match_username $match_not_username $match_courseid $match_not_courseid + $match_community $match_name $match_lonid $match_handle $match_not_handle); @@ -56,32 +57,20 @@ our @EXPORT = qw(&add_get_param &esca our @EXPORT_OK = qw($match_domain $match_not_domain $match_username $match_not_username $match_courseid $match_not_courseid + $match_community $match_name $match_lonid $match_handle $match_not_handle); our %EXPORT_TAGS = ( 'match' =>[qw($match_domain $match_not_domain $match_username $match_not_username $match_courseid $match_not_courseid + $match_community $match_name $match_lonid $match_handle $match_not_handle)],); my %perlvar; -=pod - -=head2 NOTE: - -add_get_param() - -Inputs are a url, and a hash ref of -form name => value pairs -takes care of properly adding the form name elements and values to the -the url doing proper escaping of the values and joining with ? or & as -needed - -=cut - sub add_get_param { my ($url,$form_data) = @_; my $needs_question_mark = ($url !~ /\?/); @@ -114,8 +103,8 @@ sub unescape { return $str; } -$match_domain = $LONCAPA::domain_re = qr{[\w\-.]+}; -$match_not_domain = $LONCAPA::not_domain_re = qr{[^\w\-.]+}; +$match_domain = $LONCAPA::domain_re = qr{[[:alnum:]\-.]+}; +$match_not_domain = $LONCAPA::not_domain_re = qr{[^[:alnum:]\-.]+}; sub clean_domain { my ($domain) = @_; $domain =~ s/$match_not_domain//g; @@ -133,6 +122,7 @@ sub clean_username { $match_courseid = $LONCAPA::courseid_re = qr{\d[\w\-.]+}; +$match_community =$LONCAPA::community_re = qr{0[\w\-.]+}; $match_not_courseid = $LONCAPA::not_courseid_re = qr{[^\w\-.]+}; sub clean_courseid { my ($courseid) = @_; @@ -165,6 +155,47 @@ sub clean_handle { return $handle; } +# +# -- Ensure another process for same filesystem action is not running. +# lond uses for: apachereload; loncron uses for: lciptables +# + +sub try_to_lock { + my ($lockfile)=@_; + my $currentpid; + my $lastpid; + # Do not manipulate lock file as root + if ($>==0) { + return 0; + } + # Try to generate lock file. + # Wait 3 seconds. If same process id is in + # lock file, then assume lock file is stale, and + # go ahead. If process id's fluctuate, try + # for a maximum of 10 times. + for (0..10) { + if (-e $lockfile) { + open(LOCK,"<$lockfile"); + $currentpid=; + close LOCK; + if ($currentpid==$lastpid) { + last; + } + sleep 3; + $lastpid=$currentpid; + } else { + last; + } + if ($_==10) { + return 0; + } + } + open(LOCK,">$lockfile"); + print LOCK $$; + close LOCK; + return 1; +} + # -------------------------------------------- Return path to profile directory sub propath { @@ -175,8 +206,7 @@ sub propath { $subdir =~ s/(.)(.)(.).*/$1\/$2\/$3/; my $proname="$perlvar{'lonUsersDir'}/$udom/$subdir/$uname"; return $proname; -} - +} sub tie_domain_hash { my ($domain,$namespace,$how,$loghead,$logtail) = @_; @@ -385,6 +415,7 @@ BEGIN { __END__ +=pod =head1 NAME @@ -409,12 +440,14 @@ unpack non-word characters into CGI-comp pack CGI-compatible hex codes into actual non-word ASCII character =item add_get_param() + +Append escaped form elements (name=value etc.) to a url. Inputs: url (with or without exit GET from parameters), hash ref of form name => value pairs - Return: url with properly added the form name elements and values to the - the url doing proper escaping of the values and joining with ? or & + Return: url with form name elements and values appended to the + the url, doing proper escaping of the values and joining with ? or & as needed =item clean_handle()