MCPcopy Create free account
hub / github.com/cloudflare/agents / mkdir

Method mkdir

packages/shell/src/filesystem.ts:1100–1162  ·  view source on GitHub ↗
(
    path: string,
    opts?: { recursive?: boolean },
    _depth = 0
  )

Source from the content-addressed store, hash-verified

1098 }
1099
1100 async mkdir(
1101 path: string,
1102 opts?: { recursive?: boolean },
1103 _depth = 0
1104 ): Promise<void> {
1105 await this.ensureInit();
1106 if (_depth > MAX_MKDIR_DEPTH) {
1107 throw new Error(
1108 `ELOOP: mkdir recursion too deep (max ${MAX_MKDIR_DEPTH} levels)`
1109 );
1110 }
1111 const normalized = normalizePath(path);
1112 if (normalized === "/") return;
1113 const T = this.tableName;
1114
1115 const existing = await this.sql.query<{ type: string }>(
1116 `SELECT type FROM ${T} WHERE path = ?`,
1117 normalized
1118 );
1119
1120 if (existing.length > 0) {
1121 if (existing[0].type === "directory" && opts?.recursive) return;
1122 throw new Error(
1123 existing[0].type === "directory"
1124 ? `EEXIST: directory already exists: ${path}`
1125 : `EEXIST: path exists as a file: ${path}`
1126 );
1127 }
1128
1129 const parentPath = getParent(normalized);
1130 const parentRows = await this.sql.query<{ type: string }>(
1131 `SELECT type FROM ${T} WHERE path = ?`,
1132 parentPath
1133 );
1134
1135 if (!parentRows[0]) {
1136 if (opts?.recursive) {
1137 await this.mkdir(parentPath, { recursive: true }, _depth + 1);
1138 } else {
1139 throw new Error(`ENOENT: parent directory not found: ${parentPath}`);
1140 }
1141 } else if (parentRows[0].type !== "directory") {
1142 throw new Error(`ENOTDIR: parent is not a directory: ${parentPath}`);
1143 }
1144
1145 const name = getBasename(normalized);
1146 const now = Math.floor(Date.now() / 1000);
1147 await this.sql.run(
1148 `INSERT INTO ${T}
1149 (path, parent_path, name, type, size, created_at, modified_at)
1150 VALUES (?, ?, ?, 'directory', 0, ?, ?)`,
1151 normalized,
1152 parentPath,
1153 name,
1154 now,
1155 now
1156 );
1157 this.emit("create", normalized, "directory");

Callers 1

cpMethod · 0.95

Calls 7

ensureInitMethod · 0.95
emitMethod · 0.95
_observeMethod · 0.95
getParentFunction · 0.85
getBasenameFunction · 0.85
normalizePathFunction · 0.70
runMethod · 0.65

Tested by

no test coverage detected