Linux教程網
Linux教程網
Linux教程網
您现在的位置: Linux教程網 >> UnixLinux >  >> Linux 程式設計 >> Linux 程式設計

APUE 習題 4.6 解答:實作不寫入空洞位元組的簡易 cp

題目

撰寫一個類似 cp 的程式,複製包含空洞(hole)的檔案,但不要把位元組 0 寫進輸出檔。

思路

所謂「空洞」,是檔案中因為 lseek 跳過而未寫入的區域,讀出來會是一串 0。本解法的做法很直接:以固定大小的緩衝區讀入,把非 0 位元組前移緊湊,只寫出實際有效長度。這麼一來空洞自然被消除,輸出檔會比原檔小。

程式碼

#include <stdio.h>
#include <unistd.h>
#include <fcntl.h>
#include <string.h>
#include <stdlib.h>

#define BUF_SIZ 128

int my_cp(const char *file1, const char *file2)
{
    int fd1, fd2;
    char buffer[BUF_SIZ];
    int res;
    int current_position = 0;
    int byte_count = 0;

    fd1 = open(file1, O_RDWR);
    if (-1 == fd1) {
        perror("open file1 failed");
        return -1;
    }

    fd2 = open(file2, O_RDWR | O_APPEND | O_CREAT | O_TRUNC, 0666);
    if (-1 == fd2) {
        perror("open file2 failed");
        return -1;
    }

    memset(buffer, '\0', BUF_SIZ);
    res = read(fd1, buffer, BUF_SIZ);
    if (-1 == res) {
        perror("file1 read error");
        return -1;
    }

    while (0 != res) {
        byte_count = 0;
        for (current_position = 0; current_position < BUF_SIZ; current_position++) {
            if ('\0' != buffer[current_position]) {
                buffer[byte_count] = buffer[current_position];
                byte_count++;
            }
        }

        res = write(fd2, buffer, byte_count);
        if (-1 == res) {
            perror("file2 write failed");
            return -1;
        }

        memset(buffer, '\0', BUF_SIZ);
        res = read(fd1, buffer, BUF_SIZ);
        if (-1 == res) {
            perror("file1 read failed");
            return -1;
        }
    }
    return 0;
}

int main(int argc, char *argv[])
{
    if (3 != argc) {
        printf("correct usage: ./my_cp file1 file2\n");
        exit(EXIT_FAILURE);
    }

    // 產生一個含空洞的 file1
    int fd = open(argv[1], O_RDWR | O_CREAT | O_TRUNC, 0666);
    if (-1 == fd) {
        perror("open failed");
        exit(EXIT_FAILURE);
    }

    int res = write(fd, "Hey,man!", strlen("Hey,man!"));
    if (-1 == res) {
        perror("file1 write failed");
        exit(EXIT_FAILURE);
    }

    // 跳過 10000 位元組,這段就是空洞
    off_t offset = lseek(fd, 10000, SEEK_SET);
    if (-1 == offset) {
        perror("lseek failed");
        exit(EXIT_FAILURE);
    }

    res = write(fd, "How are you?", strlen("How are you?"));
    if (-1 == res) {
        perror("file1 write failed");
        exit(EXIT_FAILURE);
    }

    res = my_cp(argv[1], argv[2]);
    if (-1 == res) {
        perror("copy failed");
        exit(EXIT_FAILURE);
    }

    exit(EXIT_SUCCESS);
}

執行結果

[www.linuxidc.com@localhost APUE]$ ./my_cp file1 file2
[www.linuxidc.com@localhost APUE]$ cat file2
Hey,man!How are you?

輸出檔 file2 的大小為 20 bytes,正好等於 strlen("Hey,man!") + strlen("How are you?"),一萬多個空洞位元組被完全消除,測試通過。

這份解法的限制

這個實作有兩個已知缺陷,值得在面試或 code review 中主動指出:其一,它會把檔案中「真實存在的 0 位元組」一併刪除,因此只適合文字檔,對二進位檔會造成資料損壞;其二,緩衝區邊界判斷用的是 BUF_SIZ 而非實際讀到的 res,最後一筆短讀取時會多掃描無效區域。更嚴謹的做法是改用 SEEK_DATA/SEEK_HOLE 直接定位資料區段,只複製真實資料。

Copyright © Linux教程網 All Rights Reserved