unlink.c revision 544349270e4c74a6feb971123884a8cf5052a7ee
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 "ext2_fs.h"
19#include "ext2fs.h"
20
21struct link_struct  {
22	const char	*name;
23	int		namelen;
24	ext2_ino_t	inode;
25	int		flags;
26	int		done;
27};
28
29#ifdef __TURBOC__
30 #pragma argsused
31#endif
32static int unlink_proc(struct ext2_dir_entry *dirent,
33		     int	offset EXT2FS_ATTR((unused)),
34		     int	blocksize EXT2FS_ATTR((unused)),
35		     char	*buf EXT2FS_ATTR((unused)),
36		     void	*priv_data)
37{
38	struct link_struct *ls = (struct link_struct *) priv_data;
39
40	if (ls->name && ((dirent->name_len & 0xFF) != ls->namelen))
41		return 0;
42	if (ls->name && strncmp(ls->name, dirent->name,
43				dirent->name_len & 0xFF))
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
56errcode_t ext2fs_unlink(ext2_filsys fs, ext2_ino_t dir,
57			const char *name, ext2_ino_t ino,
58			int flags EXT2FS_ATTR((unused)))
59{
60	errcode_t	retval;
61	struct link_struct ls;
62
63	EXT2_CHECK_MAGIC(fs, EXT2_ET_MAGIC_EXT2FS_FILSYS);
64
65	if (!(fs->flags & EXT2_FLAG_RW))
66		return EXT2_ET_RO_FILSYS;
67
68	ls.name = name;
69	ls.namelen = name ? strlen(name) : 0;
70	ls.inode = ino;
71	ls.flags = 0;
72	ls.done = 0;
73
74	retval = ext2fs_dir_iterate(fs, dir, 0, 0, unlink_proc, &ls);
75	if (retval)
76		return retval;
77
78	return (ls.done) ? 0 : EXT2_ET_DIR_NO_SPACE;
79}
80
81