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 IAR程序莫名变量多重定义
  • ¥15 (标签-UDP|关键词-client)
  • ¥15 关于库卡officelite无法与虚拟机通讯的问题
  • ¥15 qgcomp混合物线性模型分析的代码出现错误:Model aliasing occurred
  • ¥100 已有python代码,要求做成可执行程序,程序设计内容不多
  • ¥15 目标检测项目无法读取视频
  • ¥15 GEO datasets中基因芯片数据仅仅提供了normalized signal如何进行差异分析
  • ¥100 求采集电商背景音乐的方法
  • ¥15 数学建模竞赛求指导帮助
  • ¥15 STM32控制MAX7219问题求解答