summaryrefslogtreecommitdiffstats
path: root/cpukit/libfs/src/imfs/imfs_rmnod.c
blob: c50041942f02395a9656288b61ce1d1a763cc3d5 (plain) (blame)
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
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
/*
 *  IMFS Node Removal Handler
 *
 *  This file contains the handler used to remove a node when a file type
 *  does not require special actions.
 *
 *  COPYRIGHT (c) 1989-1999.
 *  On-Line Applications Research Corporation (OAR).
 *
 *  Modifications to support reference counting in the file system are
 *  Copyright (c) 2012 embedded brains GmbH.
 *
 *  The license and distribution terms for this file may be
 *  found in the file LICENSE in this distribution or at
 *  http://www.rtems.com/license/LICENSE.
 *
 *  $Id$
 */

#if HAVE_CONFIG_H
  #include "config.h"
#endif

#include "imfs.h"

#include <stdlib.h>

void IMFS_create_orphan( IMFS_jnode_t *jnode )
{
  if ( jnode->Parent != NULL ) {
    rtems_chain_extract( &jnode->Node );
    jnode->Parent = NULL;
  }

  --jnode->st_nlink;

  IMFS_update_ctime( jnode );
}

void IMFS_check_node_remove( IMFS_jnode_t *jnode )
{
  if ( jnode->st_nlink < 1 ) {
    switch ( jnode->type ) {
      case IMFS_MEMORY_FILE:
        IMFS_memfile_remove( jnode );
        break;
      case IMFS_SYM_LINK:
        free( jnode->info.sym_link.name );
        break;
      default:
        break;
    }

    free( jnode );
  }
}

static int IMFS_rmnod_directory(
  const rtems_filesystem_location_info_t *parentloc,
  const rtems_filesystem_location_info_t *loc
)
{
  IMFS_jnode_t *node = loc->node_access;
  int rv = 0;

  if ( !rtems_chain_is_empty( &node->info.directory.Entries ) ) {
    errno = ENOTEMPTY;
    rv = -1;
  } else if (
    rtems_filesystem_location_is_root( loc )
      || node->info.directory.mt_fs != NULL
  ) {
    errno = EBUSY;
    rv = -1;
  }

  return rv;
}

static int IMFS_rmnod_hard_link(
  const rtems_filesystem_location_info_t *parentloc,
  const rtems_filesystem_location_info_t *loc
)
{
  int rv = 0;
  IMFS_jnode_t *node = loc->node_access;
  IMFS_jnode_t *target = node->info.hard_link.link_node;

  if ( target->st_nlink == 1) {
    rtems_filesystem_location_info_t target_loc = *loc;

    target_loc.node_access = target;
    IMFS_Set_handlers( &target_loc );

    rv = (*target_loc.ops->rmnod_h)( parentloc, &target_loc );
  } else {
    --target->st_nlink;
    IMFS_update_ctime( target );
  }

  return rv;
}

int IMFS_rmnod(
  const rtems_filesystem_location_info_t *parentloc,
  const rtems_filesystem_location_info_t *loc
)
{
  int rv = 0;
  IMFS_jnode_t *node = loc->node_access;

  switch ( node->type ) {
    case IMFS_DIRECTORY:
      rv = IMFS_rmnod_directory( parentloc, loc );
      break;
    case IMFS_HARD_LINK:
      rv = IMFS_rmnod_hard_link( parentloc, loc );
      break;
    default:
      break;
  }

  if ( rv == 0 ) {
    IMFS_create_orphan( node );
    IMFS_check_node_remove( node );
  }

  return rv;
}