Whamcloud - gitweb
Many files:
[tools/e2fsprogs.git] / lib / ext2fs / unlink.c
1 /*
2  * unlink.c --- delete links in a ext2fs directory
3  * 
4  * Copyright (C) 1993, 1994, 1997 Theodore Ts'o.
5  *
6  * %Begin-Header%
7  * This file may be redistributed under the terms of the GNU Public
8  * License.
9  * %End-Header%
10  */
11
12 #include <stdio.h>
13 #include <string.h>
14 #if HAVE_UNISTD_H
15 #include <unistd.h>
16 #endif
17
18 #include <linux/ext2_fs.h>
19
20 #include "ext2fs.h"
21
22 struct link_struct  {
23         const char      *name;
24         int             namelen;
25         ino_t           inode;
26         int             flags;
27         int             done;
28 };      
29
30 #ifdef __TURBOC__
31 #pragma argsused
32 #endif
33 static int unlink_proc(struct ext2_dir_entry *dirent,
34                      int        offset,
35                      int        blocksize,
36                      char       *buf,
37                      void       *private)
38 {
39         struct link_struct *ls = (struct link_struct *) private;
40
41         if (ls->name && (dirent->name_len != ls->namelen))
42                 return 0;
43         if (ls->name && strncmp(ls->name, dirent->name, dirent->name_len))
44                 return 0;
45         if (ls->inode && (dirent->inode != ls->inode))
46                 return 0;
47
48         dirent->inode = 0;
49         ls->done++;
50         return DIRENT_ABORT|DIRENT_CHANGED;
51 }
52
53 #ifdef __TURBOC__
54 #pragma argsused
55 #endif
56 errcode_t ext2fs_unlink(ext2_filsys fs, ino_t dir, const char *name, ino_t ino,
57                         int flags)
58 {
59         errcode_t       retval;
60         struct link_struct ls;
61
62         EXT2_CHECK_MAGIC(fs, EXT2_ET_MAGIC_EXT2FS_FILSYS);
63
64         if (!(fs->flags & EXT2_FLAG_RW))
65                 return EXT2_ET_RO_FILSYS;
66
67         ls.name = name;
68         ls.namelen = name ? strlen(name) : 0;
69         ls.inode = ino;
70         ls.flags = 0;
71         ls.done = 0;
72
73         retval = ext2fs_dir_iterate(fs, dir, 0, 0, unlink_proc, &ls);
74         if (retval)
75                 return retval;
76
77         return (ls.done) ? 0 : EXT2_ET_DIR_NO_SPACE;
78 }
79