Whamcloud - gitweb
ead5ec35009fb9a01a3b2239b6fe9d8b45fec62e
[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 #if EXT2_FLAT_INCLUDES
19 #include "ext2_fs.h"
20 #else
21 #include <linux/ext2_fs.h>
22 #endif
23
24 #include "ext2fs.h"
25
26 struct link_struct  {
27         const char      *name;
28         int             namelen;
29         ino_t           inode;
30         int             flags;
31         int             done;
32 };      
33
34 #ifdef __TURBOC__
35 #pragma argsused
36 #endif
37 static int unlink_proc(struct ext2_dir_entry *dirent,
38                      int        offset,
39                      int        blocksize,
40                      char       *buf,
41                      void       *priv_data)
42 {
43         struct link_struct *ls = (struct link_struct *) priv_data;
44
45         if (ls->name && ((dirent->name_len & 0xFF) != ls->namelen))
46                 return 0;
47         if (ls->name && strncmp(ls->name, dirent->name,
48                                 dirent->name_len & 0xFF))
49                 return 0;
50         if (ls->inode && (dirent->inode != ls->inode))
51                 return 0;
52
53         dirent->inode = 0;
54         ls->done++;
55         return DIRENT_ABORT|DIRENT_CHANGED;
56 }
57
58 #ifdef __TURBOC__
59 #pragma argsused
60 #endif
61 errcode_t ext2fs_unlink(ext2_filsys fs, ino_t dir, const char *name, ino_t ino,
62                         int flags)
63 {
64         errcode_t       retval;
65         struct link_struct ls;
66
67         EXT2_CHECK_MAGIC(fs, EXT2_ET_MAGIC_EXT2FS_FILSYS);
68
69         if (!(fs->flags & EXT2_FLAG_RW))
70                 return EXT2_ET_RO_FILSYS;
71
72         ls.name = name;
73         ls.namelen = name ? strlen(name) : 0;
74         ls.inode = ino;
75         ls.flags = 0;
76         ls.done = 0;
77
78         retval = ext2fs_dir_iterate(fs, dir, 0, 0, unlink_proc, &ls);
79         if (retval)
80                 return retval;
81
82         return (ls.done) ? 0 : EXT2_ET_DIR_NO_SPACE;
83 }
84