summaryrefslogtreecommitdiff
blob: 8bf0d364ba48ace088907bdf35c2173373351a2b (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
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
/******************************* 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 <stdio.h>
#include <string.h>
#include <sys/types.h>

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

/*******************************************************************************
* bk_estimate_iso_size()
* Public function
* Estimates the size of the directory trees + file contents on the iso
* */
bk_off_t bk_estimate_iso_size(const VolInfo* volInfo, int filenameTypes)
{
    /* reset alreadyCounted flags */
    BkHardLink* currentLink;
    currentLink = volInfo->fileLocations;
    while(currentLink != NULL)
    {
        currentLink->alreadyCounted = false;
        
        currentLink = currentLink->next;
    }
    
    return estimateIsoSize(&(volInfo->dirTree), filenameTypes);
}

/*******************************************************************************
* bk_get_creation_time()
* Public function
* */
time_t bk_get_creation_time(const VolInfo* volInfo)
{
    return volInfo->creationTime;
}

/*******************************************************************************
* bk_get_dir_from_string()
* public function
* gets a pointer to a Dir in tree described by the string pathStr
* */
int bk_get_dir_from_string(const VolInfo* volInfo, const char* pathStr, 
                           BkDir** dirFoundPtr)
{
    return getDirFromString(&(volInfo->dirTree), pathStr, dirFoundPtr);
}

/*******************************************************************************
* bk_get_permissions()
* public function
* gets the permissions (not all of the posix info) for an item (file, dir, etc.)
* */
int bk_get_permissions(VolInfo* volInfo, const char* pathAndName, 
                       mode_t* permissions)
{
    int rc;
    NewPath srcPath;
    BkFileBase* base;
    bool itemFound;
    
    if(permissions == NULL)
        return BKERROR_GET_PERM_BAD_PARAM;
    
    rc = makeNewPathFromString(pathAndName, &srcPath);
    if(rc <= 0)
    {
        freePathContents(&srcPath);
        return rc;
    }
    
    itemFound = findBaseByNewPath(&srcPath, &(volInfo->dirTree), &base);
    
    freePathContents(&srcPath);
    
    if(!itemFound)
        return BKERROR_ITEM_NOT_FOUND_ON_IMAGE;
    
    *permissions = base->posixFileMode & 0777;
    
    return 1;
}

/*******************************************************************************
* bk_get_publisher()
* Public function
* Returns a pointer to the string in volInfo that holds the volume name.
* */
const char* bk_get_publisher(const VolInfo* volInfo)
{
    return volInfo->publisher;
}

/*******************************************************************************
* bk_get_volume_name()
* Public function
* Returns a pointer to the string in volInfo that holds the volume name.
* */
const char* bk_get_volume_name(const VolInfo* volInfo)
{
    return volInfo->volId;
}

/*******************************************************************************
* estimateIsoSize()
* Recursive
* Estimate the size of the directory trees + file contents on the iso
* */
bk_off_t estimateIsoSize(const BkDir* tree, int filenameTypes)
{
    bk_off_t estimateDrSize;
    bk_off_t thisDirSize;
    int numItems; /* files and directories */
    BkFileBase* child;
    
    thisDirSize = 0;
    numItems = 0;
    
    child = tree->children;
    while(child != NULL)
    {
        if(IS_DIR(child->posixFileMode))
        {
            thisDirSize += estimateIsoSize(BK_DIR_PTR(child), filenameTypes);
        }
        else if(IS_REG_FILE(child->posixFileMode))
        {
            if(BK_FILE_PTR(child)->location == NULL ||
               !BK_FILE_PTR(child)->location->alreadyCounted)
            {
                thisDirSize += BK_FILE_PTR(child)->size;
                thisDirSize += BK_FILE_PTR(child)->size % NBYTES_LOGICAL_BLOCK;
            }
            if(BK_FILE_PTR(child)->location != NULL)
                BK_FILE_PTR(child)->location->alreadyCounted = true;
        }
        
        numItems++;
        
        child = child->next;
    }
    
    estimateDrSize = 70;
    if(filenameTypes & FNTYPE_JOLIET)
        estimateDrSize += 70;
    if(filenameTypes & FNTYPE_ROCKRIDGE)
        estimateDrSize += 70;
    
    thisDirSize += 68 + (estimateDrSize * numItems);
    thisDirSize += NBYTES_LOGICAL_BLOCK - (68 + (estimateDrSize * numItems)) % NBYTES_LOGICAL_BLOCK;
    
    return thisDirSize;
}

/*******************************************************************************
* getDirFromString()
* recursive
* gets a pointer to a Dir in tree described by the string pathStr
* */
int getDirFromString(const BkDir* tree, const char* pathStr, BkDir** dirFoundPtr)
{
    size_t count;
    size_t pathStrLen;
    bool stopLooking;
    /* name of the directory in the path this instance of the function works on */
    char* currentDirName;
    BkFileBase* child;
    int rc;
    
    pathStrLen = strlen(pathStr);
    
    if(pathStrLen == 1 && pathStr[0] == '/')
    /* root, special case */
    {
        /* cast to prevent compiler const warning */
        *dirFoundPtr = (BkDir*)tree;
        return 1;
    }
    
    if(pathStrLen < 3 || pathStr[0] != '/' || pathStr[1] == '/' || 
       pathStr[pathStrLen - 1] != '/')
        return BKERROR_MISFORMED_PATH;
    
    stopLooking = false;
    for(count = 2; count < pathStrLen && !stopLooking; count++)
    /* find the first directory in the path */
    {
        if(pathStr[count] == '/')
        /* found it */
        {
            /* make a copy of the string to use with strcmp */
            currentDirName = (char*)malloc(count);
            if(currentDirName == NULL)
                return BKERROR_OUT_OF_MEMORY;
            
            strncpy(currentDirName, &(pathStr[1]), count - 1);
            currentDirName[count - 1] = '\0';
            
            child = tree->children;
            while(child != NULL && !stopLooking)
            /* each child directory in tree */
            {
                if( strcmp(child->name, currentDirName) == 0 &&
                    IS_DIR(child->posixFileMode) )
                /* found the right child directory */
                {
                    if(pathStr[count + 1] == '\0')
                    /* this is the directory i'm looking for */
                    {
                        *dirFoundPtr = BK_DIR_PTR(child);
                        stopLooking = true;
                        rc = 1;
                    }
                    else
                    /* intermediate directory, go further down the tree */
                    {
                        rc = getDirFromString(BK_DIR_PTR(child), 
                                              &(pathStr[count]), dirFoundPtr);
                        if(rc <= 0)
                        {
                            free(currentDirName);
                            return rc;
                        }
                        stopLooking = true;
                    }
                        
                }
                
                child = child->next;
            }
            
            free(currentDirName);
            
            if(!stopLooking)
                return BKERROR_DIR_NOT_FOUND_ON_IMAGE;
        } /* if(found it) */
    } /* for(find the first directory in the path) */
    
    /* can't see how i could get here but to keep the compiler happy */
    return 1;
}