public void addChunk(WriteFileCommand writeChunkCommand) { if (writeChunkCommand is SparseWriteCommand) { PackingType = PackingType.Sparse; } else if (writeChunkCommand is UnlzoCommand) { PackingType = PackingType.Lzo; } FileChunksTotalSize += writeChunkCommand.Size; Chunks.Add(writeChunkCommand); }
public List <WriteFileCommand> pack(string workDirectory, FileStream outputStream, IMessageLogger messageLogger) { long currentOffset = outputStream.Position; List <WriteFileCommand> chunks = new List <WriteFileCommand>(); if (Chunks.Count > 0) { messageLogger.logMessage($"Добавление образа {Name}."); string imgFilename = Path.Combine(workDirectory, Name + ".img"); WriteFileCommand command = Chunks[0]; if (command is WritePiCommand writePiCommand) { long size = writeFileToBinary(outputStream, imgFilename); chunks.Add(new WritePiCommand(writePiCommand, currentOffset, size)); } else if (command is WriteBootCommand writeBootCommand) { long size = writeFileToBinary(outputStream, imgFilename); chunks.Add(new WriteBootCommand(writeBootCommand, currentOffset, size)); } else if (command is StoreSecureInfoCommand storeSecureInfoCommand) { long size = writeFileToBinary(outputStream, imgFilename); chunks.Add(new StoreSecureInfoCommand(storeSecureInfoCommand, currentOffset, size)); } else if (command is StoreNuttxConfigCommand storeNuttxConfigCommand) { long size = writeFileToBinary(outputStream, imgFilename); chunks.Add(new StoreNuttxConfigCommand(storeNuttxConfigCommand, currentOffset, size)); } else if (command is SparseWriteCommand writeSparseCommand) { messageLogger.logMessage("Упаковка sparse."); // string simgFilename = Path.Combine( workDirectory, Name + ".simg" ); Compress.Sparse.Compress(imgFilename, 4096, 157286400); int index = 0; string chunkFileName = imgFilename + ".chunk." + index++; while (File.Exists(chunkFileName)) { messageLogger.logMessage("Добавление части " + index + "."); currentOffset = outputStream.Position; long size = writeFileToBinary(outputStream, chunkFileName); File.Delete(chunkFileName); chunks.Add(new SparseWriteCommand(writeSparseCommand, currentOffset, size, firstChunk: index == 1)); chunkFileName = imgFilename + ".chunk." + index++; } } else if (command is UnlzoCommand unlzoCommand) { const long chunkSize = 100 * 1024 * 1024; messageLogger.logMessage("Упаковка lzo."); string wholeLzoFilename = imgFilename + ".lzo"; if (File.Exists(wholeLzoFilename)) { File.Delete(wholeLzoFilename); } Compress.Lzo.Compress(imgFilename, wholeLzoFilename); long packedLength = 0; using (FileStream inputStream = new FileStream(wholeLzoFilename, FileMode.Open, FileAccess.Read)) { packedLength = inputStream.Length; } if (packedLength < chunkSize) { currentOffset = outputStream.Position; long size = writeFileToBinary(outputStream, wholeLzoFilename); //long size = (!File.Exists( imgFilename + ".chunk.1" )) ? // writeFileToBinary( outputStream, wholeLzoFilename ) : // writeFileToBinary( outputStream, imgFilename + ".chunk.1" ); chunks.Add(new UnlzoCommand(unlzoCommand, currentOffset, size, firstChunk: true)); } else { using (FileStream inputStream = new FileStream(imgFilename, FileMode.Open, FileAccess.Read)) { long imgLength = inputStream.Length; //if (!File.Exists( imgFilename + ".chunk.1" )) { messageLogger.logMessage("Разбиение на чанки."); int chunksCounter = 0; for (long position = 0; position < imgLength; position += chunkSize) { chunksCounter++; long remainderLength = imgLength - position; //readFileFromBinary( inputStream, position, remainderLength >= chunkSize ? chunkSize : remainderLength, imgFilename + $"a{(chunksCounter + 9):X}" ); readFileFromBinary(inputStream, position, remainderLength >= chunkSize ? chunkSize : remainderLength, imgFilename + ".plain_chunk." + chunksCounter); } if (chunksCounter > 1) { messageLogger.logMessage("Упаковка " + chunksCounter + " частей."); } for (int chunkIndex = 1; chunkIndex <= chunksCounter; chunkIndex++) { messageLogger.logMessage("Упаковка части " + chunkIndex + "."); //string plainChunkName = imgFilename + $"a{(chunkIndex + 9):X}"; string plainChunkName = imgFilename + ".plain_chunk." + chunkIndex; string chunkName = imgFilename + ".chunk." + chunkIndex; if (File.Exists(chunkName)) { File.Delete(chunkName); } Compress.Lzo.Compress(plainChunkName, chunkName); File.Delete(plainChunkName); } } //for (int chunkIndex = 1; chunkIndex <= chunksCounter; chunkIndex++) for (int chunkIndex = 1; File.Exists(imgFilename + ".chunk." + chunkIndex); chunkIndex++) { messageLogger.logMessage("Добавление части " + chunkIndex + "."); currentOffset = outputStream.Position; long size = writeFileToBinary(outputStream, imgFilename + ".chunk." + chunkIndex); File.Delete(imgFilename + ".chunk." + chunkIndex); chunks.Add(new UnlzoCommand(unlzoCommand, currentOffset, size, firstChunk: chunkIndex == 1)); } } } File.Delete(wholeLzoFilename); } } return(chunks); }
public Partition(WriteFileCommand writeChunkCommand) { Name = writeChunkCommand.PartitionName; Size = writeChunkCommand.Size; }