int file_chunk(char *src_filename, char *chunk_filename, int chunk_algo) { int fd_src, fd_chunk; chunk_file_header chunk_file_hdr; int ret = 0, rwsize; fd_src = open(src_filename, O_RDONLY); if (fd_src == -1) return -1; fd_chunk = open(chunk_filename, O_WRONLY|O_CREAT|O_TRUNC, 0644); if (fd_chunk == -1) { ret = -1; goto _FILE_CHUNK_EXIT; } /* pre-write chunk file header */ chunk_file_hdr.block_nr = 0; rwsize = write(fd_chunk, &chunk_file_hdr, CHUNK_FILE_HEADER_SZ); if (rwsize == -1 || rwsize != CHUNK_FILE_HEADER_SZ) { ret = -1; goto _FILE_CHUNK_EXIT; } /* file chunking according to chunk algorithms */ switch(chunk_algo) { case CHUNK_FSP: ret = file_chunk_fsp(fd_src, fd_chunk, &chunk_file_hdr); break; case CHUNK_CDC: ret = file_chunk_cdc(fd_src, fd_chunk, &chunk_file_hdr); break; case CHUNK_SBC: ret = file_chunk_sbc(fd_src, fd_chunk, &chunk_file_hdr); break; } /* write back chunk file header */ if (ret == 0) { if (lseek(fd_chunk, 0, SEEK_SET) == -1) { ret = -1; goto _FILE_CHUNK_EXIT; } chunk_file_hdr.block_sz = BLOCK_SZ; rwsize = write(fd_chunk, &chunk_file_hdr, CHUNK_FILE_HEADER_SZ); if (rwsize == -1 || rwsize != CHUNK_FILE_HEADER_SZ) ret = -1; } _FILE_CHUNK_EXIT: close(fd_src); close(fd_chunk); return ret; }
int filename_chunk_cdc(const char *filename, CDCFileDescriptor *file_descr, SeafileCrypt *crypt, gboolean write_data) { int fd_src = g_open (filename, O_RDONLY | O_BINARY, 0); if (fd_src < 0) return -1; int ret = file_chunk_cdc (fd_src, file_descr, crypt, write_data); close (fd_src); return ret; }
int filename_chunk_cdc(const char *filename, CDCFileDescriptor *file_descr, SeafileCrypt *crypt, gboolean write_data) { int fd_src = seaf_util_open (filename, O_RDONLY | O_BINARY); if (fd_src < 0) { seaf_warning ("CDC: failed to open %s.\n", filename); return -1; } int ret = file_chunk_cdc (fd_src, file_descr, crypt, write_data); close (fd_src); return ret; }