* * @param: 文件描述符传来的InstanceSeq,要删除的文件的名字 * @return: 成功删除返回true,否则返回false * @notes: 需要检测name是否存在; */
(InstanceSeq uint64, filename string, opType int, checkSum uint64)
| 246 | * @notes: 需要检测name是否存在; |
| 247 | */ |
| 248 | func (Fsn *FileSystemNode) Delete(InstanceSeq uint64, filename string, opType int, checkSum uint64) error { |
| 249 | Node, IsExist := Fsn.next[filename] |
| 250 | if !IsExist { |
| 251 | log.Printf("INFO : %s/%s does not exist.\n", Fsn.nowPath, filename) |
| 252 | return ChubbyGoFileSystemError(PathError) |
| 253 | } |
| 254 | |
| 255 | if checkSum != Node.checksum { |
| 256 | log.Printf("WARNING : A danger requirment, unmatched checksum, now(%d) -> client(%d).\n", Node.checksum, checkSum) |
| 257 | return ChubbyGoFileSystemError(CheckSumError) |
| 258 | } |
| 259 | |
| 260 | if InstanceSeq < Node.instanceSeq { |
| 261 | log.Println("WARNING : Delete -> Request from a backward file descriptor!") |
| 262 | return ChubbyGoFileSystemError(InstanceSeqError) |
| 263 | } |
| 264 | |
| 265 | if Node.fileType == Directory && len(Fsn.next) != 0 { |
| 266 | log.Println("WARNING : Recursive deletion of files is currently not allowed!") |
| 267 | return ChubbyGoFileSystemError(DoesNotSupportRecursiveDeletion) // TODO 目前不支持这种递归删除,因为下面文件可能还持有锁,这个后面再说 |
| 268 | } |
| 269 | |
| 270 | if Node.OpenReferenceCount <= 0 { |
| 271 | log.Printf("ERROR : Delete a file(%s/%s) that referenceCount is zero.\n", Fsn.nowPath, filename) |
| 272 | return ChubbyGoFileSystemError(CannotDeleteFilesWithZeroReferenceCount) |
| 273 | } |
| 274 | |
| 275 | Node.OpenReferenceCount-- |
| 276 | |
| 277 | // close :引用计数为零时只有临时文件会被删除,永久文件和目录文件都不会被删除 |
| 278 | // delete: 相反 |
| 279 | if Node.OpenReferenceCount != 0 { |
| 280 | return nil // delete成功,其实只是把客户端的句柄消除掉而已 |
| 281 | } |
| 282 | |
| 283 | // name存在,引用计数为零且与远端InstanceSeq相等,可以执行删除 |
| 284 | if opType == Opdelete || Node.fileType == TemporaryFile { // 此次是delete操作,如果是close操作的话永久文件和目录则不需要删除 |
| 285 | delete(Fsn.next, filename) |
| 286 | Fsn.nextNameCache[filename]++ // 下一次创建的时候INstanceSeq与上一次不同 |
| 287 | } |
| 288 | |
| 289 | // 当文件的引用计数为零的时候更新Checksum,也就说下一次打开时得到的句柄是不一样的,可以有效防止客户端伪造checkSum |
| 290 | Node.checksum = Node.makeCheckSum() |
| 291 | |
| 292 | return nil |
| 293 | } |
| 294 | |
| 295 | /* |
| 296 | * @param: 文件描述符传来的InstanceSeq;要删除的文件的名字 |
nothing calls this directly
no test coverage detected