10 our @EXPORT = qw(scrypt jailed_file
12 filedb_atomic_append filedb_atomic_edit filedb_atomic_grep
13 valid_email valid_email_multi
14 valid_repo_url valid_web_url);
20 crypt($pwd||'', join ('', ('.', '/', 0..9, 'A'..'Z', 'a'..'z')[rand 64, rand 64]));
26 $Girocco::Config
::chroot."/$filename";
35 use Fcntl
qw(O_WRONLY O_CREAT O_EXCL);
37 my $handle = new IO
::Handle
;
39 unless (sysopen($handle, $path, O_WRONLY
|O_CREAT
|O_EXCL
)) {
41 while (not sysopen($handle, $path, O_WRONLY
|O_CREAT
|O_EXCL
)) {
42 ($! == EEXIST
) or die "$path open failed: $!";
43 ($cnt++ < 16) or die "$path open failed: cannot open lockfile";
47 # XXX: filedb-specific
48 chmod 0664, $path or die "$path g+w failed: $!";
54 my ($path, $noreplace) = @_;
57 rename "$path.lock", $path or die "$path unlock failed: $!";
59 unlink "$path.lock" or die "$path unlock failed: $!";
63 sub filedb_atomic_append
{
64 my ($file, $line) = @_;
67 open my $src, $file or die "$file open for reading failed: $!";
68 my $dst = lock_file
($file);
71 my $aid = (split /:/)[2];
72 $id = $aid + 1 if ($aid >= $id);
74 print $dst $_ or die "$file(l) write failed: $!";
78 print $dst "$line\n" or die "$file(l) write failed: $!";
80 close $dst or die "$file(l) close failed: $!";
88 sub filedb_atomic_edit
{
91 open my $src, $file or die "$file open for reading failed: $!";
92 my $dst = lock_file
($file);
95 print $dst $fn->($_) or die "$file(l) write failed: $!";
98 close $dst or die "$file(l) close failed: $!";
104 sub filedb_atomic_grep
{
105 my ($file, $fn) = @_;
108 open my $src, $file or die "$file open for reading failed: $!";
109 my $dst = lock_file
($file);
112 my $result = $fn->($_);
113 push(@results, $result) if $result;
116 close $dst or die "$file(l) close failed: $!";
119 unlock_file
($file, 1);
125 /^[a-zA-Z0-9+._-]+@[a-zA-Z0-9.-]+$/;
127 sub valid_email_multi
{
129 # More relaxed, we just want to avoid too dangerous characters.
130 /^[a-zA-Z0-9+._, @-]+$/;
134 /^https?:\/\
/[a-zA-Z0-9.:-]+(\/[_\
%a-zA
-Z0
-9.\
/~:?&=;-]*)?(#[a-zA-Z0-9._-]+)?$/;
138 /^(https?|git|svn(\+http)?|svn(\+https)?|darcs|bzr):\/\
/[a-zA-Z0-9.:-]+(\/[_\
%a-zA
-Z0
-9.\
/~-]*)?$/;