io.dart 3.8 KB
Newer Older
Ian Hickson's avatar
Ian Hickson committed
1
// Copyright 2014 The Flutter Authors. All rights reserved.
2 3 4 5 6 7 8 9 10 11 12 13 14
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.

import 'dart:io' as io show IOOverrides, Directory, File, Link;

import 'package:flutter_tools/src/base/file_system.dart';

/// An [IOOverrides] that can delegate to [FileSystem] implementation if provided.
///
/// Does not override any of the socket facilities.
///
/// Do not provide a [LocalFileSystem] as a delegate. Since internally this calls
/// out to `dart:io` classes, it will result in a stack overflow error as the
15
/// IOOverrides and LocalFileSystem call each other endlessly.
16 17 18 19
///
/// The only safe delegate types are those that do not call out to `dart:io`,
/// like the [MemoryFileSystem].
class FlutterIOOverrides extends io.IOOverrides {
20
  FlutterIOOverrides({ FileSystem? fileSystem })
21 22
    : _fileSystemDelegate = fileSystem;

23
  final FileSystem? _fileSystemDelegate;
24 25 26 27 28 29

  @override
  io.Directory createDirectory(String path) {
    if (_fileSystemDelegate == null) {
      return super.createDirectory(path);
    }
30
    return _fileSystemDelegate!.directory(path);
31 32 33 34 35 36 37
  }

  @override
  io.File createFile(String path) {
    if (_fileSystemDelegate == null) {
      return super.createFile(path);
    }
38
    return _fileSystemDelegate!.file(path);
39 40 41 42 43 44 45
  }

  @override
  io.Link createLink(String path) {
    if (_fileSystemDelegate == null) {
      return super.createLink(path);
    }
46
    return _fileSystemDelegate!.link(path);
47 48 49 50 51 52 53
  }

  @override
  Stream<FileSystemEvent> fsWatch(String path, int events, bool recursive) {
    if (_fileSystemDelegate == null) {
      return super.fsWatch(path, events, recursive);
    }
54
    return _fileSystemDelegate!.file(path).watch(events: events, recursive: recursive);
55 56 57 58 59 60 61
  }

  @override
  bool fsWatchIsSupported() {
    if (_fileSystemDelegate == null) {
      return super.fsWatchIsSupported();
    }
62
    return _fileSystemDelegate!.isWatchSupported;
63 64 65 66 67 68 69
  }

  @override
  Future<FileSystemEntityType> fseGetType(String path, bool followLinks) {
    if (_fileSystemDelegate == null) {
      return super.fseGetType(path, followLinks);
    }
70
    return _fileSystemDelegate!.type(path, followLinks: followLinks);
71 72 73 74 75 76 77
  }

  @override
  FileSystemEntityType fseGetTypeSync(String path, bool followLinks) {
    if (_fileSystemDelegate == null) {
      return super.fseGetTypeSync(path, followLinks);
    }
78
    return _fileSystemDelegate!.typeSync(path, followLinks: followLinks);
79 80 81 82 83 84 85
  }

  @override
  Future<bool> fseIdentical(String path1, String path2) {
    if (_fileSystemDelegate == null) {
      return super.fseIdentical(path1, path2);
    }
86
    return _fileSystemDelegate!.identical(path1, path2);
87 88 89 90 91 92 93
  }

  @override
  bool fseIdenticalSync(String path1, String path2) {
    if (_fileSystemDelegate == null) {
      return super.fseIdenticalSync(path1, path2);
    }
94
    return _fileSystemDelegate!.identicalSync(path1, path2);
95 96 97 98 99 100 101
  }

  @override
  io.Directory getCurrentDirectory() {
    if (_fileSystemDelegate == null) {
      return super.getCurrentDirectory();
    }
102
    return _fileSystemDelegate!.currentDirectory;
103 104 105 106 107 108 109
  }

  @override
  io.Directory getSystemTempDirectory() {
    if (_fileSystemDelegate == null) {
      return super.getSystemTempDirectory();
    }
110
    return _fileSystemDelegate!.systemTempDirectory;
111 112 113 114 115 116 117
  }

  @override
  void setCurrentDirectory(String path) {
    if (_fileSystemDelegate == null) {
      return super.setCurrentDirectory(path);
    }
118
    _fileSystemDelegate!.currentDirectory = path;
119 120 121 122 123 124 125
  }

  @override
  Future<FileStat> stat(String path) {
    if (_fileSystemDelegate == null) {
      return super.stat(path);
    }
126
    return _fileSystemDelegate!.stat(path);
127 128 129 130 131 132 133
  }

  @override
  FileStat statSync(String path) {
    if (_fileSystemDelegate == null) {
      return super.statSync(path);
    }
134
    return _fileSystemDelegate!.statSync(path);
135 136
  }
}