summaryrefslogtreecommitdiffstats
path: root/src/VBox/Runtime/common/vfs/vfsfss2dir.cpp
blob: 33de3f5a4a540ac67f71e1e0967776248fd98471 (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
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
/* $Id: vfsfss2dir.cpp $ */
/** @file
 * IPRT - Virtual File System, FS write stream dumping in a normal directory.
 *
 * This is just a simple mechanism to provide a drop in for the TAR  creator
 * that writes files individually to the disk instead of a TAR archive.  It
 * has an additional feature for removing the files to help bail out on error.
 */

/*
 * Copyright (C) 2010-2020 Oracle Corporation
 *
 * This file is part of VirtualBox Open Source Edition (OSE), as
 * available from http://www.virtualbox.org. This file is free software;
 * you can redistribute it and/or modify it under the terms of the GNU
 * General Public License (GPL) as published by the Free Software
 * Foundation, in version 2 as it comes in the "COPYING" file of the
 * VirtualBox OSE distribution. VirtualBox OSE is distributed in the
 * hope that it will be useful, but WITHOUT ANY WARRANTY of any kind.
 *
 * The contents of this file may alternatively be used under the terms
 * of the Common Development and Distribution License Version 1.0
 * (CDDL) only, as it comes in the "COPYING.CDDL" file of the
 * VirtualBox OSE distribution, in which case the provisions of the
 * CDDL are applicable instead of those of the GPL.
 *
 * You may elect to license modified versions of this file under the
 * terms and conditions of either the GPL or the CDDL or both.
 */


/*********************************************************************************************************************************
*   Header Files                                                                                                                 *
*********************************************************************************************************************************/
/// @todo #define RTVFSFSS2DIR_USE_DIR
#include "internal/iprt.h"
#include <iprt/vfs.h>

#include <iprt/assert.h>
#include <iprt/err.h>
#include <iprt/file.h>
#include <iprt/mem.h>
#ifndef RTVFSFSS2DIR_USE_DIR
# include <iprt/path.h>
#endif
#include <iprt/string.h>
#include <iprt/vfslowlevel.h>


/*********************************************************************************************************************************
*   Structures and Typedefs                                                                                                      *
*********************************************************************************************************************************/
/**
 * Undo entry for RTVFSFSSWRITE2DIR.
 */
typedef struct RTVFSFSSWRITE2DIRENTRY
{
    /** The list entry (head is RTVFSFSSWRITE2DIR::Entries). */
    RTLISTNODE      Entry;
    /** The file mode mask. */
    RTFMODE         fMode;
#ifdef RTVFSFSS2DIR_USE_DIR
    /** The name (relative to RTVFSFSSWRITE2DIR::hVfsBaseDir). */
#else
    /** The name (relative to RTVFSFSSWRITE2DIR::szBaseDir). */
#endif
    RT_FLEXIBLE_ARRAY_EXTENSION
    char            szName[RT_FLEXIBLE_ARRAY];
} RTVFSFSSWRITE2DIRENTRY;
/** Pointer to a RTVFSFSSWRITE2DIR undo entry. */
typedef RTVFSFSSWRITE2DIRENTRY *PRTVFSFSSWRITE2DIRENTRY;

/**
 * FSS write to directory instance.
 */
typedef struct RTVFSFSSWRITE2DIR
{
    /** Flags (RTVFSFSS2DIR_F_XXX). */
    uint32_t        fFlags;
    /** Number of files and stuff we've created. */
    uint32_t        cEntries;
    /** Files and stuff we've created (RTVFSFSSWRITE2DIRENTRY).
     * This is used for reverting changes on failure. */
    RTLISTANCHOR    Entries;
#ifdef RTVFSFSS2DIR_USE_DIR
    /** The handle of the base directory. */
    RTVFSDIR        hVfsBaseDir;
#else
    /** Path to the directory that all operations are relative to. */
    RT_FLEXIBLE_ARRAY_EXTENSION
    char            szBaseDir[RT_FLEXIBLE_ARRAY];
#endif
} RTVFSFSSWRITE2DIR;
/** Pointer to a write-to-directory FSS instance. */
typedef RTVFSFSSWRITE2DIR *PRTVFSFSSWRITE2DIR;


/*********************************************************************************************************************************
*   Internal Functions                                                                                                           *
*********************************************************************************************************************************/
static DECLCALLBACK(int) rtVfsFssToDir_PushFile(void *pvThis, const char *pszPath, uint64_t cbFile, PCRTFSOBJINFO paObjInfo,
                                                uint32_t cObjInfo, uint32_t fFlags, PRTVFSIOSTREAM phVfsIos);


/**
 * @interface_method_impl{RTVFSOBJOPS,pfnClose}
 */
static DECLCALLBACK(int) rtVfsFssToDir_Close(void *pvThis)
{
    PRTVFSFSSWRITE2DIR pThis = (PRTVFSFSSWRITE2DIR)pvThis;

#ifdef RTVFSFSS2DIR_USE_DIR
    RTVfsDirRelease(pThis->hVfsBaseDir);
    pThis->hVfsBaseDir = NIL_RTVFSDIR;
#endif

    PRTVFSFSSWRITE2DIRENTRY pCur;
    PRTVFSFSSWRITE2DIRENTRY pNext;
    RTListForEachSafe(&pThis->Entries, pCur, pNext, RTVFSFSSWRITE2DIRENTRY, Entry)
    {
        RTMemFree(pCur);
    }
    return VINF_SUCCESS;
}


/**
 * @interface_method_impl{RTVFSOBJOPS,pfnQueryInfo}
 */
static DECLCALLBACK(int) rtVfsFssToDir_QueryInfo(void *pvThis, PRTFSOBJINFO pObjInfo, RTFSOBJATTRADD enmAddAttr)
{
    RT_NOREF(pvThis);

    /* no info here, sorry. */
    RT_ZERO(*pObjInfo);
    pObjInfo->Attr.enmAdditional = enmAddAttr;

    return VINF_SUCCESS;
}


/**
 * @interface_method_impl{RTVFSFSSTREAMOPS,pfnAdd}
 */
static DECLCALLBACK(int) rtVfsFssToDir_Add(void *pvThis, const char *pszPath, RTVFSOBJ hVfsObj, uint32_t fFlags)
{
    PRTVFSFSSWRITE2DIR pThis = (PRTVFSFSSWRITE2DIR)pvThis;
    RT_NOREF(fFlags);

    /*
     * Query information about the object.
     */
    RTFSOBJINFO ObjInfo;
    int rc = RTVfsObjQueryInfo(hVfsObj, &ObjInfo, RTFSOBJATTRADD_UNIX);
    AssertRCReturn(rc, rc);

    /*
     * Deal with files.
     */
    if (RTFS_IS_FILE(ObjInfo.Attr.fMode))
    {
        RTVFSIOSTREAM hVfsIosSrc = RTVfsObjToIoStream(hVfsObj);
        AssertReturn(hVfsIosSrc != NIL_RTVFSIOSTREAM, VERR_WRONG_TYPE);

        RTVFSIOSTREAM hVfsIosDst;
        rc = rtVfsFssToDir_PushFile(pvThis, pszPath, ObjInfo.cbObject, &ObjInfo, 1, 0 /*fFlags*/, &hVfsIosDst);
        if (RT_SUCCESS(rc))
        {
            rc = RTVfsUtilPumpIoStreams(hVfsIosSrc, hVfsIosDst, (size_t)RT_ALIGN(ObjInfo.cbObject, _4K));
            RTVfsIoStrmRelease(hVfsIosDst);
        }
        RTVfsIoStrmRelease(hVfsIosSrc);
    }
    /*
     * Symbolic links.
     */
    else if (RTFS_IS_SYMLINK(ObjInfo.Attr.fMode))
    {
        RTVFSSYMLINK hVfsSymlink = RTVfsObjToSymlink(hVfsObj);
        AssertReturn(hVfsSymlink != NIL_RTVFSSYMLINK, VERR_WRONG_TYPE);

        AssertFailedStmt(rc = VERR_NOT_IMPLEMENTED);
        RT_NOREF(pThis);

        RTVfsSymlinkRelease(hVfsSymlink);
    }
    /*
     * Directories.
     */
    else if (RTFS_IS_DIRECTORY(ObjInfo.Attr.fMode))
        AssertFailedStmt(rc = VERR_NOT_IMPLEMENTED);
    /*
     * And whatever else we need when we need it...
     */
    else
        AssertFailedStmt(rc = VERR_NOT_IMPLEMENTED);

    return rc;
}


/**
 * @interface_method_impl{RTVFSFSSTREAMOPS,pfnPushFile}
 */
static DECLCALLBACK(int) rtVfsFssToDir_PushFile(void *pvThis, const char *pszPath, uint64_t cbFile, PCRTFSOBJINFO paObjInfo,
                                                uint32_t cObjInfo, uint32_t fFlags, PRTVFSIOSTREAM phVfsIos)
{
    PRTVFSFSSWRITE2DIR pThis = (PRTVFSFSSWRITE2DIR)pvThis;
    RT_NOREF(cbFile, fFlags);
    int rc;

#ifndef RTVFSFSS2DIR_USE_DIR
    /*
     * Join up the path with the base dir and make sure it fits.
     */
    char szFullPath[RTPATH_MAX];
    rc = RTPathJoin(szFullPath, sizeof(szFullPath), pThis->szBaseDir, pszPath);
    if (RT_SUCCESS(rc))
    {
#endif
        /*
         * Create an undo entry for it.
         */
        size_t const            cbRelativePath = strlen(pszPath);
        PRTVFSFSSWRITE2DIRENTRY pEntry;
        pEntry = (PRTVFSFSSWRITE2DIRENTRY)RTMemAllocVar(RT_UOFFSETOF_DYN(RTVFSFSSWRITE2DIRENTRY, szName[cbRelativePath]));
        if (pEntry)
        {
            if (cObjInfo)
                pEntry->fMode = (paObjInfo[0].Attr.fMode & ~RTFS_TYPE_MASK) | RTFS_TYPE_FILE;
            else
                pEntry->fMode = RTFS_TYPE_FILE | 0664;
            memcpy(pEntry->szName, pszPath, cbRelativePath);

            /*
             * Create the file.
             */
            uint64_t fOpen = RTFILE_O_WRITE | RTFILE_O_DENY_WRITE;
            fOpen |= ((pEntry->fMode & RTFS_UNIX_ALL_ACCESS_PERMS) << RTFILE_O_CREATE_MODE_SHIFT);
            if (!(pThis->fFlags & RTVFSFSS2DIR_F_OVERWRITE_FILES))
                fOpen |= RTFILE_O_CREATE;
            else
                fOpen |= RTFILE_O_CREATE_REPLACE;
#ifdef RTVFSFSS2DIR_USE_DIR
            rc = RTVfsDirOpenFileAsIoStream(pThis->hVfsBaseDir, pszPath, fOpen, phVfsIos);
#else
            rc = RTVfsIoStrmOpenNormal(szFullPath, fOpen, phVfsIos);
#endif
            if (RT_SUCCESS(rc))
                RTListAppend(&pThis->Entries, &pEntry->Entry);
            else
                RTMemFree(pEntry);
        }
        else
            rc = VERR_NO_MEMORY;
#ifndef RTVFSFSS2DIR_USE_DIR
    }
    else if (rc == VERR_BUFFER_OVERFLOW)
        rc = VERR_FILENAME_TOO_LONG;
#endif
    return rc;
}


/**
 * @interface_method_impl{RTVFSFSSTREAMOPS,pfnEnd}
 */
static DECLCALLBACK(int) rtVfsFssToDir_End(void *pvThis)
{
    RT_NOREF(pvThis);
    return VINF_SUCCESS;
}


/**
 * The write-to-directory FSS operations.
 */
static const RTVFSFSSTREAMOPS g_rtVfsFssToDirOps =
{
    { /* Obj */
        RTVFSOBJOPS_VERSION,
        RTVFSOBJTYPE_FS_STREAM,
        "TarFsStreamWriter",
        rtVfsFssToDir_Close,
        rtVfsFssToDir_QueryInfo,
        RTVFSOBJOPS_VERSION
    },
    RTVFSFSSTREAMOPS_VERSION,
    0,
    NULL,
    rtVfsFssToDir_Add,
    rtVfsFssToDir_PushFile,
    rtVfsFssToDir_End,
    RTVFSFSSTREAMOPS_VERSION
};


#ifdef RTVFSFSS2DIR_USE_DIR
RTDECL(int) RTVfsFsStrmToDir(RTVFSDIR hVfsBaseDir, uint32_t fFlags, PRTVFSFSSTREAM phVfsFss)
{
    /*
     * Input validation.
     */
    AssertPtrReturn(phVfsFss, VERR_INVALID_HANDLE);
    *phVfsFss = NIL_RTVFSFSSTREAM;
    AssertReturn(!(fFlags & ~RTVFSFSS2DIR_F_VALID_MASK), VERR_INVALID_FLAGS);
    uint32_t cRefs = RTVfsDirRetain(hVfsBaseDir);
    AssertReturn(cRefs != UINT32_MAX, VERR_INVALID_HANDLE);

    /*
     * Create the file system stream handle and init our data.
     */
    PRTVFSFSSWRITE2DIR      pThis;
    RTVFSFSSTREAM           hVfsFss;
    int rc = RTVfsNewFsStream(&g_rtVfsFssToDirOps, sizeof(*pThis), NIL_RTVFS, NIL_RTVFSLOCK, false /*fReadOnly*/,
                              &hVfsFss, (void **)&pThis);
    if (RT_SUCCESS(rc))
    {
        pThis->fFlags       = fFlags;
        pThis->cEntries     = 0;
        pThis->hVfsBaseDir  = hVfsBaseDir;
        RTListInit(&pThis->Entries);

        *phVfsFss = hVfsFss;
        return VINF_SUCCESS;
    }
    RTVfsDirRelease(hVfsBaseDir);

}
#endif /* RTVFSFSS2DIR_USE_DIR */


RTDECL(int) RTVfsFsStrmToNormalDir(const char *pszBaseDir, uint32_t fFlags, PRTVFSFSSTREAM phVfsFss)
{
#ifdef RTVFSFSS2DIR_USE_DIR
    RTVFSDIR hVfsBaseDir;
    int rc = RTVfsDirOpenNormal(pszBaseDir, 0 /*fFlags*/, &hVfsBaseDir);
    if (RT_SUCCESS(rc))
    {
        rc = RTVfsFsStrmToDir(hVfsBaseDir, fFlags, phVfsFss);
        RTVfsDirRelease(hVfsBaseDir);
    }
#else

    /*
     * Input validation.
     */
    AssertPtrReturn(phVfsFss, VERR_INVALID_HANDLE);
    *phVfsFss = NIL_RTVFSFSSTREAM;
    AssertReturn(!(fFlags & ~RTVFSFSS2DIR_F_VALID_MASK), VERR_INVALID_FLAGS);
    AssertPtrReturn(pszBaseDir, VERR_INVALID_POINTER);
    AssertReturn(*pszBaseDir != '\0', VERR_INVALID_NAME);

    /*
     * Straighten the path and make sure it's an existing directory.
     */
    char szAbsPath[RTPATH_MAX];
    int rc = RTPathAbs(pszBaseDir, szAbsPath, sizeof(szAbsPath));
    if (RT_SUCCESS(rc))
    {
        RTFSOBJINFO ObjInfo;
        rc = RTPathQueryInfo(szAbsPath, &ObjInfo, RTFSOBJATTRADD_NOTHING);
        if (RT_SUCCESS(rc))
        {
            if (RTFS_IS_DIRECTORY(ObjInfo.Attr.fMode))
            {
                /*
                 * Create the file system stream handle and init our data.
                 */
                size_t const            cbBaseDir = strlen(szAbsPath) + 1;
                PRTVFSFSSWRITE2DIR      pThis;
                RTVFSFSSTREAM           hVfsFss;
                rc = RTVfsNewFsStream(&g_rtVfsFssToDirOps, RT_UOFFSETOF_DYN(RTVFSFSSWRITE2DIR, szBaseDir[cbBaseDir]),
                                      NIL_RTVFS, NIL_RTVFSLOCK, false /*fReadOnly*/, &hVfsFss, (void **)&pThis);
                if (RT_SUCCESS(rc))
                {
                    pThis->fFlags   = fFlags;
                    pThis->cEntries = 0;
                    RTListInit(&pThis->Entries);
                    memcpy(pThis->szBaseDir, szAbsPath, cbBaseDir);

                    *phVfsFss = hVfsFss;
                    return VINF_SUCCESS;
                }
            }
            else
                rc = VERR_NOT_A_DIRECTORY;
        }
    }
#endif
    return rc;
}


RTDECL(int) RTVfsFsStrmToDirUndo(RTVFSFSSTREAM hVfsFss)
{
    /*
     * Validate input.
     */
    PRTVFSFSSWRITE2DIR pThis = (PRTVFSFSSWRITE2DIR)RTVfsFsStreamToPrivate(hVfsFss, &g_rtVfsFssToDirOps);
    AssertReturn(pThis, VERR_WRONG_TYPE);

    /*
     * Do the job, in reverse order.  Dropping stuff we
     * successfully remove from the list.
     */
    int                   rc = VINF_SUCCESS;
    PRTVFSFSSWRITE2DIRENTRY pCur;
    PRTVFSFSSWRITE2DIRENTRY pPrev;
    RTListForEachReverseSafe(&pThis->Entries, pCur, pPrev, RTVFSFSSWRITE2DIRENTRY, Entry)
    {
#ifdef RTVFSFSS2DIR_USE_DIR
        int rc2 = RTVfsDirUnlinkEntry(pThis->hVfsBaseDir, pCur->szName);
#else
        char szFullPath[RTPATH_MAX];
        int rc2 = RTPathJoin(szFullPath, sizeof(szFullPath), pThis->szBaseDir, pCur->szName);
        AssertRC(rc2);
        if (RT_SUCCESS(rc2))
            rc2 = RTPathUnlink(szFullPath, 0 /*fUnlink*/);
#endif
        if (   RT_SUCCESS(rc2)
            || rc2 == VERR_PATH_NOT_FOUND
            || rc2 == VERR_FILE_NOT_FOUND
            || rc2 == VERR_NOT_FOUND)
        {
            RTListNodeRemove(&pCur->Entry);
            RTMemFree(pCur);
        }
        else if (RT_SUCCESS(rc))
            rc = rc2;
    }
    return rc;
}