Skip to content
This repository was archived by the owner on Nov 1, 2023. It is now read-only.

Commit ef375c3

Browse files
authored
Slight refactoring
1 parent 8679e41 commit ef375c3

File tree

4 files changed

+118
-189
lines changed

4 files changed

+118
-189
lines changed

src/ApiService/ApiService/OneFuzzTypes/Model.cs

Lines changed: 7 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -917,7 +917,7 @@ public record VmDefinition(
917917
long Value
918918
);
919919

920-
public record TaskDefinition(
920+
public readonly record struct TaskDefinition(
921921
TaskFeature[] Features,
922922
VmDefinition Vm,
923923
ContainerDefinition[] Containers,
@@ -932,37 +932,31 @@ public record WorkSet(
932932
List<WorkUnit> WorkUnits
933933
);
934934

935-
936-
937-
938-
939-
public record ContainerDefinition(
935+
public readonly record struct ContainerDefinition(
940936
ContainerType Type,
941937
Compare Compare,
942938
long Value,
943939
ContainerPermission Permissions);
944940

945-
946941
// TODO: service shouldn't pass SyncedDir, but just the url and let the agent
947942
// come up with paths
948-
public record SyncedDir(string Path, Uri Url);
949-
943+
public readonly record struct SyncedDir(string Path, Uri Url);
950944

951945
[JsonConverter(typeof(ContainerDefConverter))]
952946
public interface IContainerDef { }
953947
public record SingleContainer(SyncedDir SyncedDir) : IContainerDef;
954-
public record MultipleContainer(List<SyncedDir> SyncedDirs) : IContainerDef;
948+
public record MultipleContainer(IReadOnlyList<SyncedDir> SyncedDirs) : IContainerDef;
955949

956950

957951
public class ContainerDefConverter : JsonConverter<IContainerDef> {
958952
public override IContainerDef? Read(ref Utf8JsonReader reader, Type typeToConvert, JsonSerializerOptions options) {
959953
if (reader.TokenType == JsonTokenType.StartObject) {
960954
var result = (SyncedDir?)JsonSerializer.Deserialize(ref reader, typeof(SyncedDir), options);
961-
if (result is null) {
962-
return null;
955+
if (result is SyncedDir sd) {
956+
return new SingleContainer(sd);
963957
}
964958

965-
return new SingleContainer(result);
959+
return null;
966960
}
967961

968962
if (reader.TokenType == JsonTokenType.StartArray) {

src/ApiService/ApiService/onefuzzlib/Config.cs

Lines changed: 72 additions & 70 deletions
Original file line numberDiff line numberDiff line change
@@ -78,77 +78,79 @@ private static BlobContainerSasPermissions ConvertPermissions(ContainerPermissio
7878
config.inputQueue = await _queue.GetQueueSas(task.TaskId.ToString(), StorageType.Corpus, QueueSasPermissions.Add | QueueSasPermissions.Read | QueueSasPermissions.Update | QueueSasPermissions.Process);
7979
}
8080

81-
var containersByType = definition.Containers.Where(c => c.Type != ContainerType.Setup && task.Config.Containers != null)
82-
.ToAsyncEnumerable()
83-
.SelectAwait(async countainerDef => {
84-
var containers = await
85-
task.Config.Containers!
86-
.Where(c => c.Type == countainerDef.Type).Select(container => (countainerDef, container))
87-
.Where(x => x.container != null)
88-
.ToAsyncEnumerable()
89-
.SelectAwait(async (x, i) =>
90-
new SyncedDir(
91-
string.Join("_", "task", x.Item1.Type.ToString().ToLower(), i),
92-
await _containers.GetContainerSasUrl(x.Item2.Name, StorageType.Corpus, ConvertPermissions(x.Item1.Permissions)))
93-
).ToListAsync();
94-
return (countainerDef, containers);
95-
}
96-
);
97-
98-
await foreach (var data in containersByType) {
99-
100-
if (!data.containers.Any()) {
101-
continue;
102-
}
81+
if (task.Config.Containers is not null) {
82+
var containersByType =
83+
await Async.Task.WhenAll(
84+
definition.Containers
85+
.Where(c => c.Type is not ContainerType.Setup)
86+
.Select(async countainerDef => {
87+
var syncedDirs =
88+
await Async.Task.WhenAll(
89+
task.Config.Containers
90+
.Where(c => c.Type == countainerDef.Type)
91+
.Select(async (container, i) =>
92+
new SyncedDir(
93+
string.Join("_", "task", countainerDef.Type.ToString().ToLower(), i),
94+
await _containers.GetContainerSasUrl(container.Name, StorageType.Corpus, ConvertPermissions(countainerDef.Permissions)))
95+
));
96+
97+
return (countainerDef, syncedDirs);
98+
}));
99+
100+
foreach (var (countainerDef, syncedDirs) in containersByType) {
101+
if (!syncedDirs.Any()) {
102+
continue;
103+
}
103104

104-
IContainerDef def = data.countainerDef switch {
105-
ContainerDefinition { Compare: Compare.Equal, Value: 1 } or
106-
ContainerDefinition { Compare: Compare.AtMost, Value: 1 } when data.containers.Count == 1 => new SingleContainer(data.containers[0]),
107-
_ => new MultipleContainer(data.containers)
108-
};
109-
110-
switch (data.countainerDef.Type) {
111-
case ContainerType.Analysis:
112-
config.Analysis = def;
113-
break;
114-
case ContainerType.Coverage:
115-
config.Coverage = def;
116-
break;
117-
case ContainerType.Crashes:
118-
config.Crashes = def;
119-
break;
120-
case ContainerType.Inputs:
121-
config.Inputs = def;
122-
break;
123-
case ContainerType.NoRepro:
124-
config.NoRepro = def;
125-
break;
126-
case ContainerType.ReadonlyInputs:
127-
config.ReadonlyInputs = def;
128-
break;
129-
case ContainerType.Reports:
130-
config.Reports = def;
131-
break;
132-
case ContainerType.Tools:
133-
config.Tools = def;
134-
break;
135-
case ContainerType.UniqueInputs:
136-
config.UniqueInputs = def;
137-
break;
138-
case ContainerType.UniqueReports:
139-
config.UniqueReports = def;
140-
break;
141-
case ContainerType.RegressionReports:
142-
config.RegressionReports = def;
143-
break;
144-
case ContainerType.Extra:
145-
config.Extra = def;
146-
break;
147-
case ContainerType.ExtraRw:
148-
config.ExtraRw = def;
149-
break;
150-
default:
151-
throw new InvalidDataException($"unknown container type: {data.countainerDef.Type}");
105+
IContainerDef def = countainerDef switch {
106+
ContainerDefinition { Compare: Compare.Equal | Compare.AtMost, Value: 1 }
107+
when syncedDirs is [var syncedDir] => new SingleContainer(syncedDir),
108+
_ => new MultipleContainer(syncedDirs)
109+
};
110+
111+
switch (countainerDef.Type) {
112+
case ContainerType.Analysis:
113+
config.Analysis = def;
114+
break;
115+
case ContainerType.Coverage:
116+
config.Coverage = def;
117+
break;
118+
case ContainerType.Crashes:
119+
config.Crashes = def;
120+
break;
121+
case ContainerType.Inputs:
122+
config.Inputs = def;
123+
break;
124+
case ContainerType.NoRepro:
125+
config.NoRepro = def;
126+
break;
127+
case ContainerType.ReadonlyInputs:
128+
config.ReadonlyInputs = def;
129+
break;
130+
case ContainerType.Reports:
131+
config.Reports = def;
132+
break;
133+
case ContainerType.Tools:
134+
config.Tools = def;
135+
break;
136+
case ContainerType.UniqueInputs:
137+
config.UniqueInputs = def;
138+
break;
139+
case ContainerType.UniqueReports:
140+
config.UniqueReports = def;
141+
break;
142+
case ContainerType.RegressionReports:
143+
config.RegressionReports = def;
144+
break;
145+
case ContainerType.Extra:
146+
config.Extra = def;
147+
break;
148+
case ContainerType.ExtraRw:
149+
config.ExtraRw = def;
150+
break;
151+
default:
152+
throw new InvalidDataException($"unknown container type: {countainerDef.Type}");
153+
}
152154
}
153155
}
154156

0 commit comments

Comments
 (0)