| 1387 | } |
| 1388 | |
| 1389 | int Ext2Volume::Unlink(Ext2Node* node, DirectoryEntry* ent, bool unlinkDirectories){ |
| 1390 | List<DirectoryEntry> entries; |
| 1391 | if(int e = ListDir(node, entries)){ |
| 1392 | Log::Error("[Ext2] Unlink: Error listing directory!", ent->inode); |
| 1393 | return e; |
| 1394 | } |
| 1395 | |
| 1396 | for(unsigned i = 0; i < entries.get_length(); i++) { |
| 1397 | if(strcmp(entries[i].name, ent->name) == 0){ |
| 1398 | ent->inode = entries.remove_at(i).inode; |
| 1399 | goto found; |
| 1400 | } |
| 1401 | } |
| 1402 | |
| 1403 | Log::Error("[Ext2] Unlink: Directory entry %s does not exist!", ent->name); |
| 1404 | return -ENOENT; |
| 1405 | |
| 1406 | found: |
| 1407 | if(!ent->inode){ |
| 1408 | Log::Error("[Ext2] Unlink: Invalid inode %d", ent->inode); |
| 1409 | return -EINVAL; |
| 1410 | } |
| 1411 | |
| 1412 | if(Ext2Node* file = inodeCache.get(ent->inode)){ |
| 1413 | if((file->flags & FS_NODE_TYPE) == FS_NODE_DIRECTORY){ |
| 1414 | if(!unlinkDirectories){ |
| 1415 | return -EISDIR; |
| 1416 | } |
| 1417 | } |
| 1418 | |
| 1419 | file->nlink--; |
| 1420 | file->e2inode.linkCount--; |
| 1421 | |
| 1422 | if(!file->handleCount){ |
| 1423 | inodeCache.remove(file->inode); |
| 1424 | CleanNode(file); |
| 1425 | } |
| 1426 | } else { |
| 1427 | ext2_inode_t e2inode; |
| 1428 | if(ReadInode(ent->inode, e2inode)){ |
| 1429 | Log::Error("[Ext2] Link: Error reading inode %d", ent->inode); |
| 1430 | return -1; |
| 1431 | } |
| 1432 | |
| 1433 | if((e2inode.mode & EXT2_S_IFMT) == EXT2_S_IFDIR){ |
| 1434 | if(!unlinkDirectories){ |
| 1435 | return -EISDIR; |
| 1436 | } |
| 1437 | } |
| 1438 | |
| 1439 | e2inode.linkCount--; |
| 1440 | |
| 1441 | if(e2inode.linkCount){ |
| 1442 | SyncInode(e2inode, ent->inode); |
| 1443 | } else { // Last link, delete inode |
| 1444 | EraseInode(e2inode, ent->inode); |
| 1445 | } |
| 1446 | } |
no test coverage detected