From 2e9ac7d0033abf934b66e23e5b2f8494785a31eb Mon Sep 17 00:00:00 2001 From: Olaf Barthel Date: Wed, 20 Apr 2005 12:06:00 +0000 Subject: [PATCH] - The Unix-like rename() now removes the target file even if that file has been protected from deletion. git-svn-id: file:///Users/olsen/Code/migration-svn-zu-git/logical-line-staging/clib2/trunk@14920 87f5fb63-7c3d-0410-a384-fd976d0f7a62 --- library/changes | 4 ++++ library/stdio_rename.c | 47 +++++++++++++++++++++++++++++++++--------- 2 files changed, 41 insertions(+), 10 deletions(-) diff --git a/library/changes b/library/changes index a316c6f..ef3b9a4 100644 --- a/library/changes +++ b/library/changes @@ -1,3 +1,7 @@ +- The Unix-like rename() now removes the target file even if that + file has been protected from deletion. + + c.lib 1.191 (9.4.2005) - The name of the public record locking semaphore has to be preallocated diff --git a/library/stdio_rename.c b/library/stdio_rename.c index cf71fc7..ef94e7d 100644 --- a/library/stdio_rename.c +++ b/library/stdio_rename.c @@ -1,5 +1,5 @@ /* - * $Id: stdio_rename.c,v 1.4 2005-02-28 10:07:31 obarthel Exp $ + * $Id: stdio_rename.c,v 1.5 2005-04-20 12:06:00 obarthel Exp $ * * :ts=4 * @@ -63,6 +63,8 @@ rename(const char *oldname,const char *newname) if(__check_abort_enabled) __check_abort(); + PROFILE_OFF(); + #if defined(CHECK_FOR_NULL_POINTERS) { if(oldname == NULL || newname == NULL) @@ -96,9 +98,7 @@ rename(const char *oldname,const char *newname) D(("renaming '%s' to '%s'",oldname,newname)); - PROFILE_OFF(); status = Rename((STRPTR)oldname,(STRPTR)newname); - PROFILE_ON(); if(status == DOSFALSE) { @@ -106,13 +106,14 @@ rename(const char *oldname,const char *newname) #if defined(UNIX_PATH_SEMANTICS) { - BOOL renamed; + LONG error; - if(IoErr() != ERROR_OBJECT_EXISTS) + error = IoErr(); + if(error != ERROR_OBJECT_EXISTS) { SHOWMSG("that was some other error"); - __set_errno(__translate_io_error_to_errno(IoErr())); + __set_errno(__translate_io_error_to_errno(error)); goto out; } @@ -120,11 +121,35 @@ rename(const char *oldname,const char *newname) /* ZZZ there should be a safer solution for this */ - PROFILE_OFF(); - renamed = (BOOL)(DeleteFile((STRPTR)newname) && Rename((STRPTR)oldname,(STRPTR)newname)); - PROFILE_ON(); + if(CANNOT DeleteFile((STRPTR)newname)) + { + error = IoErr(); + if(error != ERROR_DELETE_PROTECTED && error != ERROR_OBJECT_NOT_FOUND) + { + SHOWMS("couldn't delete the file"); - if(NOT renamed) + __set_errno(__translate_io_error_to_errno(error)); + goto out; + } + + if(CANNOT SetProtection((STRPTR)newname,0)) + { + SHOWMS("couldn't reset the protection"); + + __set_errno(__translate_io_error_to_errno(IoErr())); + goto out; + } + + if(CANNOT DeleteFile((STRPTR)newname)) + { + SHOWMS("couldn't delete the file again"); + + __set_errno(__translate_io_error_to_errno(IoErr())); + goto out; + } + } + + if(CANNOT Rename((STRPTR)oldname,(STRPTR)newname)) { SHOWMSG("that didn't work"); @@ -144,6 +169,8 @@ rename(const char *oldname,const char *newname) out: + PROFILE_ON(); + RETURN(result); return(result); }