public open()

in packages/web-ide-fs/src/browserfs/GitLabReadableFileSystem.ts [128:181]


  public open(path: string, flags: FileFlag, mode: number, cb: BFSCallback<File>): void {
    // INVARIANT: You can't write to files on this file system.
    if (flags.isWriteable()) {
      return cb(new ApiError(ErrorCode.EPERM, path));
    }
    // eslint-disable-next-line @typescript-eslint/no-this-alias
    const self = this;
    // Check if the path exists, and is a file.
    const fileEntry = this.#entries.get(path);

    if (!fileEntry) {
      return cb(ApiError.ENOENT(path));
    }

    if (fileEntry.type === FileType.Blob) {
      switch (flags.pathExistsAction()) {
        case ActionType.THROW_EXCEPTION:
        case ActionType.TRUNCATE_FILE:
          return cb(ApiError.EEXIST(path));
        case ActionType.NOP:
          // Use existing file contents.
          // XXX: Uh, this maintains the previously-used flag.
          if (fileEntry.content.type === BlobContentType.Raw) {
            const stats = convertFileEntryToStats(fileEntry);

            return cb(null, new NoSyncFile(self, path, flags, stats, stats.fileData || undefined));
          }

          this.#contentProvider
            .getContent(fileEntry.content.path)
            .then(raw => {
              // TODO do something with file size
              // fileEntry.size = ...;
              fileEntry.content = {
                type: BlobContentType.Raw,
                raw,
              };

              const stats = convertFileEntryToStats(fileEntry);

              return cb(
                null,
                new NoSyncFile(self, path, flags, stats, stats.fileData || undefined),
              );
            })
            .catch(err => cb(err));
          break;
        default:
          return cb(new ApiError(ErrorCode.EINVAL, 'Invalid FileMode object.'));
      }
    } else {
      return cb(ApiError.EISDIR(path));
    }
  }