CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 4 of 4
  1. #1
    Join Date
    Mar 2013
    Posts
    2

    Porting python backup script from linux to windows

    Hi I have written a backup program in Python on Linux. It is a large project (for just one programmer, me ) and it is over 30.000 lines of code.

    I am porting this program to windows, (i use win7) and mostly it work quite nice on windows already. But ofcourse windows and linux differ and i would really appreciate some help from people knowing either win-programming and or Python.

    So I have te following questions:

    1. How do I read locked files on windows, like the registry files?
    2. How do i write to locked files on windows, like the registry?
    3. How do i read/write to locked directories on windows?
    4. Which files/directories should never be backed-up or restored? (Stuff like swap-files etc.)
    5. Is there a windows version of os.stat.st_mode?? I know windows use acl's and these are quite complicated. But I only want to read or write permissions on a file or directory. Maybe there is simple way of doing this?
    6. How do I put username/groupname etc. etc. on win-files?

    I know this a lot to ask and I should RTFM, but this stuff is complicated and I want to do it right. So pointers to documentation and stuff I should read are very much appreciated.

    A lot of thanks in advance.

  2. #2
    2kaud's Avatar
    2kaud is offline Super Moderator Power Poster
    Join Date
    Dec 2012
    Location
    England
    Posts
    7,822

    Re: Porting python backup script from linux to windows

    As you haven't had any replies I'll try to answer some of your questions. However, I'm a c++ programmer and don't know the first thing about python so I can only provide some guidance as to what to look for but not how to do it from python.

    1) & 2) You don't. Windows provides special API functions to read/write registry entries. These are the API functions beginning Reg which the programmer uses to open specific registry keys, enum key names, read key data, create keys and write data etc. The registry is effectively a set of files. These are divided into what are known as hives. The system hives are stored under windows\system32\config and user hives are stored as part of the profile for each user (ntuser.dat). This location differs depending upon which Windows OS is being used.

    3) To which locked directories are you referring? You can only read/write to files/directories to which you have the appropriate permissions UNLESS you have the backup right assigned and enabled. Then you can read any non-locked file on the system irrespective of permission.

    5) Under Win32 API, writing file/directory permissions is not simple. Reading is more simple than writing but is still non-trivial as you have to obtain the DACL then the SID(s) and then the ACE(s). Look at GetSecurityInfo WIN32 API as a starter taste. Also GetFileSecurity.

    6) groupnames are not used in windows (they exist but only for compatibility for posix programs). Usernames are associated with what is known as a SID and its the SID that is stored as part of the DACL associated with files/directories. See GetSecurityInfo WIn32 API

    As you rightly say, this stuff is very complicated. I have a shelf load of 1000+ page books detailing how to do these things in c++.

    I hope this has been of some help but as I said, I know nothing about python.
    All advice is offered in good faith only. All my code is tested (unless stated explicitly otherwise) with the latest version of Microsoft Visual Studio (using the supported features of the latest standard) and is offered as examples only - not as production quality. I cannot offer advice regarding any other c/c++ compiler/IDE or incompatibilities with VS. You are ultimately responsible for the effects of your programs and the integrity of the machines they run on. Anything I post, code snippets, advice, etc is licensed as Public Domain https://creativecommons.org/publicdomain/zero/1.0/ and can be used without reference or acknowledgement. Also note that I only provide advice and guidance via the forums - and not via private messages!

    C++23 Compiler: Microsoft VS2022 (17.6.5)

  3. #3
    Join Date
    Mar 2013
    Posts
    2

    Re: Porting python backup script from linux to windows

    Thanks,

    GetFileSecurity is available in python so that looks like the road to go. In python it looks like this:

    #####################
    win32security.GetFileSecurity

    PySECURITY_DESCRIPTOR = GetFileSecurity(filename, info )

    Obtains specified information about the security of a file or directory. The information obtained is constrained by the caller's access rights and privileges.

    Parameters

    filename : string
    The name of the file
    info=OWNER_SECURITY_INFORMATION | GROUP_SECURITY_INFORMATION | DACL_SECURITY_INFORMATION | SACL_SECURITY_INFORMATION : int
    ##############

    So this all I need, or is there more I should know??

    About the registry, do backup programs in windows copy the registry key by key? How do they do that if a backup is to be restored to an empty volume which does not contain a registry to begin with??

    Thanks!

  4. #4
    2kaud's Avatar
    2kaud is offline Super Moderator Power Poster
    Join Date
    Dec 2012
    Location
    England
    Posts
    7,822

    Re: Porting python backup script from linux to windows

    GetFileSecurity is available in python. So this all I need, or is there more I should know??
    GetFileSecurity is your 'starter for 10'. This provides a pointer to the item's security descriptor. This security descriptor holds info re SIDs, DACLs and SACLs (auditing). You then need other functions to extract data from the security descriptor. GetSecurityDescriptorDacl(..) then provides info about the DACL. Once you have the DACL, then you use GetAclInformation(..) and then GetAce(..) to finally get info about each ACE applied to the file - and hence the file permissions. GetSecurityDescriptorOwner(..) gets a pointer to the owner SID and then use LookupAccountSid(..) to get the user name.

    To read files to which you don't have access, you need to enable backup privilege ("SeBackupPrivilege") - if you have the right!. This needs OpenProcessToken(..), LookupPrivilegeValue(..) and AdjustTokenPrivileges(..).


    do backup programs in windows copy the registry key by key
    Backup programs tend to come in two kinds. One that takes a complete copy of the partition at the disk sector level that then can be used from which to restore a complete windows system. The other is to take copies of the individual files to be used to restore files to a working windows system when/as required. If you want to be able to restore a backup to an empty volume you really need a disk sector level copy that is then used with a bootable system repair DVD. Programs like Ghost etc use this method to create a file containing a complete copy of a volume saved on another volume (or DVD). Then in the event of a total disc failure you use the Ghost boot DVD to restore the partition from the previously created file.

    Hope this helps. How you do this in Python though, sorry but I can't help as I said I don't know Phython.
    All advice is offered in good faith only. All my code is tested (unless stated explicitly otherwise) with the latest version of Microsoft Visual Studio (using the supported features of the latest standard) and is offered as examples only - not as production quality. I cannot offer advice regarding any other c/c++ compiler/IDE or incompatibilities with VS. You are ultimately responsible for the effects of your programs and the integrity of the machines they run on. Anything I post, code snippets, advice, etc is licensed as Public Domain https://creativecommons.org/publicdomain/zero/1.0/ and can be used without reference or acknowledgement. Also note that I only provide advice and guidance via the forums - and not via private messages!

    C++23 Compiler: Microsoft VS2022 (17.6.5)

Tags for this Thread

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  





Click Here to Expand Forum to Full Width

Featured