How to protect folder to be deleted or renamed?

Everything else that doesn't fall into one of the other PB categories.
User avatar
Kukulkan
Addict
Addict
Posts: 1415
Joined: Mon Jun 06, 2005 2:35 pm
Location: germany
Contact:

How to protect folder to be deleted or renamed?

Post by Kukulkan »

Hi, I wonder how to protect a folder that I created using PB against deletion or renaming? It should be protected as long as my software is running.

But(!) all the content in this folder should stay editable like before. So, only for this particular folder and the content needs to be editable by the current user.

Is it sufficient to create a handle on this (eg ExamineDirectory())?

Any idea? At least for Windows? Cross-Plattform would be better :-)

Thanks,

Kukulkan
User avatar
RSBasic
Moderator
Moderator
Posts: 1228
Joined: Thu Dec 31, 2009 11:05 pm
Location: Gernsbach (Germany)
Contact:

Re: How to protect folder to be deleted or renamed?

Post by RSBasic »

You can create a new hidden file that you open. Your folder can't be deleted or renamed.
Image
Image
User avatar
Kukulkan
Addict
Addict
Posts: 1415
Joined: Mon Jun 06, 2005 2:35 pm
Location: germany
Contact:

Re: How to protect folder to be deleted or renamed?

Post by Kukulkan »

Hi RSBasic,

nice idea for Windows. Thanks :) Now I need a solution for Mac OSX and Linux...

Kukulkan
Dude
Addict
Addict
Posts: 1907
Joined: Mon Feb 16, 2015 2:49 pm

Re: How to protect folder to be deleted or renamed?

Post by Dude »

Here's a Windows-only way to do it:

Code: Select all

; Prevent a folder being deleted or renamed while our app is running.

tmpdir$=GetTemporaryDirectory()+"zzz\"
lockfile$=tmpdir$+"zzz"

CreateDirectory(tmpdir$)
CreateFile(#PB_Any,lockfile$)

lockedfile=OpenFile_(lockfile$,@ofs.OFSTRUCT,#OF_SHARE_DENY_READ)

MessageRequester("Test","Leave this prompt open, and try to delete/rename:"+#LF$+tmpdir$)

CloseHandle_(lockedfile)
User avatar
Keya
Addict
Addict
Posts: 1890
Joined: Thu Jun 04, 2015 7:10 am

Re: How to protect folder to be deleted or renamed?

Post by Keya »

also for Windows if you use FILE_FLAG_BACKUP_SEMANTICS you can directly call CreateFile on a directory, keeping a handle open to it without requiring any files (i dont know if you need special user permissions for that, apparently SE_BACKUP_NAME privilege using AdjustTokenPrivileges if youre not Admin)

Code: Select all

sPath.s = "c:\temp\deltest"
CreateDirectory(sPath)  ;create in case it doesnt exist
hDir = CreateFile_(sPath, #GENERIC_WRITE, #FILE_SHARE_WRITE, 0, #OPEN_EXISTING, #FILE_FLAG_BACKUP_SEMANTICS, #Null)
If hDir
  MessageRequester("Test", "Handle now open to " + sPath + " (shouldnt be able to delete)")
Else
  MessageRequester("Error", "Couldnt open " + sPath)
EndIf
;CloseHandle_(hDir)  ;can delete it after this
A similar but i guess less flexible approach is to use SetCurrentDirectory which also seems to keep a handle open

I cant test Linux/OSX at the moment but can you delete a directory if a process has an open handle to a file within it?
or if you have a little Sleep.app binary running from a directory, can you delete the directory?
Dude
Addict
Addict
Posts: 1907
Joined: Mon Feb 16, 2015 2:49 pm

Re: How to protect folder to be deleted or renamed?

Post by Dude »

Keya, that's awesome! :)

[Edit 1] Doesn't work with root folders though (C:, D:, etc). Only child folders.

[Edit 2] Needs admin rights for folders such as "C:\Program Files\Internet Explorer\".
Post Reply