| 236 | # works out the temporary directory, including in the case where Greenstone is not writable |
| 237 | # In that case, gs3-setup.bat would already have set the GS_TMP_OUTPUT_DIR temp variable |
| 238 | sub determine_tmp_dir |
| 239 | { |
| 240 | my $try_collect_dir = shift(@_) || 0; |
| 241 | |
| 242 | my $tmp_dirname; |
| 243 | if(defined $ENV{'GS_TMP_OUTPUT_DIR'}) { |
| 244 | $tmp_dirname = $ENV{'GS_TMP_OUTPUT_DIR'}; |
| 245 | } elsif($try_collect_dir && defined $ENV{'GSDLCOLLECTDIR'}) { |
| 246 | $tmp_dirname = $ENV{'GSDLCOLLECTDIR'}; |
| 247 | } elsif(defined $ENV{'GSDLHOME'}) { |
| 248 | $tmp_dirname = $ENV{'GSDLHOME'}; |
| 249 | } else { |
| 250 | return undef; |
| 251 | } |
| 252 | |
| 253 | if(!defined $ENV{'GS_TMP_OUTPUT_DIR'}) { |
| 254 | # test the tmp_dirname folder is writable, by trying to write out a file |
| 255 | # Unfortunately, cound not get if(-w $dirname) to work on directories on Windows |
| 256 | ## http://alvinalexander.com/blog/post/perl/perl-file-test-operators-reference-cheat-sheet (test file/dir writable) |
| 257 | ## http://www.makelinux.net/alp/083 (real and effective user IDs) |
| 258 | |
| 259 | my $tmp_test_file = &FileUtils::filenameConcatenate($tmp_dirname, "writability_test.tmp"); |
| 260 | if (open (FOUT, $tmp_test_file)) { |
| 261 | close(FOUT); |
| 262 | &FileUtils::removeFiles($tmp_test_file); |
| 263 | } else { # location not writable, use TMP location |
| 264 | $tmp_dirname = &FileUtils::filenameConcatenate($ENV{'TMP'}, "greenstone"); |
| 265 | $ENV{'GS_TMP_OUTPUT_DIR'} = $tmp_dirname; # store for next time |
| 266 | } |
| 267 | } |
| 268 | |
| 269 | $tmp_dirname = &FileUtils::filenameConcatenate($tmp_dirname, "tmp"); |
| 270 | &FileUtils::makeAllDirectories ($tmp_dirname) unless -e $tmp_dirname; |
| 271 | |
| 272 | return $tmp_dirname; |
| 273 | } |
| 274 | |