1 // 2 // Copyright (c) 2010-2024 Antmicro 3 // 4 // This file is licensed under the MIT License. 5 // Full license text is available in 'licenses/MIT.txt'. 6 // 7 8 using System.IO; 9 using Antmicro.Renode.Exceptions; 10 using Antmicro.Renode.Utilities; 11 using Antmicro.Renode.Logging; 12 13 namespace Antmicro.Renode.Storage 14 { 15 public static class DataStorage 16 { Create(string imageFile, long? size = null, bool persistent = false, byte paddingByte = 0)17 public static Stream Create(string imageFile, long? size = null, bool persistent = false, byte paddingByte = 0) 18 { 19 Logger.Log(LogLevel.Warning, "Create method is obsolete, please use CreateFromFile method"); 20 return CreateFromFile(imageFile, size, persistent, paddingByte); 21 } 22 CreateFromFile(string imageFile, long? size = null, bool persistent = false, byte paddingByte = 0)23 public static Stream CreateFromFile(string imageFile, long? size = null, bool persistent = false, byte paddingByte = 0) 24 { 25 if(string.IsNullOrEmpty(imageFile)) 26 { 27 throw new ConstructionException("No image file provided."); 28 } 29 30 if(!persistent) 31 { 32 var tempFileName = TemporaryFilesManager.Instance.GetTemporaryFile(); 33 FileCopier.Copy(imageFile, tempFileName, true); 34 imageFile = tempFileName; 35 } 36 37 return new SerializableStreamView(new FileStream(imageFile, FileMode.OpenOrCreate), size, paddingByte: paddingByte); 38 } 39 Create(long size, byte paddingByte = 0)40 public static Stream Create(long size, byte paddingByte = 0) 41 { 42 Logger.Log(LogLevel.Warning, "Create method is obsolete, please use CreateInTemporaryFile method"); 43 return CreateInTemporaryFile(size, paddingByte); 44 } 45 CreateInTemporaryFile(long size, byte paddingByte = 0)46 public static Stream CreateInTemporaryFile(long size, byte paddingByte = 0) 47 { 48 return new SerializableStreamView(new FileStream(TemporaryFilesManager.Instance.GetTemporaryFile(), FileMode.OpenOrCreate), size, paddingByte); 49 } 50 CreateInMemory(int size, byte paddingByte = 0)51 public static Stream CreateInMemory(int size, byte paddingByte = 0) 52 { 53 var mem = new byte[size]; 54 return new SerializableStreamView(new MemoryStream(mem), size, paddingByte); 55 } 56 } 57 } 58