xref: /AOO42X/main/tools/source/fsys/filecopy.cxx (revision b1c5455db1639c48e26c568e4fa7ee78ca5d60ee)
189b56da7SAndrew Rist /**************************************************************
2cdf0e10cSrcweir  *
389b56da7SAndrew Rist  * Licensed to the Apache Software Foundation (ASF) under one
489b56da7SAndrew Rist  * or more contributor license agreements.  See the NOTICE file
589b56da7SAndrew Rist  * distributed with this work for additional information
689b56da7SAndrew Rist  * regarding copyright ownership.  The ASF licenses this file
789b56da7SAndrew Rist  * to you under the Apache License, Version 2.0 (the
889b56da7SAndrew Rist  * "License"); you may not use this file except in compliance
989b56da7SAndrew Rist  * with the License.  You may obtain a copy of the License at
10cdf0e10cSrcweir  *
1189b56da7SAndrew Rist  *   http://www.apache.org/licenses/LICENSE-2.0
12cdf0e10cSrcweir  *
1389b56da7SAndrew Rist  * Unless required by applicable law or agreed to in writing,
1489b56da7SAndrew Rist  * software distributed under the License is distributed on an
1589b56da7SAndrew Rist  * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
1689b56da7SAndrew Rist  * KIND, either express or implied.  See the License for the
1789b56da7SAndrew Rist  * specific language governing permissions and limitations
1889b56da7SAndrew Rist  * under the License.
19cdf0e10cSrcweir  *
2089b56da7SAndrew Rist  *************************************************************/
2189b56da7SAndrew Rist 
2289b56da7SAndrew Rist 
23cdf0e10cSrcweir 
24cdf0e10cSrcweir // MARKER(update_precomp.py): autogen include statement, do not remove
25cdf0e10cSrcweir #include "precompiled_tools.hxx"
26cdf0e10cSrcweir 
27cdf0e10cSrcweir #if defined WNT
28cdf0e10cSrcweir #ifndef _SVWIN_H
29cdf0e10cSrcweir #include <io.h>
30cdf0e10cSrcweir #include <tools/svwin.h>
31cdf0e10cSrcweir #endif
32cdf0e10cSrcweir 
33cdf0e10cSrcweir #elif defined(OS2)
34cdf0e10cSrcweir #include <sys/types.h>
35cdf0e10cSrcweir #include <sys/stat.h>
36cdf0e10cSrcweir #include <fcntl.h>
37cdf0e10cSrcweir #include <share.h>
38cdf0e10cSrcweir #include <io.h>
39cdf0e10cSrcweir 
40cdf0e10cSrcweir #elif defined UNX
41cdf0e10cSrcweir #include <fcntl.h>
42cdf0e10cSrcweir #include <unistd.h>
43cdf0e10cSrcweir #include <sys/stat.h>
44cdf0e10cSrcweir 
45cdf0e10cSrcweir #endif
46cdf0e10cSrcweir 
47cdf0e10cSrcweir #include <ctype.h>
48cdf0e10cSrcweir #include <errno.h>
49cdf0e10cSrcweir #include <stdlib.h>
50cdf0e10cSrcweir #include <string.h>
51cdf0e10cSrcweir 
52cdf0e10cSrcweir #include <stdio.h>
53cdf0e10cSrcweir #include "comdep.hxx"
54cdf0e10cSrcweir #include <tools/fsys.hxx>
55cdf0e10cSrcweir #include <tools/stream.hxx>
56cdf0e10cSrcweir #include <osl/file.hxx>
57cdf0e10cSrcweir 
58cdf0e10cSrcweir using namespace ::osl;
59cdf0e10cSrcweir 
60cdf0e10cSrcweir /*************************************************************************
61cdf0e10cSrcweir |*
62cdf0e10cSrcweir |*    FileCopier::FileCopier()
63cdf0e10cSrcweir |*
64cdf0e10cSrcweir |*    Beschreibung      FSYS.SDW
65cdf0e10cSrcweir |*    Ersterstellung    MI 13.04.94
66cdf0e10cSrcweir |*    Letzte Aenderung  MI 13.04.94
67cdf0e10cSrcweir |*
68cdf0e10cSrcweir *************************************************************************/
69cdf0e10cSrcweir 
FileCopier()70cdf0e10cSrcweir FileCopier::FileCopier() :
71cdf0e10cSrcweir 
72cdf0e10cSrcweir     nBytesTotal ( 0 ),
73cdf0e10cSrcweir     nBytesCopied( 0 ),
74cdf0e10cSrcweir     nBlockSize  ( 4096 ),
75cdf0e10cSrcweir     pImp        ( new FileCopier_Impl )
76cdf0e10cSrcweir 
77cdf0e10cSrcweir {
78cdf0e10cSrcweir }
79cdf0e10cSrcweir 
80cdf0e10cSrcweir // -----------------------------------------------------------------------
81cdf0e10cSrcweir 
FileCopier(const DirEntry & rSource,const DirEntry & rTarget)82cdf0e10cSrcweir FileCopier::FileCopier( const DirEntry& rSource, const DirEntry& rTarget ) :
83cdf0e10cSrcweir 
84cdf0e10cSrcweir     aSource     ( rSource ),
85cdf0e10cSrcweir     aTarget     ( rTarget ),
86cdf0e10cSrcweir     nBytesTotal ( 0 ),
87cdf0e10cSrcweir     nBytesCopied( 0 ),
88cdf0e10cSrcweir     nBlockSize  ( 4096 ),
89cdf0e10cSrcweir     pImp        ( new FileCopier_Impl )
90cdf0e10cSrcweir 
91cdf0e10cSrcweir {
92cdf0e10cSrcweir }
93cdf0e10cSrcweir 
94cdf0e10cSrcweir // -----------------------------------------------------------------------
95cdf0e10cSrcweir 
FileCopier(const FileCopier & rCopier)96cdf0e10cSrcweir FileCopier::FileCopier( const FileCopier& rCopier ) :
97cdf0e10cSrcweir 
98cdf0e10cSrcweir     aSource         ( rCopier.aSource ),
99cdf0e10cSrcweir     aTarget         ( rCopier.aTarget ),
100cdf0e10cSrcweir     nBytesTotal     ( 0 ),
101cdf0e10cSrcweir     nBytesCopied    ( 0 ),
102cdf0e10cSrcweir     aProgressLink   ( rCopier.aProgressLink ),
103cdf0e10cSrcweir     nBlockSize      ( 4096 ),
104cdf0e10cSrcweir     pImp            ( new FileCopier_Impl )
105cdf0e10cSrcweir 
106cdf0e10cSrcweir {
107cdf0e10cSrcweir }
108cdf0e10cSrcweir 
109cdf0e10cSrcweir /*************************************************************************
110cdf0e10cSrcweir |*
111cdf0e10cSrcweir |*    FileCopier::~FileCopier()
112cdf0e10cSrcweir |*
113cdf0e10cSrcweir |*    Beschreibung      FSYS.SDW
114cdf0e10cSrcweir |*    Ersterstellung    MI 13.04.94
115cdf0e10cSrcweir |*    Letzte Aenderung  MI 13.04.94
116cdf0e10cSrcweir |*
117cdf0e10cSrcweir *************************************************************************/
118cdf0e10cSrcweir 
~FileCopier()119cdf0e10cSrcweir FileCopier::~FileCopier()
120cdf0e10cSrcweir {
121cdf0e10cSrcweir     delete pImp;
122cdf0e10cSrcweir }
123cdf0e10cSrcweir 
124cdf0e10cSrcweir /*************************************************************************
125cdf0e10cSrcweir |*
126cdf0e10cSrcweir |*    FileCopier::operator =()
127cdf0e10cSrcweir |*
128cdf0e10cSrcweir |*    Beschreibung      FSYS.SDW
129cdf0e10cSrcweir |*    Ersterstellung    MI 13.04.94
130cdf0e10cSrcweir |*    Letzte Aenderung  MI 13.04.94
131cdf0e10cSrcweir |*
132cdf0e10cSrcweir *************************************************************************/
133cdf0e10cSrcweir 
operator =(const FileCopier & rCopier)134cdf0e10cSrcweir FileCopier& FileCopier::operator = ( const FileCopier &rCopier )
135cdf0e10cSrcweir {
136cdf0e10cSrcweir     aSource = rCopier.aSource;
137cdf0e10cSrcweir     aTarget = rCopier.aTarget;
138cdf0e10cSrcweir     nBytesTotal = rCopier.nBytesTotal;
139cdf0e10cSrcweir     nBytesCopied = rCopier.nBytesCopied;
140cdf0e10cSrcweir     nBytesCopied = rCopier.nBytesCopied;
141cdf0e10cSrcweir     nBlockSize = rCopier.nBlockSize;
142cdf0e10cSrcweir     aProgressLink = rCopier.aProgressLink;
143cdf0e10cSrcweir     *pImp = *(rCopier.pImp);
144cdf0e10cSrcweir     return *this;
145cdf0e10cSrcweir }
146cdf0e10cSrcweir 
147cdf0e10cSrcweir /*************************************************************************
148cdf0e10cSrcweir |*
149cdf0e10cSrcweir |*    FileCopier::Progress()
150cdf0e10cSrcweir |*
151cdf0e10cSrcweir |*    Beschreibung      FSYS.SDW
152cdf0e10cSrcweir |*    Ersterstellung    MI 13.04.94
153cdf0e10cSrcweir |*    Letzte Aenderung  MI 13.04.94
154cdf0e10cSrcweir |*
155cdf0e10cSrcweir *************************************************************************/
156cdf0e10cSrcweir 
Progress()157cdf0e10cSrcweir sal_Bool FileCopier::Progress()
158cdf0e10cSrcweir {
159cdf0e10cSrcweir     if ( !aProgressLink )
160cdf0e10cSrcweir         return sal_True;
161cdf0e10cSrcweir     else
162cdf0e10cSrcweir     {
163cdf0e10cSrcweir         if ( aProgressLink.Call( this ) )
164cdf0e10cSrcweir             return sal_True;
165cdf0e10cSrcweir         return ( 0 == Error( ERRCODE_ABORT, 0, 0 ) );
166cdf0e10cSrcweir     }
167cdf0e10cSrcweir }
168cdf0e10cSrcweir 
169cdf0e10cSrcweir //---------------------------------------------------------------------------
170cdf0e10cSrcweir 
Error(ErrCode eErr,const DirEntry * pSource,const DirEntry * pTarget)171cdf0e10cSrcweir ErrCode FileCopier::Error( ErrCode eErr, const DirEntry* pSource, const DirEntry* pTarget )
172cdf0e10cSrcweir {
173cdf0e10cSrcweir     // kein Fehler oder kein ErrorHandler?
174cdf0e10cSrcweir     if ( !eErr || !pImp->aErrorLink )
175cdf0e10cSrcweir         // => Error beibehalten
176cdf0e10cSrcweir         return eErr;
177cdf0e10cSrcweir 
178cdf0e10cSrcweir     // sonst gesetzten ErrorHandler fragen
179cdf0e10cSrcweir     pImp->pErrSource = pSource;
180cdf0e10cSrcweir     pImp->pErrTarget = pTarget;
181cdf0e10cSrcweir     pImp->eErr = eErr;
182cdf0e10cSrcweir     ErrCode eRet = (ErrCode) pImp->aErrorLink.Call( this );
183cdf0e10cSrcweir     pImp->pErrSource = 0;
184cdf0e10cSrcweir     pImp->pErrTarget = 0;
185cdf0e10cSrcweir     return eRet;
186cdf0e10cSrcweir }
187cdf0e10cSrcweir 
188cdf0e10cSrcweir //---------------------------------------------------------------------------
189cdf0e10cSrcweir 
GetErrorSource() const190cdf0e10cSrcweir const DirEntry* FileCopier::GetErrorSource() const
191cdf0e10cSrcweir {
192cdf0e10cSrcweir     return pImp->pErrSource;
193cdf0e10cSrcweir }
194cdf0e10cSrcweir 
195cdf0e10cSrcweir //---------------------------------------------------------------------------
196cdf0e10cSrcweir 
GetErrorTarget() const197cdf0e10cSrcweir const DirEntry* FileCopier::GetErrorTarget() const
198cdf0e10cSrcweir {
199cdf0e10cSrcweir     return pImp->pErrTarget;
200cdf0e10cSrcweir }
201cdf0e10cSrcweir 
202cdf0e10cSrcweir //---------------------------------------------------------------------------
203cdf0e10cSrcweir 
GetError() const204cdf0e10cSrcweir ErrCode FileCopier::GetError() const
205cdf0e10cSrcweir {
206cdf0e10cSrcweir     return pImp->eErr;
207cdf0e10cSrcweir }
208cdf0e10cSrcweir 
209cdf0e10cSrcweir //---------------------------------------------------------------------------
210cdf0e10cSrcweir 
SetErrorHdl(const Link & rLink)211cdf0e10cSrcweir void FileCopier::SetErrorHdl( const Link &rLink )
212cdf0e10cSrcweir {
213cdf0e10cSrcweir     pImp->aErrorLink = rLink;
214cdf0e10cSrcweir }
215cdf0e10cSrcweir 
216cdf0e10cSrcweir //---------------------------------------------------------------------------
217cdf0e10cSrcweir 
GetErrorHdl() const218cdf0e10cSrcweir const Link& FileCopier::GetErrorHdl() const
219cdf0e10cSrcweir {
220cdf0e10cSrcweir     return pImp->aErrorLink ;
221cdf0e10cSrcweir }
222cdf0e10cSrcweir 
223cdf0e10cSrcweir /*************************************************************************
224cdf0e10cSrcweir |*
225cdf0e10cSrcweir |*    FileCopier::Execute()
226cdf0e10cSrcweir |*
227cdf0e10cSrcweir |*    Beschreibung      FSYS.SDW
228cdf0e10cSrcweir |*    Ersterstellung    MI 13.04.94
229cdf0e10cSrcweir |*    Letzte Aenderung  PB 16.06.00
230cdf0e10cSrcweir |*
231cdf0e10cSrcweir *************************************************************************/
232cdf0e10cSrcweir 
DoCopy_Impl(const DirEntry & rSource,const DirEntry & rTarget)233cdf0e10cSrcweir FSysError FileCopier::DoCopy_Impl(
234cdf0e10cSrcweir     const DirEntry &rSource, const DirEntry &rTarget )
235cdf0e10cSrcweir {
236cdf0e10cSrcweir     FSysError eRet = FSYS_ERR_OK;
237cdf0e10cSrcweir     ErrCode eWarn = FSYS_ERR_OK;
238cdf0e10cSrcweir 
239cdf0e10cSrcweir     // HPFS->FAT?
240cdf0e10cSrcweir     FSysPathStyle eSourceStyle = DirEntry::GetPathStyle( rSource.ImpGetTopPtr()->GetName() );
241cdf0e10cSrcweir     FSysPathStyle eTargetStyle = DirEntry::GetPathStyle( rTarget.ImpGetTopPtr()->GetName() );
242cdf0e10cSrcweir     sal_Bool bMakeShortNames = ( eSourceStyle == FSYS_STYLE_HPFS && eTargetStyle == FSYS_STYLE_FAT );
243cdf0e10cSrcweir 
244cdf0e10cSrcweir     // Zieldateiname ggf. kuerzen
245cdf0e10cSrcweir     DirEntry aTgt;
246cdf0e10cSrcweir     if ( bMakeShortNames )
247cdf0e10cSrcweir     {
248cdf0e10cSrcweir         aTgt = rTarget.GetPath();
249cdf0e10cSrcweir         aTgt.MakeShortName( rTarget.GetName() );
250cdf0e10cSrcweir     }
251cdf0e10cSrcweir     else
252cdf0e10cSrcweir         aTgt = rTarget;
253cdf0e10cSrcweir 
254cdf0e10cSrcweir     // kein Move wenn Namen gekuerzt werden muessten
255cdf0e10cSrcweir     if ( bMakeShortNames && FSYS_ACTION_MOVE == ( pImp->nActions & FSYS_ACTION_MOVE ) && aTgt != rTarget )
256cdf0e10cSrcweir         return ERRCODE_IO_NAMETOOLONG;
257cdf0e10cSrcweir 
258cdf0e10cSrcweir     // source is directory?
259cdf0e10cSrcweir     FileStat aSourceFileStat( rSource );
260cdf0e10cSrcweir     if ( aSourceFileStat.IsKind( FSYS_KIND_DIR ) )
261cdf0e10cSrcweir     {
262cdf0e10cSrcweir #ifdef OS2
263cdf0e10cSrcweir         CHAR szSource[CCHMAXPATHCOMP];
264cdf0e10cSrcweir         HOBJECT hSourceObject;
265cdf0e10cSrcweir 
266cdf0e10cSrcweir         strcpy(szSource, ByteString(rSource.GetFull(), osl_getThreadTextEncoding()).GetBuffer());
267cdf0e10cSrcweir         hSourceObject = WinQueryObject(szSource);
268cdf0e10cSrcweir 
269cdf0e10cSrcweir         if ( hSourceObject )
270cdf0e10cSrcweir         {
271cdf0e10cSrcweir             PSZ  pszSourceName;
272cdf0e10cSrcweir             PSZ  pszTargetName;
273cdf0e10cSrcweir             CHAR szTarget[CCHMAXPATHCOMP];
274cdf0e10cSrcweir             HOBJECT hTargetObject;
275cdf0e10cSrcweir             HOBJECT hReturn = NULLHANDLE;
276cdf0e10cSrcweir 
277cdf0e10cSrcweir             strcpy(szTarget, ByteString(rTarget.GetFull(), osl_getThreadTextEncoding()).GetBuffer());
278cdf0e10cSrcweir             pszTargetName = strrchr(szTarget, '\\');
279cdf0e10cSrcweir             pszSourceName = strrchr(szSource, '\\');
280cdf0e10cSrcweir 
281cdf0e10cSrcweir             hTargetObject = WinQueryObject(szTarget);
282cdf0e10cSrcweir 
283cdf0e10cSrcweir             if ( hTargetObject )
284cdf0e10cSrcweir                 WinDestroyObject(hTargetObject);
285cdf0e10cSrcweir 
286cdf0e10cSrcweir             if ( pszTargetName && pszSourceName )
287cdf0e10cSrcweir             {
288cdf0e10cSrcweir                 *pszTargetName = '\0';
289cdf0e10cSrcweir                 pszSourceName++;
290cdf0e10cSrcweir                 pszTargetName++;
291cdf0e10cSrcweir 
292cdf0e10cSrcweir                 if(strcmp(pszSourceName, pszTargetName) == 0)
293cdf0e10cSrcweir                 {
294cdf0e10cSrcweir                     hTargetObject = WinQueryObject(szTarget);
295cdf0e10cSrcweir 
296cdf0e10cSrcweir                     if(pImp->nActions & FSYS_ACTION_MOVE)
297cdf0e10cSrcweir                     {
298cdf0e10cSrcweir                         hReturn = WinMoveObject(hSourceObject, hTargetObject, 0);
299cdf0e10cSrcweir                     }
300cdf0e10cSrcweir                     else
301cdf0e10cSrcweir                     {
302cdf0e10cSrcweir                         hReturn = WinCopyObject(hSourceObject, hTargetObject, 0);
303cdf0e10cSrcweir                     }
304cdf0e10cSrcweir                     if ( bMakeShortNames && aTarget.Exists() )
305cdf0e10cSrcweir                         aTarget.Kill();
306cdf0e10cSrcweir                     return hReturn ? FSYS_ERR_OK : FSYS_ERR_UNKNOWN;
307cdf0e10cSrcweir                 }
308cdf0e10cSrcweir             }
309cdf0e10cSrcweir         }
310cdf0e10cSrcweir #endif
311cdf0e10cSrcweir         // recursive copy
312cdf0e10cSrcweir         eRet = Error( aTgt.MakeDir() ? FSYS_ERR_OK : FSYS_ERR_UNKNOWN, 0, &aTgt );
313cdf0e10cSrcweir         Dir aSourceDir( rSource, FSYS_KIND_DIR|FSYS_KIND_FILE );
314cdf0e10cSrcweir         for ( sal_uInt16 n = 0; ERRCODE_TOERROR(eRet) == FSYS_ERR_OK && n < aSourceDir.Count(); ++n )
315cdf0e10cSrcweir         {
316cdf0e10cSrcweir             const DirEntry &rSubSource = aSourceDir[n];
317cdf0e10cSrcweir             DirEntryFlag eFlag = rSubSource.GetFlag();
318cdf0e10cSrcweir             if ( eFlag != FSYS_FLAG_CURRENT && eFlag != FSYS_FLAG_PARENT )
319cdf0e10cSrcweir             {
320cdf0e10cSrcweir                 DirEntry aSubTarget( aTgt );
321cdf0e10cSrcweir                 aSubTarget += rSubSource.GetName();
322cdf0e10cSrcweir                 eRet = DoCopy_Impl( rSubSource, aSubTarget );
323cdf0e10cSrcweir                 if ( eRet && !eWarn )
324cdf0e10cSrcweir                 eWarn = eRet;
325cdf0e10cSrcweir             }
326cdf0e10cSrcweir         }
327cdf0e10cSrcweir     }
328cdf0e10cSrcweir     else if ( aSourceFileStat.IsKind(FSYS_KIND_FILE) )
329cdf0e10cSrcweir     {
330cdf0e10cSrcweir         if ( ( FSYS_ACTION_KEEP_EXISTING == ( pImp->nActions & FSYS_ACTION_KEEP_EXISTING ) ) &&
331cdf0e10cSrcweir              aTgt.Exists() )
332cdf0e10cSrcweir         {
333cdf0e10cSrcweir             // Do not overwrite existing file in target folder.
334cdf0e10cSrcweir             return ERRCODE_NONE;
335cdf0e10cSrcweir         }
336cdf0e10cSrcweir 
337cdf0e10cSrcweir         // copy file
338cdf0e10cSrcweir         nBytesCopied = 0;
339cdf0e10cSrcweir         nBytesTotal = FileStat( rSource ).GetSize();
340cdf0e10cSrcweir 
341cdf0e10cSrcweir         ::rtl::OUString aFileName;
342cdf0e10cSrcweir         FileBase::getFileURLFromSystemPath( ::rtl::OUString(rSource.GetFull()), aFileName );
343cdf0e10cSrcweir         SvFileStream aSrc( aFileName, STREAM_READ|STREAM_NOCREATE|STREAM_SHARE_DENYNONE );
344cdf0e10cSrcweir 
345cdf0e10cSrcweir         if ( !aSrc.GetError() )
346cdf0e10cSrcweir         {
347cdf0e10cSrcweir #ifdef UNX
348cdf0e10cSrcweir             struct stat buf;
349cdf0e10cSrcweir             if ( fstat( aSrc.GetFileHandle(), &buf ) == -1 )
350cdf0e10cSrcweir                 eRet = Error( FSYS_ERR_ACCESSDENIED, 0, &aTgt );
351cdf0e10cSrcweir #endif
352cdf0e10cSrcweir             ::rtl::OUString aTargetFileName;
353cdf0e10cSrcweir             FileBase::getFileURLFromSystemPath( ::rtl::OUString(aTgt.GetFull()), aTargetFileName );
354cdf0e10cSrcweir 
355cdf0e10cSrcweir             SvFileStream aTargetStream( aTargetFileName, STREAM_WRITE | STREAM_TRUNC | STREAM_SHARE_DENYWRITE );
356cdf0e10cSrcweir             if ( !aTargetStream.GetError() )
357cdf0e10cSrcweir             {
358cdf0e10cSrcweir #ifdef UNX
359cdf0e10cSrcweir                 if ( fchmod( aTargetStream.GetFileHandle(), buf.st_mode ) == -1 )
360cdf0e10cSrcweir                     eRet = Error( FSYS_ERR_ACCESSDENIED, 0, &aTgt );
361cdf0e10cSrcweir #endif
362cdf0e10cSrcweir                 size_t nAllocSize = 0, nSize = 0;
363cdf0e10cSrcweir                 char *pBuf = 0;
364cdf0e10cSrcweir                 while ( Progress() && nSize == nAllocSize && eRet == FSYS_ERR_OK )
365cdf0e10cSrcweir                 {
366cdf0e10cSrcweir                     // adjust the block-size
367cdf0e10cSrcweir                     if ( nBlockSize > nAllocSize )
368cdf0e10cSrcweir                     {
369cdf0e10cSrcweir                         delete[] pBuf;
370cdf0e10cSrcweir                         nAllocSize = nBlockSize;
371cdf0e10cSrcweir                         pBuf = new char[nAllocSize];
372cdf0e10cSrcweir                     }
373cdf0e10cSrcweir 
374cdf0e10cSrcweir                     // copy one block
375cdf0e10cSrcweir                     nSize = aSrc.Read( pBuf, nBlockSize );
376cdf0e10cSrcweir                     aTargetStream.Write( pBuf, nSize );
377cdf0e10cSrcweir                     if ( aTargetStream.GetError() )
378cdf0e10cSrcweir                         eRet = Error( aTargetStream.GetError(), 0, &aTgt );
379cdf0e10cSrcweir 
380cdf0e10cSrcweir                     // adjust counters
381cdf0e10cSrcweir                     nBytesCopied += nSize;
382cdf0e10cSrcweir                     if ( nBytesCopied > nBytesTotal )
383cdf0e10cSrcweir                         nBytesTotal = nBytesCopied;
384cdf0e10cSrcweir                 }
385cdf0e10cSrcweir                 delete[] pBuf;
386cdf0e10cSrcweir             }
387cdf0e10cSrcweir             else
388cdf0e10cSrcweir                 eRet = Error( aTargetStream.GetError(), 0, &aTgt );
389cdf0e10cSrcweir 
390cdf0e10cSrcweir             // unvollstaendiges File wieder loeschen
391cdf0e10cSrcweir             aTargetStream.Close();
392cdf0e10cSrcweir 
393cdf0e10cSrcweir             if ( nBytesCopied != nBytesTotal )
394cdf0e10cSrcweir             {
395cdf0e10cSrcweir                 aTgt.Kill();
396cdf0e10cSrcweir             }
397cdf0e10cSrcweir         }
398cdf0e10cSrcweir         else
399cdf0e10cSrcweir             eRet = Error( aSrc.GetError(), &rSource, 0 );
400cdf0e10cSrcweir     }
401cdf0e10cSrcweir     else if ( aSourceFileStat.IsKind(FSYS_KIND_NONE) )
402cdf0e10cSrcweir         eRet = Error( ERRCODE_IO_NOTEXISTS, &rSource, 0 );
403cdf0e10cSrcweir     else
404cdf0e10cSrcweir         eRet = Error( ERRCODE_IO_NOTSUPPORTED, &rSource, 0 );
405cdf0e10cSrcweir 
406cdf0e10cSrcweir #ifdef WNT
407cdf0e10cSrcweir     // Set LastWriteTime and Attributes of the target identical with the source
408cdf0e10cSrcweir 
409cdf0e10cSrcweir     if ( FSYS_ERR_OK == ERRCODE_TOERROR(eRet) )
410cdf0e10cSrcweir     {
411cdf0e10cSrcweir         WIN32_FIND_DATA fdSource;
412cdf0e10cSrcweir         ByteString aFullSource(aSource.GetFull(), osl_getThreadTextEncoding());
413cdf0e10cSrcweir         ByteString aFullTarget(aTgt.GetFull(), osl_getThreadTextEncoding());
414cdf0e10cSrcweir         HANDLE  hFind = FindFirstFile( aFullSource.GetBuffer() , &fdSource );
415cdf0e10cSrcweir         if ( hFind != INVALID_HANDLE_VALUE )
416cdf0e10cSrcweir         {
417cdf0e10cSrcweir             FindClose( hFind );
418cdf0e10cSrcweir 
419cdf0e10cSrcweir             HANDLE hFile = CreateFile( aFullTarget.GetBuffer(), GENERIC_WRITE,
420cdf0e10cSrcweir                                        FILE_SHARE_READ | FILE_SHARE_WRITE, NULL,
421cdf0e10cSrcweir                                        OPEN_EXISTING, FILE_ATTRIBUTE_NORMAL, NULL );
422cdf0e10cSrcweir 
423cdf0e10cSrcweir             if ( hFile != INVALID_HANDLE_VALUE )
424cdf0e10cSrcweir             {
425cdf0e10cSrcweir                 SetFileTime( hFile, NULL, NULL, &fdSource.ftLastWriteTime );
426cdf0e10cSrcweir                 CloseHandle( hFile );
427cdf0e10cSrcweir             }
428cdf0e10cSrcweir 
429cdf0e10cSrcweir             SetFileAttributes( aFullTarget.GetBuffer(), fdSource.dwFileAttributes );
430cdf0e10cSrcweir         }
431cdf0e10cSrcweir     }
432cdf0e10cSrcweir #endif
433cdf0e10cSrcweir     // bei Move ggf. das File/Dir loeschen
434cdf0e10cSrcweir     if ( FSYS_ERR_OK == ERRCODE_TOERROR(eRet) && ( pImp->nActions  & FSYS_ACTION_MOVE ) )
435cdf0e10cSrcweir     {
436cdf0e10cSrcweir         ErrCode eKillErr = Error( rSource.Kill() | ERRCODE_WARNING_MASK, &rSource, 0 );
437cdf0e10cSrcweir         if ( eKillErr != ERRCODE_WARNING_MASK )
438cdf0e10cSrcweir         {
439cdf0e10cSrcweir             if ( rSource.Exists() )
440cdf0e10cSrcweir                 // loeschen ging nicht => dann die Kopie wieder loeschen
441cdf0e10cSrcweir                 aTgt.Kill( pImp->nActions );
442cdf0e10cSrcweir             if ( !eWarn )
443cdf0e10cSrcweir                 eWarn = eKillErr;
444cdf0e10cSrcweir         }
445cdf0e10cSrcweir     }
446cdf0e10cSrcweir 
447cdf0e10cSrcweir     return !eRet ? eWarn : eRet;
448cdf0e10cSrcweir }
449cdf0e10cSrcweir 
450cdf0e10cSrcweir // -----------------------------------------------------------------------
451cdf0e10cSrcweir 
Execute(FSysAction nActions)452cdf0e10cSrcweir FSysError FileCopier::Execute( FSysAction nActions )
453cdf0e10cSrcweir {
454cdf0e10cSrcweir     return ExecuteExact( nActions );
455cdf0e10cSrcweir }
456cdf0e10cSrcweir 
457cdf0e10cSrcweir // -----------------------------------------------------------------------
458cdf0e10cSrcweir 
ExecuteExact(FSysAction nActions,FSysExact eExact)459cdf0e10cSrcweir FSysError FileCopier::ExecuteExact( FSysAction nActions, FSysExact eExact )
460cdf0e10cSrcweir {
461cdf0e10cSrcweir     DirEntry aAbsSource = DirEntry( aSource);
462cdf0e10cSrcweir     DirEntry aAbsTarget = DirEntry( aTarget );
463cdf0e10cSrcweir     pImp->nActions = nActions;
464cdf0e10cSrcweir 
465*86e1cf34SPedro Giffuni     // check if both paths are accessible and source and target are different
466cdf0e10cSrcweir     if ( !aAbsTarget.ToAbs() || !aAbsSource.ToAbs() || aAbsTarget == aAbsSource )
467cdf0e10cSrcweir         return FSYS_ERR_ACCESSDENIED;
468cdf0e10cSrcweir 
469cdf0e10cSrcweir     // check if copy would be endless recursive into itself
470cdf0e10cSrcweir     if ( FSYS_ACTION_RECURSIVE == ( nActions & FSYS_ACTION_RECURSIVE ) &&
471cdf0e10cSrcweir          aAbsSource.Contains( aAbsTarget ) )
472cdf0e10cSrcweir         return ERRCODE_IO_RECURSIVE;
473cdf0e10cSrcweir 
474cdf0e10cSrcweir     // target is directory?
475cdf0e10cSrcweir     if ( eExact == FSYS_NOTEXACT &&
476cdf0e10cSrcweir          FileStat( aAbsTarget ).IsKind(FSYS_KIND_DIR) && FileStat( aAbsSource ).IsKind(FSYS_KIND_FILE) )
477cdf0e10cSrcweir         // append name of source
478cdf0e10cSrcweir         aAbsTarget += aSource.GetName();
479cdf0e10cSrcweir 
480cdf0e10cSrcweir     // recursive copy
481cdf0e10cSrcweir     return DoCopy_Impl( aAbsSource, aAbsTarget );
482cdf0e10cSrcweir }
483