blob: d2d31cc43cc738579fcdd12b4f572eca29310a5e [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
Theodore Ts'od1154eb2011-09-18 17:34:37 -040012#include "config.h"
Theodore Ts'o19c78dc1997-04-29 16:17:09 +000013#include <stdio.h>
14#include <string.h>
Theodore Ts'o4cbe8af1997-08-10 23:07:40 +000015#if HAVE_UNISTD_H
Theodore Ts'o19c78dc1997-04-29 16:17:09 +000016#include <unistd.h>
Theodore Ts'o4cbe8af1997-08-10 23:07:40 +000017#endif
Theodore Ts'o19c78dc1997-04-29 16:17:09 +000018
Theodore Ts'ob5abe6f1998-01-19 14:47:53 +000019#include "ext2_fs.h"
Theodore Ts'o19c78dc1997-04-29 16:17:09 +000020#include "ext2fs.h"
21
22struct link_struct {
23 const char *name;
24 int namelen;
Theodore Ts'o31dbecd2001-01-11 04:54:39 +000025 ext2_ino_t inode;
Theodore Ts'o19c78dc1997-04-29 16:17:09 +000026 int flags;
Theodore Ts'o4e60fb62005-01-07 22:09:49 -050027 struct ext2_dir_entry *prev;
Theodore Ts'o19c78dc1997-04-29 16:17:09 +000028 int done;
Theodore Ts'oefc6f622008-08-27 23:07:54 -040029};
Theodore Ts'o19c78dc1997-04-29 16:17:09 +000030
Theodore Ts'o3cb6c501997-08-11 20:29:22 +000031#ifdef __TURBOC__
Theodore Ts'o31dbecd2001-01-11 04:54:39 +000032 #pragma argsused
Theodore Ts'o3cb6c501997-08-11 20:29:22 +000033#endif
Theodore Ts'o19c78dc1997-04-29 16:17:09 +000034static int unlink_proc(struct ext2_dir_entry *dirent,
Theodore Ts'o7e5a86a2007-12-01 07:08:45 -050035 int offset,
Theodore Ts'o54434922003-12-07 01:28:50 -050036 int blocksize EXT2FS_ATTR((unused)),
37 char *buf EXT2FS_ATTR((unused)),
Theodore Ts'ob5abe6f1998-01-19 14:47:53 +000038 void *priv_data)
Theodore Ts'o19c78dc1997-04-29 16:17:09 +000039{
Theodore Ts'ob5abe6f1998-01-19 14:47:53 +000040 struct link_struct *ls = (struct link_struct *) priv_data;
Theodore Ts'o4e60fb62005-01-07 22:09:49 -050041 struct ext2_dir_entry *prev;
Theodore Ts'o19c78dc1997-04-29 16:17:09 +000042
Theodore Ts'o4e60fb62005-01-07 22:09:49 -050043 prev = ls->prev;
44 ls->prev = dirent;
Theodore Ts'o19c78dc1997-04-29 16:17:09 +000045
Theodore Ts'o4e60fb62005-01-07 22:09:49 -050046 if (ls->name) {
47 if ((dirent->name_len & 0xFF) != ls->namelen)
48 return 0;
49 if (strncmp(ls->name, dirent->name, dirent->name_len & 0xFF))
50 return 0;
51 }
52 if (ls->inode) {
53 if (dirent->inode != ls->inode)
54 return 0;
55 } else {
56 if (!dirent->inode)
57 return 0;
58 }
59
Theodore Ts'o7e5a86a2007-12-01 07:08:45 -050060 if (offset)
Theodore Ts'o4e60fb62005-01-07 22:09:49 -050061 prev->rec_len += dirent->rec_len;
62 else
63 dirent->inode = 0;
Theodore Ts'o19c78dc1997-04-29 16:17:09 +000064 ls->done++;
65 return DIRENT_ABORT|DIRENT_CHANGED;
66}
67
Theodore Ts'o3cb6c501997-08-11 20:29:22 +000068#ifdef __TURBOC__
Theodore Ts'o31dbecd2001-01-11 04:54:39 +000069 #pragma argsused
Theodore Ts'o3cb6c501997-08-11 20:29:22 +000070#endif
Theodore Ts'o31dbecd2001-01-11 04:54:39 +000071errcode_t ext2fs_unlink(ext2_filsys fs, ext2_ino_t dir,
72 const char *name, ext2_ino_t ino,
Theodore Ts'o54434922003-12-07 01:28:50 -050073 int flags EXT2FS_ATTR((unused)))
Theodore Ts'o19c78dc1997-04-29 16:17:09 +000074{
75 errcode_t retval;
76 struct link_struct ls;
77
78 EXT2_CHECK_MAGIC(fs, EXT2_ET_MAGIC_EXT2FS_FILSYS);
79
Theodore Ts'o4e60fb62005-01-07 22:09:49 -050080 if (!name && !ino)
81 return EXT2_ET_INVALID_ARGUMENT;
82
Theodore Ts'o19c78dc1997-04-29 16:17:09 +000083 if (!(fs->flags & EXT2_FLAG_RW))
84 return EXT2_ET_RO_FILSYS;
85
86 ls.name = name;
87 ls.namelen = name ? strlen(name) : 0;
88 ls.inode = ino;
89 ls.flags = 0;
90 ls.done = 0;
Theodore Ts'o4e60fb62005-01-07 22:09:49 -050091 ls.prev = 0;
Theodore Ts'o19c78dc1997-04-29 16:17:09 +000092
Theodore Ts'oefc6f622008-08-27 23:07:54 -040093 retval = ext2fs_dir_iterate(fs, dir, DIRENT_FLAG_INCLUDE_EMPTY,
Theodore Ts'o4e60fb62005-01-07 22:09:49 -050094 0, unlink_proc, &ls);
Theodore Ts'o19c78dc1997-04-29 16:17:09 +000095 if (retval)
96 return retval;
97
98 return (ls.done) ? 0 : EXT2_ET_DIR_NO_SPACE;
99}
100