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 程序哪有错误怎么改?
  • ¥15 交换机和交换机之间的链路带宽以及主机带宽的理解
  • ¥15 ai创想家对战模式代码
  • ¥15 集合A由3个2行4列二维数组构成,从集合A中任意取一个二维数组元素、如果该二维数组元素的对应列位置的上、下两数都是奇数,而且仅有2个列是奇数/奇数,则该数组有意义,并放入集合B中打印输出。
  • ¥15 电信IPV6 无法外网访问吗
  • ¥15 有偿求效果比较好的遥感影像匹配的c++代码
  • ¥15 博主,你好,我下载了你的智能网联汽车辅助驾驶安全信息检测系统,现在不会运行,可以教我吗,
  • ¥15 怎么在excle输入下列公式
  • ¥15 Arduino,利用modbus的RS485协议,进行对外置的温湿度传感器进行数据读取
  • ¥15 vhdl+MODELSIM