Write 实现 BackendProtocol.Write
(ctx context.Context, path, content string)
| 142 | |
| 143 | // Write 实现 BackendProtocol.Write |
| 144 | func (b *StateBackend) Write(ctx context.Context, path, content string) (*WriteResult, error) { |
| 145 | b.mu.Lock() |
| 146 | defer b.mu.Unlock() |
| 147 | |
| 148 | now := time.Now() |
| 149 | lines := strings.Split(content, "\n") |
| 150 | |
| 151 | // 检查文件是否已存在 |
| 152 | _, exists := b.files[path] |
| 153 | |
| 154 | data := &FileData{ |
| 155 | Lines: lines, |
| 156 | ModifiedAt: now, |
| 157 | CreatedAt: now, |
| 158 | } |
| 159 | |
| 160 | if exists { |
| 161 | // 保留创建时间 |
| 162 | data.CreatedAt = b.files[path].CreatedAt |
| 163 | } |
| 164 | |
| 165 | b.files[path] = data |
| 166 | |
| 167 | return &WriteResult{ |
| 168 | Error: "", // 空字符串表示成功 |
| 169 | Path: path, |
| 170 | BytesWritten: int64(len(content)), |
| 171 | FilesUpdate: map[string]any{ |
| 172 | path: data, |
| 173 | }, |
| 174 | }, nil |
| 175 | } |
| 176 | |
| 177 | // Edit 实现 BackendProtocol.Edit |
| 178 | func (b *StateBackend) Edit(ctx context.Context, path, oldStr, newStr string, replaceAll bool) (*EditResult, error) { |
no outgoing calls