int main(int argc, char *argv[]) { int64_t file_size; int64_t remain_bytes; char *filename; int fd; int read_bytes; int result; int crc32; char buff[512 * 1024]; if (argc < 2) { printf("Usage: %s <filename>\n", argv[0]); return 1; } filename = argv[1]; fd = open(filename, O_RDONLY); if (fd < 0) { printf("file: "__FILE__", line: %d, " \ "open file %s fail, " \ "errno: %d, error info: %s\n", \ __LINE__, filename, errno, STRERROR(errno)); return errno != 0 ? errno : EACCES; } if ((file_size=lseek(fd, 0, SEEK_END)) < 0) { printf("file: "__FILE__", line: %d, " \ "call lseek fail, " \ "errno: %d, error info: %s\n", \ __LINE__, errno, STRERROR(errno)); close(fd); return errno; } if (lseek(fd, 0, SEEK_SET) < 0) { printf("file: "__FILE__", line: %d, " \ "call lseek fail, " \ "errno: %d, error info: %s\n", \ __LINE__, errno, STRERROR(errno)); close(fd); return errno; } crc32 = CRC32_XINIT; result = 0; remain_bytes = file_size; while (remain_bytes > 0) { if (remain_bytes > sizeof(buff)) { read_bytes = sizeof(buff); } else { read_bytes = remain_bytes; } if (read(fd, buff, read_bytes) != read_bytes) { printf("file: "__FILE__", line: %d, " \ "call lseek fail, " \ "errno: %d, error info: %s\n", \ __LINE__, errno, STRERROR(errno)); result = errno != 0 ? errno : EIO; break; } crc32 = CRC32_ex(buff, read_bytes, crc32); remain_bytes -= read_bytes; } close(fd); if (result == 0) { crc32 = CRC32_FINAL(crc32); printf("%u\n", crc32); } return result; }
int dio_write_file(struct fast_task_info *pTask) { StorageClientInfo *pClientInfo; StorageFileContext *pFileContext; int result; int write_bytes; char *pDataBuff; pClientInfo = (StorageClientInfo *)pTask->arg; pFileContext = &(pClientInfo->file_context); result = 0; do { if (pFileContext->fd < 0) { if (pFileContext->extra_info.upload.before_open_callback!=NULL) { result = pFileContext->extra_info.upload. \ before_open_callback(pTask); if (result != 0) { break; } } if ((result=dio_open_file(pFileContext)) != 0) { break; } } pDataBuff = pTask->data + pFileContext->buff_offset; write_bytes = pTask->length - pFileContext->buff_offset; if (fc_safe_write(pFileContext->fd, pDataBuff, write_bytes) != write_bytes) { result = errno != 0 ? errno : EIO; logError("file: "__FILE__", line: %d, " \ "write to file: %s fail, fd=%d, write_bytes=%d, " \ "errno: %d, error info: %s", \ __LINE__, pFileContext->filename, \ pFileContext->fd, write_bytes, \ result, STRERROR(result)); } pthread_mutex_lock(&g_dio_thread_lock); g_storage_stat.total_file_write_count++; if (result == 0) { g_storage_stat.success_file_write_count++; } pthread_mutex_unlock(&g_dio_thread_lock); if (result != 0) { break; } if (pFileContext->calc_crc32) { pFileContext->crc32 = CRC32_ex(pDataBuff, write_bytes, \ pFileContext->crc32); } if (pFileContext->calc_file_hash) { if (g_file_signature_method == STORAGE_FILE_SIGNATURE_METHOD_HASH) { CALC_HASH_CODES4(pDataBuff, write_bytes, \ pFileContext->file_hash_codes) } else { my_md5_update(&pFileContext->md5_context, \ (unsigned char *)pDataBuff, write_bytes); } } /* logInfo("###dio write bytes: %d, pTask->length=%d, buff_offset=%d", \ write_bytes, pTask->length, pFileContext->buff_offset); */ pFileContext->offset += write_bytes; if (pFileContext->offset < pFileContext->end) { pFileContext->buff_offset = 0; storage_nio_notify(pTask); //notify nio to deal } else { if (pFileContext->calc_crc32) { pFileContext->crc32 = CRC32_FINAL( \ pFileContext->crc32); } if (pFileContext->calc_file_hash) { if (g_file_signature_method == STORAGE_FILE_SIGNATURE_METHOD_HASH) { FINISH_HASH_CODES4(pFileContext->file_hash_codes) } else { my_md5_final((unsigned char *)(pFileContext-> \ file_hash_codes), &pFileContext->md5_context); } } if (pFileContext->extra_info.upload.before_close_callback != NULL) { result = pFileContext->extra_info.upload. \ before_close_callback(pTask); } /* file write done, close it */ close(pFileContext->fd); pFileContext->fd = -1; if (pFileContext->done_callback != NULL) { pFileContext->done_callback(pTask, result); } } return 0; } while (0); pClientInfo->clean_func(pTask); if (pFileContext->done_callback != NULL) { pFileContext->done_callback(pTask, result); } return result; }