unlink.c revision 31dbecd482405e0d3a67eb58e1a1c8cb9f2ad83e
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
26struct link_struct  {
27	const char	*name;
28	int		namelen;
29	ext2_ino_t	inode;
30	int		flags;
31	int		done;
32};
33
34#ifdef __TURBOC__
35 #pragma argsused
36#endif
37static 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
61errcode_t ext2fs_unlink(ext2_filsys fs, ext2_ino_t dir,
62			const char *name, ext2_ino_t ino,
63			int flags)
64{
65	errcode_t	retval;
66	struct link_struct ls;
67
68	EXT2_CHECK_MAGIC(fs, EXT2_ET_MAGIC_EXT2FS_FILSYS);
69
70	if (!(fs->flags & EXT2_FLAG_RW))
71		return EXT2_ET_RO_FILSYS;
72
73	ls.name = name;
74	ls.namelen = name ? strlen(name) : 0;
75	ls.inode = ino;
76	ls.flags = 0;
77	ls.done = 0;
78
79	retval = ext2fs_dir_iterate(fs, dir, 0, 0, unlink_proc, &ls);
80	if (retval)
81		return retval;
82
83	return (ls.done) ? 0 : EXT2_ET_DIR_NO_SPACE;
84}
85
86