create method

  1. @override
void create(
  1. String path,
  2. {bool recursive = false,
  3. EntityType type = EntityType.file}
)
override

Implementation

@override
void create(
  String path, {
  bool recursive = false,
  EntityType type = EntityType.file,
}) {
  final [...dirNames, entityName] = split(path);
  VEntity entity = root;
  for (final dirName in dirNames) {
    if (entity case VDir dir) {
      if (dir.children[dirName] != null) {
        entity = dir.children[dirName]!;
        continue;
      } else if (recursive) {
        dir.children[dirName] = entity = VDir(dirName);
        continue;
      }
    }
    return;
  }

  if (entity case VDir dir) {
    if (dir.children[entityName] == null) {
      dir.children[entityName] = switch (type) {
        EntityType.file => VFile(entityName),
        EntityType.dir => VDir(entityName)
      };
    } else {
      throw "[Virtual FileSystem]: file or dir with also name exists!";
    }
  }
}