Index: openacs-4/packages/acs-content-repository/tcl/acs-content-repository-procs.tcl =================================================================== RCS file: /usr/local/cvsroot/openacs-4/packages/acs-content-repository/tcl/acs-content-repository-procs.tcl,v diff -u -r1.7 -r1.8 --- openacs-4/packages/acs-content-repository/tcl/acs-content-repository-procs.tcl 30 Mar 2013 19:07:23 -0000 1.7 +++ openacs-4/packages/acs-content-repository/tcl/acs-content-repository-procs.tcl 27 Oct 2014 16:39:09 -0000 1.8 @@ -24,18 +24,26 @@ future revision. Right now go with short and sweet, count on scheduling to prevent conflicts } { - db_transaction { - # subselect makes sure there isn't a parent revision still lying around - db_foreach fetch_paths { *SQL* } { - - # try to remove file from filesystem - set file [cr_fs_path $storage_area_key]/$path - ns_log Debug "cr_delete_scheduled_files: deleting $file" - file delete $file - } - # now that all scheduled files deleted, clear table - db_dml delete_files { *SQL* } + db_transaction { + # subselect makes sure there isn't a parent revision still lying around + db_foreach fetch_paths { *SQL* } { + set file [cr_fs_path $storage_area_key]/$path + if {[regexp {^[0-9/]+$} $path]} { + # the filename looks valid, delete the file from filesystem + ns_log Debug "cr_delete_scheduled_files: deleting $file" + file delete $file + } else { + ns_log Warning "cr_delete_scheduled_files: refuse to delete $file" + } + } + # now that all scheduled files deleted, clear table + db_dml delete_files { *SQL* } } + + # + # cleanup orphaned files (leftovers from aborted transactions) + # + cr_cleanup_orphaned_files } @@ -67,3 +75,58 @@ } } } + +## +## Check for orphans in the content respository directory, and delete +## such files if required. +## +## gustaf.neumann@wu-wien.ac.at +## + + +ad_proc cr_check_orphaned_files {-delete:boolean {-mtime ""}} { + + Check for orphaned files in the content respository directory, and + delete such files if required. Orphaned files might be created, + when files are added to the content repository, but the transaction + is being aborted. This function is intended to be used for one-time + maintainenace operations. Starting with 5.8.1, OpenACS contains + support for handling orphaned files much more efficiently via a + transaction log that is checked via cr_cleanup_orphaned_files in + cr_delete_scheduled_files. + + @param -delete delete the orphaned files + @param -mtime same semantics as mtime in the file command + +} { + set cr_root [nsv_get CR_LOCATIONS CR_FILES] + set root_length [string length $cr_root] + set result "" + + set cmd [list exec find $cr_root/ -type f] + if {$mtime ne ""} {lappend cmd -mtime $mtime} + foreach f [split [{*}$cmd] \n] { + set name [string range $f $root_length end] + if {![regexp {^[0-9/]+$} $name]} continue + + # For every file in the content respository directory, check if this + # file is still referenced from the content-revisions. + + set x [cr_count_file_entries $name] + if {$x > 0} continue + + lappend result $f + if {$delete_p} { + file delete $f + } + } + + return $result +} + +# +# Local variables: +# mode: tcl +# tcl-indent-level: 4 +# indent-tabs-mode: nil +# End: