summaryrefslogtreecommitdiff
blob: e8ab0b070a0c650db7cb9946a76f5f8de63a7d3d (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
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
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
/******************************* LICENCE **************************************
* Any code in this file may be redistributed or modified under the terms of
* the GNU General Public Licence as published by the Free Software 
* Foundation; version 2 of the licence.
****************************** END LICENCE ***********************************/

/******************************************************************************
* Author:
* Andrew Smith, http://littlesvr.ca/misc/contactandrew.php
*
* Contributors:
* 
******************************************************************************/

#include <strings.h>
#include <stdlib.h>
#include <string.h>
#include <sys/types.h>
#include <stdio.h>

#include "bk.h"
#include "bkInternal.h"
#include "bkPath.h"
#include "bkError.h"
#include "bkDelete.h"

/*******************************************************************************
* bk_delete_boot_record()
* deletes whatever reference to a boot record volinfo has
* */
void bk_delete_boot_record(VolInfo* volInfo)
{
    volInfo->bootMediaType = BOOT_MEDIA_NONE;
    
    if(volInfo->bootRecordPathAndName != NULL)
    {
        free(volInfo->bootRecordPathAndName);
        volInfo->bootRecordPathAndName = NULL;
    }
}

int bk_delete(VolInfo* volInfo, const char* pathAndName)
{
    int rc;
    NewPath path;
    bool dirFound;
    BkDir* parentDir;
    
    rc = makeNewPathFromString(pathAndName, &path);
    if(rc <= 0)
    {
        freePathContents(&path);
        return rc;
    }
    
    if(path.numChildren == 0)
    {
        freePathContents(&path);
        return BKERROR_DELETE_ROOT;
    }
    
    /* i want the parent directory */
    path.numChildren--;
    dirFound = findDirByNewPath(&path, &(volInfo->dirTree), &parentDir);
    path.numChildren++;
    if(!dirFound)
    {
        freePathContents(&path);
        return BKERROR_DIR_NOT_FOUND_ON_IMAGE;
    }
    
    deleteNode(volInfo, parentDir, path.children[path.numChildren - 1]);
    
    freePathContents(&path);
    
    return 1;
}

void deleteNode(VolInfo* volInfo, BkDir* parentDir, char* nodeToDeleteName)
{
    BkFileBase** childPtr;
    BkFileBase* nodeToFree;
    
    childPtr = &(parentDir->children);
    while(*childPtr != NULL)
    {
        if( strcmp((*childPtr)->name, nodeToDeleteName) == 0 )
        {
            nodeToFree = *childPtr;
            
            *childPtr = (*childPtr)->next;
            
            if( IS_DIR(nodeToFree->posixFileMode) )
            {
                deleteDirContents(volInfo, BK_DIR_PTR(nodeToFree));
            }
            else if ( IS_REG_FILE(nodeToFree->posixFileMode) )
            {
                deleteRegFileContents(volInfo, BK_FILE_PTR(nodeToFree));
            }
            /* else the free below will be enough */
            
            free(nodeToFree);
            
            break;
        }
        
        childPtr = &((*childPtr)->next);
    }
}

/*******************************************************************************
* deleteDirContents()
* deletes all the contents of a directory
* recursive
* */
void deleteDirContents(VolInfo* volInfo, BkDir* dir)
{
    BkFileBase* child;
    BkFileBase* nextChild;
    
    child = dir->children;
    while(child != NULL)
    {
        nextChild = child->next;
        
        deleteNode(volInfo, dir, child->name);
        
        child = nextChild;
    }
}

/* delete the contents of the BkFile structure, not the actual file contents */
void deleteRegFileContents(VolInfo* volInfo, BkFile* file)
{
    if( file->onImage )
        free( file->pathAndName );
    
    /* check whether file is being used as a boot record */
    if(volInfo->bootMediaType != BOOT_MEDIA_NONE &&
       volInfo->bootMediaType == BOOT_MEDIA_NO_EMULATION)
    {
        if(volInfo->bootRecordIsVisible && 
           volInfo->bootRecordOnImage == file)
        {
            /* and stop using it. perhaps insert a hook here one day to
            * let the user know the boot record has been/will be deleted */
            bk_delete_boot_record(volInfo);
        }
    }
}