Python
Kod: Tümünü seç
def write_partition(comm, disk_fd, local_path, part_offset, part_size):
write_offset = BLOCK_SIZE * (part_offset // BLOCK_SIZE)
end_offset = part_offset + part_size
# TODO support unaligned writes via read/modify/write
if part_offset % BLOCK_SIZE:
raise RuntimeError("Unaligned partition writes are not supported yet")
# Sanity check
assert part_offset >= 34 * 512, "Will not allow overwriting GPT scheme"
with open_local_readable(local_path) as f:
try:
length = f.seek(0, 2)
except OSError:
# Will try to write up to the end of the file.
_logger.debug("File %s is not seekable, length is unknown",
local_path)
else:
# Restore position and check if file is small enough
f.seek(0)
if length > part_size:
raise RuntimeError("File size %d is larger than partition "
"size %d" % (length, part_size))
# Some special bytes report 0 (such as /dev/zero)
if length > 0:
_logger.debug("Will write %d bytes", length)
written = 0
while write_offset < end_offset:
chunksize = min(end_offset - write_offset, BLOCK_SIZE * MAX_BLOCK_SIZE)
data = f.read(chunksize)
if not data:
break # End of file
laf_write(comm, disk_fd, write_offset // BLOCK_SIZE, data)
written += len(data)
write_offset += chunksize
if len(data) != chunksize:
break # Short read, end of file
_logger.info("Done after writing %d bytes from %s", written, local_path)
benim yazdığım kod ise şu...
Kod: Tümünü seç
var
TmpBuf,DatBuf:TMyByteArray;
MAX_BLOCK_SIZE,BLOCK_SIZE,part_offset,write_offset,end_offset,chunksize,Maxval,RdLen:integer;
begin
MAX_BLOCK_SIZE:=(16 * 1024 - 512) div 512;
BLOCK_SIZE:=512;
Strm:=TFileStream.Create('recovery',fmOpenRead + fmShareDenyNone);
FullDatLen:=Strm.Size;
Strm.Seek(0,soFromBeginning);
strm.Position:=0;
part_offset:=34 * 512;
write_offset:= BLOCK_SIZE * (part_offset div BLOCK_SIZE);
end_offset:=FullDatLen+part_offset;
chunksize := min(FullDatLen - write_offset, BLOCK_SIZE * MAX_BLOCK_SIZE);
Maxval:= MAX_BLOCK_SIZE div chunksize;
for i:= 0 to Maxval -1 do
begin
SetLength(DatBuf,ChunkSize);
SetLength(TmpBuf,ChunkSize);
RdLen:=Strm.Read(DatBuf[0],ChunkSize);
CopyMemory(@TmpBuf[0],@DatBuf[0],Length(DatBuf));
OpenComPort1('COM6',115200);
WriteComPort(TmpBuf,Length(TmpBuf),8,5000,10);
delay(100);
DatBuf[0]:=DatBuf[0]+chunksize;
Application.ProcessMessages;
end;