首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >MMAP读写文件

MMAP读写文件
EN

Stack Overflow用户
提问于 2012-09-30 07:08:36
回答 1查看 3.6K关注 0票数 0

我试图使用mmap读取文件,然后对其进行加密,然后将加密写入输出文件。我也试图用mmap来实现这一点,但是当我运行代码时,它告诉我,由于“无效的参数”,它无法取消mmap。

代码语言:javascript
复制
//Open files initialy and obtain a handle to the file.
inputFile = open(inFileName, O_RDONLY, S_IREAD);
outputFile = open(outFileName, O_APPEND | O_CREAT | O_TRUNC | O_WRONLY, S_IWRITE);

//Allocate buffers for encrption.
from = (unsigned char*)malloc(blockSize);
to = (unsigned char*)malloc(blockSize);

mmapWriteBuff = (unsigned char*)malloc(blockSize);
mmapReadBuff = (unsigned char*)malloc(blockSize);

memset(to, 0, blockSize);
memset(from, 0, blockSize);
memset(mmapWriteBuff, 0, blockSize);
memset(mmapReadBuff, 0, blockSize);

//Make sure we have permission to read the file provided.
setFilePermissions(inFileName, PERMISSION_MODE);
setFilePermissions(outFileName, PERMISSION_MODE);

if(encriptParam)
{
    printf("*Encripting file: %s *\n", inFileName);

do//Go through the entire file.
{   
    if(memParam)
    {
            currAmt = lseek(inputFile, blockSize, SEEK_SET);
        mmapReadBuff = mmap(0, blockSize, PROT_READ, MAP_SHARED, inputFile, 0);

        /*
         *This is how you encrypt an input char* buffer "from", of length "len"
         *onto output buffer "to", using key "key".  Jyst pass "iv" and "&n" as
         *shown, and don't forget to actually tell the function to BF_ENCRYPT.
         */

        BF_cfb64_encrypt(mmapReadBuff, mmapWriteBuff, blockSize, &key, iv, &n, BF_ENCRYPT);

        if(currAmt < blockSize)
        {
            writeAmt = lseek(outputFile, currAmt, SEEK_SET);
            mmapWriteBuff = mmap(0, currAmt, PROT_WRITE,  MAP_SHARED, outputFile, 0);                   
            if(errno == EINVAL)
            {
                perror("MMAP failed to start write buffer: ");

                exit(MMAP_IO_ERROR);
            }
        }
        else
        {
            writeAmt = lseek(outputFile, blockSize, SEEK_SET);
            mmapWriteBuff = mmap(0, blockSize, PROT_WRITE, MAP_SHARED, outputFile, 0);                  
            if(errno == EINVAL)
            {
                perror("MMAP failed to start write buffer: ");

                exit(MMAP_IO_ERROR);
            }
        }

        mmapWriteBuff = to;
    }
    else
    {
        currAmt = read(inputFile, from, blockSize);

        /*
         *This is how you encrypt an input char* buffer "from", of length "len"              *onto output buffer "to", using key "key".  Jyst pass "iv" and "n" as
         *shown, and don't forget to actually tell the function to BF_ENCRYT.
        */

        BF_cfb64_encrypt(from, to, blockSize, &key, iv, &n, BF_ENCRYPT);

        if(currAmt < blockSize)
        {
            writeAmt = write(outputFile, to, currAmt);
        }
        else
        {
            writeAmt = write(outputFile, to, blockSize);
        }
    }

    if(memParam)
    {
        //if(currAmt < blockSize)
        //{
        //  if(munmap(mmapWriteBuff, currAmt) == -1)
        //  {
        //      perror("MMAP failed to unmap itself: ");
        //      
        //      exit(MMAP_IO_ERROR);
        //  }
        //  
        //  if(munmap(mmapReadBuff, currAmt) == -1)
        //  {
        //      perror("MMAP failed to unmap itself: ");
        //      
        //      exit(MMAP_IO_ERROR);
        //  }
        //}
        //else
        //{

            if(munmap(mmapReadBuff, blockSize) == -1)
            {
                perror("MMAP Read Buffer failed to unmap itself: ");

                exit(MMAP_IO_ERROR);
            }

            if(munmap(mmapWriteBuff, blockSize) == -1)
            {
                perror("MMAP Write Buffer failed to unmap itself: ");                       
                exit(MMAP_IO_ERROR);
            }


        //}

    }

    memset(to, 0, strlen((char *)to));
    memset(from, 0, strlen((char *)from));
    memset(mmapReadBuff, 0, strlen((char*)mmapReadBuff));
    memset(mmapWriteBuff, 0, strlen((char*)mmapWriteBuff));
    }
    while(currAmt > 0);

    printf("*Saving file: %s *\n", outFileName);

}

EN

回答 1

Stack Overflow用户

发布于 2012-09-30 08:11:32

一般来说,您可能想尝试不使用outputFile标志来设置O_WRONLY文件描述符。仅在O_WRONLY标志中使用mmap()是不够的。

因此,您可能需要更改以下内容:

代码语言:javascript
复制
outputFile = open(outFileName, O_APPEND | O_CREAT | O_TRUNC | O_WRONLY, S_IWRITE);

对此:

代码语言:javascript
复制
outputFile = open(outFileName, O_APPEND | O_CREAT | O_TRUNC | O_RDWR, S_IWRITE);

我不是mmap()的专家,但我知道您可能希望在打开文件描述符时同时给予它读权限和写权限。

(int*)编辑:编辑:您还想尝试将每个mmap()调用转换为,如下所示:

代码语言:javascript
复制
mmapReadBuff = (int*)mmap(0, blockSize, PROT_READ, MAP_SHARED, inputFile, 0);
票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/12659395

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档