blob: 7ffeb9e8bfea31fa494d05d12055f71af6de5ce9 [file] [log] [blame]
Theodore Ts'o19c78dc1997-04-29 16:17:09 +00001/*
2 * unlink.c --- delete links in a ext2fs directory
Theodore Ts'oefc6f622008-08-27 23:07:54 -04003 *
Theodore Ts'o19c78dc1997-04-29 16:17:09 +00004 * Copyright (C) 1993, 1994, 1997 Theodore Ts'o.
5 *
6 * %Begin-Header%
Theodore Ts'o543547a2010-05-17 21:31:56 -04007 * This file may be redistributed under the terms of the GNU Library
8 * General Public License, version 2.
Theodore Ts'o19c78dc1997-04-29 16:17:09 +00009 * %End-Header%
10 */
11
12#include <stdio.h>
13#include <string.h>
Theodore Ts'o4cbe8af1997-08-10 23:07:40 +000014#if HAVE_UNISTD_H
Theodore Ts'o19c78dc1997-04-29 16:17:09 +000015#include <unistd.h>
Theodore Ts'o4cbe8af1997-08-10 23:07:40 +000016#endif
Theodore Ts'o19c78dc1997-04-29 16:17:09 +000017
Theodore Ts'ob5abe6f1998-01-19 14:47:53 +000018#include "ext2_fs.h"
Theodore Ts'o19c78dc1997-04-29 16:17:09 +000019#include "ext2fs.h"
20
21struct link_struct {
22 const char *name;
23 int namelen;
Theodore Ts'o31dbecd2001-01-11 04:54:39 +000024 ext2_ino_t inode;
Theodore Ts'o19c78dc1997-04-29 16:17:09 +000025 int flags;
Theodore Ts'o4e60fb62005-01-07 22:09:49 -050026 struct ext2_dir_entry *prev;
Theodore Ts'o19c78dc1997-04-29 16:17:09 +000027 int done;
Theodore Ts'oefc6f622008-08-27 23:07:54 -040028};
Theodore Ts'o19c78dc1997-04-29 16:17:09 +000029
Theodore Ts'o3cb6c501997-08-11 20:29:22 +000030#ifdef __TURBOC__
Theodore Ts'o31dbecd2001-01-11 04:54:39 +000031 #pragma argsused
Theodore Ts'o3cb6c501997-08-11 20:29:22 +000032#endif
Theodore Ts'o19c78dc1997-04-29 16:17:09 +000033static int unlink_proc(struct ext2_dir_entry *dirent,
Theodore Ts'o7e5a86a2007-12-01 07:08:45 -050034 int offset,
Theodore Ts'o54434922003-12-07 01:28:50 -050035 int blocksize EXT2FS_ATTR((unused)),
36 char *buf EXT2FS_ATTR((unused)),
Theodore Ts'ob5abe6f1998-01-19 14:47:53 +000037 void *priv_data)
Theodore Ts'o19c78dc1997-04-29 16:17:09 +000038{
Theodore Ts'ob5abe6f1998-01-19 14:47:53 +000039 struct link_struct *ls = (struct link_struct *) priv_data;
Theodore Ts'o4e60fb62005-01-07 22:09:49 -050040 struct ext2_dir_entry *prev;
Theodore Ts'o19c78dc1997-04-29 16:17:09 +000041
Theodore Ts'o4e60fb62005-01-07 22:09:49 -050042 prev = ls->prev;
43 ls->prev = dirent;
Theodore Ts'o19c78dc1997-04-29 16:17:09 +000044
Theodore Ts'o4e60fb62005-01-07 22:09:49 -050045 if (ls->name) {
46 if ((dirent->name_len & 0xFF) != ls->namelen)
47 return 0;
48 if (strncmp(ls->name, dirent->name, dirent->name_len & 0xFF))
49 return 0;
50 }
51 if (ls->inode) {
52 if (dirent->inode != ls->inode)
53 return 0;
54 } else {
55 if (!dirent->inode)
56 return 0;
57 }
58
Theodore Ts'o7e5a86a2007-12-01 07:08:45 -050059 if (offset)
Theodore Ts'o4e60fb62005-01-07 22:09:49 -050060 prev->rec_len += dirent->rec_len;
61 else
62 dirent->inode = 0;
Theodore Ts'o19c78dc1997-04-29 16:17:09 +000063 ls->done++;
64 return DIRENT_ABORT|DIRENT_CHANGED;
65}
66
Theodore Ts'o3cb6c501997-08-11 20:29:22 +000067#ifdef __TURBOC__
Theodore Ts'o31dbecd2001-01-11 04:54:39 +000068 #pragma argsused
Theodore Ts'o3cb6c501997-08-11 20:29:22 +000069#endif
Theodore Ts'o31dbecd2001-01-11 04:54:39 +000070errcode_t ext2fs_unlink(ext2_filsys fs, ext2_ino_t dir,
71 const char *name, ext2_ino_t ino,
Theodore Ts'o54434922003-12-07 01:28:50 -050072 int flags EXT2FS_ATTR((unused)))
Theodore Ts'o19c78dc1997-04-29 16:17:09 +000073{
74 errcode_t retval;
75 struct link_struct ls;
76
77 EXT2_CHECK_MAGIC(fs, EXT2_ET_MAGIC_EXT2FS_FILSYS);
78
Theodore Ts'o4e60fb62005-01-07 22:09:49 -050079 if (!name && !ino)
80 return EXT2_ET_INVALID_ARGUMENT;
81
Theodore Ts'o19c78dc1997-04-29 16:17:09 +000082 if (!(fs->flags & EXT2_FLAG_RW))
83 return EXT2_ET_RO_FILSYS;
84
85 ls.name = name;
86 ls.namelen = name ? strlen(name) : 0;
87 ls.inode = ino;
88 ls.flags = 0;
89 ls.done = 0;
Theodore Ts'o4e60fb62005-01-07 22:09:49 -050090 ls.prev = 0;
Theodore Ts'o19c78dc1997-04-29 16:17:09 +000091
Theodore Ts'oefc6f622008-08-27 23:07:54 -040092 retval = ext2fs_dir_iterate(fs, dir, DIRENT_FLAG_INCLUDE_EMPTY,
Theodore Ts'o4e60fb62005-01-07 22:09:49 -050093 0, unlink_proc, &ls);
Theodore Ts'o19c78dc1997-04-29 16:17:09 +000094 if (retval)
95 return retval;
96
97 return (ls.done) ? 0 : EXT2_ET_DIR_NO_SPACE;
98}
99