NOTE: this patch MUST be applied to the filemanager source
or acidfree will not work correctly.  To apply, do something like this:

cd modules/filemanager
patch -p0 < path/to/this/patch/filemanager-acidfree.patch


--- filemanager.module.orig	2005-04-16 13:08:15.000000000 -0700
+++ filemanager.module	2005-09-15 09:43:45.000000000 -0700
@@ -31,8 +31,6 @@
  *   a string containing the url to the given path.
  */
 function filemanager_url($file, $working = FALSE, $absolute = FALSE) {
-  global $base_url;
-
   // Load file info if we only got a $fid
   $file = filemanager_get_file_info($file);
 
@@ -42,7 +40,7 @@
     return url('filemanager/' . $subdir, 'fid=' . $file->fid, NULL, $absolute);
   }
   else {
-    return variable_get('filemanager_public_url', $base_url . '/' . variable_get('filemanager_public_path','files')) . "/$subdir/$file->directory/$file->filename";
+    return url(variable_get('filemanager_public_path', 'files') . "/$subdir/{$file->directory}/{$file->filename}", NULL, NULL, $absolute);
   }
 }
 
@@ -144,9 +142,10 @@
  */
 function filemanager_add_file($area, $path, $filename, $mimetype = 'application/unknown', $remove = TRUE, $private = FALSE, $file = FALSE) {
 
-  $lock_file = variable_get('filemanager_private_path', 'private') . '/' . 'filemanager.lck';
   $size = filesize($path);
 
+  // Lock on a lock file to prevent naming conflict race conditions
+  $lock = _filemanager_lock();
   if ($file === FALSE) {
 
     if ($area == '') {
@@ -162,27 +161,7 @@
     $file->private = $private;
     $file->size = $size;
 
-    // Find a directory that is not already full and does not contain our files
-    $file->directory = 0;
-    $directories = db_query("SELECT directory, count(1) filecount FROM {file} WHERE private = '%s' GROUP BY directory ORDER BY directory ASC", $private);
-    // Lock on a lock file to prevent naming conflict race conditions
-    $flk = fopen($lock_file,'w+');
-    flock($flk, LOCK_EX);
-    while ($directory = db_fetch_object($directories)) {
-       // If the next directory in the list skips the current directory
-       // number use it or if the current directory has less than the file
-       // limit in it.
-      if ($directory->directory > $curdir || $directory->filecount < variable_get('filemanager_max_file_count', '2000')) {
-        // If the directory is ok now lets make sure we don't already
-        // have this filename in the directory.
-        if (!file_exists(filemanager_create_path($file, FALSE)) && !file_exists(filemanager_create_path($file, TRUE))) {
-          break;
-        }
-      }
-      $file->directory++;
-    }
-    flock($flk, LOCK_UN);
-    fclose($flk);
+    $file = _filemanager_find_directory($file);
     $update = false;    
   }
   else {
@@ -207,6 +186,7 @@
   $sizelimit = variable_get('filemanager_working_sizelimit', '10') * 1024 * 1024;
   if ($sizelimit > -1 && $size + filemanager_get_working_size() > $sizelimit) {
     drupal_set_message(t('Filestore add failed: Working space limit has been reached.'), 'error');
+    _filemanager_unlock($lock);
     return FALSE;
   }
 
@@ -233,12 +213,72 @@
     if ($remove) {
       file_delete($orig_path);
     }
+  } else {
+    $file = FALSE;
+  }
 
-    return $file;
+  _filemanager_unlock($lock);
+  return $file;
+}
 
-  }
+/**
+ * Renames an existing file in the filestore
+ * @param $file
+ *   file or fid to rename
+ * @param $name
+ *   new name for the file
+ * @return
+ *   the renamed file object on success or false on failure
+ */
+function filemanager_rename($file, $name) {
+    $file = filemanager_get_file_info($file);
+    $oldworking = filemanager_create_path($file, true);
+    $oldactive = filemanager_create_path($file, false);
+    $lock = _filemanager_lock();
+    $file->filename = $name;
+    $file = _filemanager_find_directory($file);
+    if (file_exists($oldworking)) {
+        if (!file_move($oldworking, filemanager_create_path($file, true), FILE_EXISTS_ERROR)) {
+            drupal_set_message("file exists: {$file->filename}", 'error');
+            $file = false;
+        }
+    }
+    if ($file != false && file_exists($oldactive)) {
+        if (!file_move($oldactive, filemanager_create_path($file, false), FILE_EXISTS_ERROR)) {
+            drupal_set_message("file exists: {$file->filename}", 'error');
+            $file = false;
+        }
+    }
+    if ($file != false) {
+        db_query("UPDATE {file} SET filename='%s' WHERE fid=%d", $file->filename, $file->fid);
+    }
+    _filemanager_unlock($lock);
+    return $file;
+}
 
-  return FALSE;
+/**
+ * Gets a working copy of the active version of a file in the filestore
+ * @param $file
+ *   file or fid to copy into working stage area
+ * @param $overwrite
+ *   if true, create a new copy, overwriting an existing working copy
+ *   otherwise, only return the new copy if an existing working copy doesn't exist
+ * @return
+ *   a file object on success or false on failure
+ */
+function filemanager_get_working_copy($file, $overwrite=false) {
+    $file = filemanager_get_file_info($file);
+    if ($file->working && !$overwrite) {
+        return $file;
+    }
+    // FIXME: check for file size bounds
+    if (file_copy(filemanager_create_path($file, false), filemanager_create_path($file, true), FILE_EXISTS_REPLACE)) {
+        $file->working = 1;
+        db_query("UPDATE {file} SET working=%d WHERE fid=%d", $file->working, $file->fid);
+    } else {
+        $file = false;
+    }
+    return $file;
 }
 
 /**
@@ -586,4 +626,40 @@
   }
 }
 
+function _filemanager_lock() {
+    $lock_file = variable_get('filemanager_private_path', 'private') . '/' . 'filemanager.lck';
+    $flk = fopen($lock_file,'w+');
+    flock($flk, LOCK_EX);
+    return $flk;
+}
+
+function _filemanager_unlock(&$handle) {
+    flock($handle, LOCK_UN);
+    fclose($handle);
+}
+
+/**
+ * this must be called while the lock is held
+ * @param $file
+ */
+function _filemanager_find_directory(&$file) {
+    // Find a directory that is not already full and does not contain our files
+    $file->directory = 0;
+    $directories = db_query("SELECT directory, count(1) filecount FROM {file} WHERE private = '%s' GROUP BY directory ORDER BY directory ASC", $private);
+
+    while ($directory = db_fetch_object($directories)) {
+       // If the next directory in the list skips the current directory
+       // number use it or if the current directory has less than the file
+       // limit in it.
+      if ($directory->filecount < variable_get('filemanager_max_file_count', '2000')) {
+        // If the directory is ok now lets make sure we don't already
+        // have this filename in the directory.
+        if (!file_exists(filemanager_create_path($file, FALSE)) && !file_exists(filemanager_create_path($file, TRUE))) {
+          break;
+        }
+      }
+      $file->directory++;
+    }
+    return $file;
+}
 ?>
