summaryrefslogtreecommitdiff
path: root/libdiskfs/dir-clear.c
blob: 7088afb31be4b970c20ad3832d775581e92a6079 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
/* 
   Copyright (C) 1994, 1995 Free Software Foundation

   This program is free software; you can redistribute it and/or
   modify it under the terms of the GNU General Public License as
   published by the Free Software Foundation; either version 2, or (at
   your option) any later version.

   This program is distributed in the hope that it will be useful, but
   WITHOUT ANY WARRANTY; without even the implied warranty of
   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
   General Public License for more details.

   You should have received a copy of the GNU General Public License
   along with this program; if not, write to the Free Software
   Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA. */

#include "priv.h"

/* Clear the `.' and `..' entries from directory DP.  Its parent is PDP,
   and the user responsible for this is identified by CRED.  Both 
   directories must be locked.  */
error_t
diskfs_clear_directory (struct node *dp,
			struct node *pdp,
			struct protid *cred)
{
  error_t err;
  struct dirstat *ds = alloca (diskfs_dirstat_size);
  struct node *np;

  /* Find and remove the `.' entry. */
  err = diskfs_lookup (dp, ".", REMOVE, &np, ds, cred);
  assert (err != ENOENT);
  if (!err)
    {
      assert (np == dp);
      diskfs_purge_cache_node (dp, np);
      err = diskfs_dirremove (dp, ds);
      diskfs_nrele (np);
    }
  else
    diskfs_drop_dirstat (dp, ds);
  if (err)
    return err;
  
  /* Decrement the link count */
  dp->dn_stat.st_nlink--;
  dp->dn_set_ctime = 1;

  /* Find and remove the `..' entry. */
  err = diskfs_lookup (dp, "..", REMOVE | SPEC_DOTDOT, &np, ds, cred);
  assert (err != ENOENT);
  if (!err)
    {
      assert (np == pdp);
      diskfs_purge_cache_node (dp, np);
      err = diskfs_dirremove (dp, ds);
    }
  else
    diskfs_drop_dirstat (dp, ds);
  if (err)
    return err;

  /* Decrement the link count on the parent */
  pdp->dn_stat.st_nlink--;
  pdp->dn_set_ctime = 1;

  diskfs_truncate (dp, 0);

  return err;
}