dounangqie4819 2018-11-29 07:40
浏览 86
已采纳

了解文件访问代码段的“为什么”

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

  • 写回答

1条回答 默认 最新

  • duancan1950 2018-11-29 09:29
    关注

    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://docs.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
    }
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 树莓派与pix飞控通信
  • ¥15 自动转发微信群信息到另外一个微信群
  • ¥15 outlook无法配置成功
  • ¥30 这是哪个作者做的宝宝起名网站
  • ¥60 版本过低apk如何修改可以兼容新的安卓系统
  • ¥25 由IPR导致的DRIVER_POWER_STATE_FAILURE蓝屏
  • ¥50 有数据,怎么建立模型求影响全要素生产率的因素
  • ¥50 有数据,怎么用matlab求全要素生产率
  • ¥15 TI的insta-spin例程
  • ¥15 完成下列问题完成下列问题