VirtualBox

source: vbox/trunk/src/VBox/Runtime/generic/RTFileMove-generic.cpp@ 1507

Last change on this file since 1507 was 1, checked in by vboxsync, 55 years ago

import

  • Property svn:eol-style set to native
  • Property svn:keywords set to Id
File size: 3.6 KB
Line 
1/* $Id: RTFileMove-generic.cpp 1 1970-01-01 00:00:00Z vboxsync $ */
2/** @file
3 * InnoTek Portable Runtime - RTFileMove, Generic.
4 */
5
6/*
7 * Copyright (C) 2006 InnoTek Systemberatung GmbH
8 *
9 * This file is part of VirtualBox Open Source Edition (OSE), as
10 * available from http://www.virtualbox.org. This file is free software;
11 * you can redistribute it and/or modify it under the terms of the GNU
12 * General Public License as published by the Free Software Foundation,
13 * in version 2 as it comes in the "COPYING" file of the VirtualBox OSE
14 * distribution. VirtualBox OSE is distributed in the hope that it will
15 * be useful, but WITHOUT ANY WARRANTY of any kind.
16 *
17 * If you received this file as part of a commercial VirtualBox
18 * distribution, then only the terms of your commercial VirtualBox
19 * license agreement apply instead of the previous paragraph.
20 */
21
22#define LOG_GROUP RTLOGGROUP_FILE
23#include <iprt/file.h>
24#include <iprt/path.h>
25#include <iprt/err.h>
26#include <iprt/assert.h>
27#include <iprt/log.h>
28
29
30RTDECL(int) RTFileMove(const char *pszSrc, const char *pszDst, unsigned fMove)
31{
32 /*
33 * Validate input.
34 */
35 AssertMsgReturn(VALID_PTR(pszSrc), ("%p\n", pszSrc), VERR_INVALID_POINTER);
36 AssertMsgReturn(VALID_PTR(pszDst), ("%p\n", pszDst), VERR_INVALID_POINTER);
37 AssertMsgReturn(*pszSrc, ("%p\n", pszSrc), VERR_INVALID_PARAMETER);
38 AssertMsgReturn(*pszDst, ("%p\n", pszDst), VERR_INVALID_PARAMETER);
39 AssertMsgReturn(!(fMove & ~RTFILEMOVE_FLAGS_REPLACE), ("%#x\n", fMove), VERR_INVALID_PARAMETER);
40
41 /*
42 * Try RTFileRename first.
43 */
44 Assert(RTPATHRENAME_FLAGS_REPLACE == RTFILEMOVE_FLAGS_REPLACE);
45 unsigned fRename = fMove;
46 int rc = RTFileRename(pszSrc, pszDst, fRename);
47 if (rc == VERR_NOT_SAME_DEVICE)
48 {
49 const char *pszDelete = NULL;
50
51 /*
52 * The source and target are not on the same device, darn.
53 * We'll try open both ends and perform a copy.
54 */
55 RTFILE FileSrc;
56 rc = RTFileOpen(&FileSrc, pszSrc, RTFILE_O_READ | RTFILE_O_DENY_WRITE | RTFILE_O_OPEN);
57 if (RT_SUCCESS(rc))
58 {
59 RTFILE FileDst;
60 rc = RTFileOpen(&FileDst, pszDst, RTFILE_O_WRITE | RTFILE_O_DENY_ALL | RTFILE_O_CREATE_REPLACE);
61 if (RT_SUCCESS(rc))
62 {
63 rc = RTFileCopyByHandles(FileSrc, FileDst);
64 if (RT_SUCCESS(rc))
65 pszDelete = pszSrc;
66 else
67 {
68 pszDelete = pszDst;
69 Log(("RTFileMove('%s', '%s', %#x): copy failed, rc=%Rrc\n",
70 pszSrc, pszDst, fMove, rc));
71 }
72
73 /* try delete without closing, and could perhaps avoid some trouble */
74 int rc2 = RTFileDelete(pszDelete);
75 if (RT_SUCCESS(rc2))
76 pszDelete = NULL;
77 RTFileClose(FileDst);
78 }
79 else
80 Log(("RTFileMove('%s', '%s', %#x): failed to create destination, rc=%Rrc\n",
81 pszSrc, pszDst, fMove, rc));
82 RTFileClose(FileSrc);
83 }
84 else
85 Log(("RTFileMove('%s', '%s', %#x): failed to open source, rc=%Rrc\n",
86 pszSrc, pszDst, fMove, rc));
87
88 /* if we failed to close it while open, close it now */
89 if (pszDelete)
90 {
91 int rc2 = RTFileDelete(pszDelete);
92 if (RT_FAILURE(rc2))
93 Log(("RTFileMove('%s', '%s', %#x): failed to delete '%s', rc2=%Rrc (rc=%Rrc)\n",
94 pszSrc, pszDst, fMove, pszDelete, rc2, rc));
95 }
96 }
97
98 LogFlow(("RTDirRename(%p:{%s}, %p:{%s}, %#x): returns %Rrc\n",
99 pszSrc, pszSrc, pszDst, pszDst, fMove, rc));
100 return rc;
101}
102
Note: See TracBrowser for help on using the repository browser.

© 2024 Oracle Support Privacy / Do Not Sell My Info Terms of Use Trademark Policy Automated Access Etiquette