OFF_T get_file_size(char *device) { OFF_T size = 0; fd_t fd; #ifdef WINDOWS SetLastError(0); fd = CreateFile(device, GENERIC_READ, FILE_SHARE_READ, NULL, OPEN_EXISTING, 0, NULL); #else fd = open(device, 0); #endif if (INVALID_FD(fd)) { return size; } size = SeekEnd(fd); size /= BLK_SIZE; CLOSE(fd); return size; }
int do_dump(child_args_t * args) { ssize_t NumBytes = 0; OFF_T TargetLBA, TotalBytes = 0; char *buff; fd_t fd; if ((buff = (char *)ALLOC(args->htrsiz * BLK_SIZE)) == NULL) { fprintf(stderr, "Can't allocate buffer\n"); return (-1); } memset(buff, 0, args->htrsiz * BLK_SIZE); fd = Open(args->device, args->flags | CLD_FLG_R); if (INVALID_FD(fd)) { pMsg(ERR, args, "could not open %s.\n", args->device); pMsg(ERR, args, "%s: Error = %u\n", args->device, GETLASTERROR()); return (-1); } TargetLBA = Seek(fd, args->start_lba * BLK_SIZE); if (TargetLBA != (args->start_lba * (OFF_T) BLK_SIZE)) { pMsg(ERR, args, "Could not seek to start position.\n"); CLOSE(fd); return (-1); } do { NumBytes = Read(fd, buff, args->htrsiz * BLK_SIZE); if ((NumBytes > args->htrsiz * BLK_SIZE) || (NumBytes < 0)) { pMsg(ERR, args, "Failure reading %s\n", args->device); pMsg(ERR, args, "Last Error was %lu\n", GETLASTERROR()); break; } dump_data(stdout, buff, NumBytes, 16, 0, FMT_STR); TotalBytes += (OFF_T) NumBytes; } while ((TotalBytes < (args->htrsiz * BLK_SIZE)) && (NumBytes > 0)); FREE(buff); CLOSE(fd); return 0; }