Search code examples
gowinapifile-permissionslogical-operatorsfile-access

Understanding the "Why" of a file access code snippet


I have been trying to make sense of a code snippet for past some days. You can find the gist here

Overview

The code reads MFT of a Windows drive, creates a struct of maps of files in the MFT. Then it goes on reading the USN Journal to detect what has changed of those files.

Problem

There are some logical operations happening in the script. I can understand what the code part is doing but why is it doing so is what has been haunting me for past couple of days. I stumbled upon various Windows docs like this but even then, it did not make much sense to me.

For example -

switch mode & (O_RDONLY | O_WRONLY | O_RDWR) {
case O_RDONLY:
    access = GENERIC_READ
case O_WRONLY:
    access = GENERIC_WRITE
case O_RDWR:
    access = GENERIC_READ | GENERIC_WRITE
}
if mode&O_CREAT != 0 {
    access |= GENERIC_WRITE
}
if mode&O_APPEND != 0 {
    access &^= GENERIC_WRITE
    access |= FILE_APPEND_DATA
}

Why are we doing these logical operations? There are other instances of such parts in the code also. If anyone can point me to the direction or help me why these operations are done, it'd be really helpful. Thanks


Solution

  • It is a conversion from the Linux (POSIX) API open (man 2 open; http://man7.org/linux/man-pages/man2/open.2.html) to the Windows API CreateFile (https://learn.microsoft.com/en-us/windows/desktop/api/fileapi/nf-fileapi-createfilew).


    For the original code, see src/syscall/syscall_windows.go (https://go.googlesource.com/go):

    func Open(path string, mode int, perm uint32) (fd Handle, err error) {
        if len(path) == 0 {
            return InvalidHandle, ERROR_FILE_NOT_FOUND
        }
        pathp, err := UTF16PtrFromString(path)
        if err != nil {
            return InvalidHandle, err
        }
        var access uint32
        switch mode & (O_RDONLY | O_WRONLY | O_RDWR) {
        case O_RDONLY:
            access = GENERIC_READ
        case O_WRONLY:
            access = GENERIC_WRITE
        case O_RDWR:
            access = GENERIC_READ | GENERIC_WRITE
        }
        if mode&O_CREAT != 0 {
            access |= GENERIC_WRITE
        }
        if mode&O_APPEND != 0 {
            access &^= GENERIC_WRITE
            access |= FILE_APPEND_DATA
        }
        sharemode := uint32(FILE_SHARE_READ | FILE_SHARE_WRITE)
        var sa *SecurityAttributes
        if mode&O_CLOEXEC == 0 {
            sa = makeInheritSa()
        }
        var createmode uint32
        switch {
        case mode&(O_CREAT|O_EXCL) == (O_CREAT | O_EXCL):
            createmode = CREATE_NEW
        case mode&(O_CREAT|O_TRUNC) == (O_CREAT | O_TRUNC):
            createmode = CREATE_ALWAYS
        case mode&O_CREAT == O_CREAT:
            createmode = OPEN_ALWAYS
        case mode&O_TRUNC == O_TRUNC:
            createmode = TRUNCATE_EXISTING
        default:
            createmode = OPEN_EXISTING
        }
        h, e := CreateFile(pathp, access, sharemode, sa, createmode, FILE_ATTRIBUTE_NORMAL, 0)
        return h, e
    }