Monks ~
My CGI script is going to accept uploaded files and then put them somewhere. I don't want to overload one directory, so I'm using a three-level structure: "a/aa/aaa" to "z/zz/zzz". This way no directory has over 26 sub directories, no directory has more than $MAX (constant) files, and there are 26^3 possible directories.
my( $p1, $p2, $p3 ); # path parts, each one character, from database
my( $count ); # number of directory items, from database
if( $count > $DIR_MAX_ENTRIES ) {
if( $p3 eq 'z' ) {
$p3 = 'a';
if( $p2 eq 'z' ) {
$p2 = 'a';
$p1 = chr( ord( $p1) + 1 );
} else {
$p2 = chr( ord( $p2) + 1 );
}
} else {
$p3 = chr( ord( $p3) + 1 );
}
}
It works as expected, with two
caveats:
- If the first character passes 'z' it barfs. This is fine: if we ever approach 'z' then the system will need redesign for other reasons.
- '$count' may be susceptible to a race condition, since it's a stored number rather than an actual count of directory entries. This is fine: it's not meant to be high-precision, and $MAX will be low enough that some slop is ok.
Finally, though, I wonder if there's a faster or simpler way to do this. I'd thought about using an array of characters (rather than 'ord()' and 'chr()') and incrementing the subscript. This wouldn't eliminate the 'if eq z' check, though, just change its form and move it a bit.
Any advice from the monks? Thanks!
--
man with no legs, inc.