Revision: 45539
          http://brlcad.svn.sourceforge.net/brlcad/?rev=45539&view=rev
Author:   brlcad
Date:     2011-07-19 13:41:06 +0000 (Tue, 19 Jul 2011)

Log Message:
-----------
initial implementation of bu_file_delete() for removing files.  performs a 
simple remove() but then will try harder by relaxing the file permissions on a 
second pass attempt if the first fails.  untested on windows but remove() is 
c90 so we should be able to rely on it.  callers will just have to make sure 
the file isn't opened.

Modified Paths:
--------------
    brlcad/trunk/include/bu.h
    brlcad/trunk/src/libbu/file.c

Modified: brlcad/trunk/include/bu.h
===================================================================
--- brlcad/trunk/include/bu.h   2011-07-19 13:20:33 UTC (rev 45538)
+++ brlcad/trunk/include/bu.h   2011-07-19 13:41:06 UTC (rev 45539)
@@ -2826,6 +2826,13 @@
  */
 BU_EXPORT extern int bu_file_executable(const char *path);
 
+/**
+ * deletes a specified file.  returns truthfully if the specified file
+ * was deleted.
+ */
+BU_EXPORT extern int bu_file_delete(const char *path);
+
+
 /** @file libbu/fnmatch.c
  *
  */

Modified: brlcad/trunk/src/libbu/file.c
===================================================================
--- brlcad/trunk/src/libbu/file.c       2011-07-19 13:20:33 UTC (rev 45538)
+++ brlcad/trunk/src/libbu/file.c       2011-07-19 13:41:06 UTC (rev 45539)
@@ -33,6 +33,7 @@
 #ifdef HAVE_GRP_H
 #  include <grp.h>
 #endif
+
 #include "bio.h"
 
 #include "bu.h"
@@ -227,6 +228,56 @@
 }
 
 
+int
+bu_file_delete(const char *path)
+{
+    int ret = 0;
+    int retry = 0;
+    struct stat sb;
+
+    /* reject empty, special, or non-existent paths */
+    if (!path
+       || BU_STR_EQUAL(path, "")
+       || BU_STR_EQUAL(path, ".")
+       || BU_STR_EQUAL(path, "..")
+       || !bu_file_exists(path))
+    {
+       return 0;
+    }
+
+    do {
+
+       if (retry++) {
+           /* second pass, try to force deletion by changing file
+            * permissions (similar to rm -f).
+            */
+           if (stat(path, &sb) == -1) {
+               break;
+           }
+           chmod(path, (sb.st_mode|S_IRWXU));
+       }
+
+       ret = (remove(path) == 0) ? 0 : 1;
+
+    } while (ret == 0 && retry < 2);
+
+    /* all boils down to whether the file still exists, not whether
+     * remove thinks it succeeded.
+     */
+    if (bu_file_exists(path)) {
+       /* failure */
+       if (retry > 1) {
+           /* restore original file permission */
+           chmod(path, sb.st_mode);
+       }
+       return 0;
+    } else {
+       /* deleted */
+       return 1;
+    }
+}
+
+
 /*
  * Local Variables:
  * mode: C


This was sent by the SourceForge.net collaborative development platform, the 
world's largest Open Source development site.

------------------------------------------------------------------------------
Magic Quadrant for Content-Aware Data Loss Prevention
Research study explores the data loss prevention market. Includes in-depth
analysis on the changes within the DLP market, and the criteria used to
evaluate the strengths and weaknesses of these DLP solutions.
http://www.accelacomm.com/jaw/sfnl/114/51385063/
_______________________________________________
BRL-CAD Source Commits mailing list
[email protected]
https://lists.sourceforge.net/lists/listinfo/brlcad-commits

Reply via email to