1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159
| #include <stdio.h> #include <unistd.h> #include <fcntl.h> #include <string.h> #include <stdlib.h> #include <sys/stat.h> #include <sys/errno.h>
ssize_t read_ex(int fd, void *buf, size_t nbyte){ size_t read_remain = nbyte; unsigned char *read_start = (unsigned char*)buf; ssize_t read_num = -1; ssize_t total_num = 0; while(read_remain) { read_num = read(fd, read_start, read_remain); if(-1 == read_num){ return -1; } else if(0 == read_num){ break; } else{ read_remain -= read_num; read_start += read_num; total_num += read_num; } } return total_num; }
ssize_t write_ex(int fd, const void *buf, size_t nbyte){ size_t write_remain = nbyte; unsigned char *write_start = (unsigned char*)buf; ssize_t write_num = -1; ssize_t total_num = 0; while(write_remain) { write_num = write(fd, write_start, write_remain); if(-1 == write_num){ return -1; } else{ write_remain -= write_num; write_start += write_num; total_num += write_num; } } return total_num; } int my_cp(const char *from, const char *to) { int fd1 = -1, fd2 = -1; int rev = -1; unsigned char *buffer = NULL, *buffer_zero = NULL; long pagesize = 0; long long blocks, blksize, size; int read_num, write_num, write_remain, have_holes = 0; struct stat st;
fd1 = open(from, O_RDONLY); if(-1 == fd1){ perror("open file1 faild"); goto err; }
if(fstat(fd1, &st) !=0) { perror("fstat: "); goto err; } else{ #ifdef _SC_PAGESIZE pagesize = sysconf(_SC_PAGESIZE); if (pagesize < 0) { if (errno != 0) { if (errno == EINVAL) { fputs(" (not supported)\n", stdout); pagesize = st.st_blksize; } else { perror("sysconf error"); goto err; } } else { fputs(" (no limit)\n", stdout); pagesize = st.st_blksize; } } printf("pagesize: %ld\n", pagesize); #else pagesize = st.st_blksize; #endif blocks = st.st_blocks; blksize = st.st_blksize; size = st.st_size; printf("st.st_blocks: %lld\n", blocks); printf("st.st_blksize: %lld\n", blksize); printf("st.st_size: %lld\n", size); if(S_ISREG(st.st_mode) && (size / pagesize + (size%pagesize?1:0)) * pagesize > 512 * blocks) { have_holes = 1; printf("%s is a sparse-block file!\n", from); } else{ have_holes = 0; printf("%s is not a sparse-block file!\n", from); } } buffer = malloc(pagesize); buffer_zero = malloc(pagesize); if(buffer == NULL || buffer_zero == NULL) { perror ("malloc fail"); goto err; } memset(buffer, '\0', pagesize); memset(buffer_zero, '\0', pagesize);
fd2 = open(to, O_WRONLY | O_CREAT | O_TRUNC, S_IRUSR|S_IWUSR|S_IRGRP|S_IROTH); if (-1 == fd2) { perror ("open file2 faild"); goto err; }
while((read_num = read_ex(fd1, buffer, pagesize)) > 0) { if(have_holes && !memcmp(buffer_zero, buffer, read_num)){ if(-1 == lseek(fd2, read_num, SEEK_CUR)){ perror("lseek file2 fail"); goto err; } } else{ write_num = write_ex(fd2, buffer, read_num); if (-1 == write_num){ perror( "write file2 error"); goto err; } } } if(-1 == read_num){ perror("read file1 error"); goto err; } rev = 0; err: if(buffer) free(buffer); if(buffer_zero) free(buffer_zero); close(fd1); close(fd2); return rev; }
int main(int argc, char *argv[]) { if(argc < 3) { printf("Usage: %s file1 file2\n", argv[0]); return -1; } my_cp(argv[1], argv[2]); return 0; }
|