* Bug 16328 fixed: typos exists => exist
[scilab.git] / scilab / modules / fileio / src / c / copyfile_windows.c
1 /*
2 * Scilab ( http://www.scilab.org/ ) - This file is part of Scilab
3 * Copyright (C) 2009 - DIGITEO - Allan CORNET
4 *
5  * Copyright (C) 2012 - 2016 - Scilab Enterprises
6  *
7  * This file is hereby licensed under the terms of the GNU GPL v2.0,
8  * pursuant to article 5.3.4 of the CeCILL v.2.1.
9  * This file was originally licensed under the terms of the CeCILL v2.1,
10  * and continues to be available under such terms.
11  * For more information, see the COPYING file which you should have received
12  * along with this program.
13 *
14 */
15 /*--------------------------------------------------------------------------*/
16 #ifdef _MSC_VER
17 #include <windows.h>
18 #include <shellapi.h>
19 #include <string.h>
20 #include "copyfile.h"
21 #include "BOOL.h"
22 #include "charEncoding.h"
23 #include "sci_malloc.h"
24 #include "isdir.h"
25 #include "createdirectory.h"
26 #include "PATH_MAX.h"
27 #include "os_string.h"
28 /*--------------------------------------------------------------------------*/
29 static int CopyFileFunction_windows(wchar_t *DestinationFilename, wchar_t *SourceFilename);
30 static int CopyDirectoryFunction_windows(wchar_t *DestinationDirectory, wchar_t *SourceDirectory);
31 /*--------------------------------------------------------------------------*/
32 int CopyFileFunction(wchar_t *DestinationFilename, wchar_t *SourceFilename)
33 {
34     if (os_wcsicmp(DestinationFilename, SourceFilename) == 0)
35     {
36         SetLastError(ERROR_ACCESS_DENIED);
37         return 1;
38     }
39     return CopyFileFunction_windows(DestinationFilename, SourceFilename);
40 }
41 /*--------------------------------------------------------------------------*/
42 int CopyDirectoryFunction(wchar_t *DestinationDirectory, wchar_t *SourceDirectory)
43 {
44     /* remove last file separator if it does not exist */
45     if ( (SourceDirectory[wcslen(SourceDirectory) - 1] == L'\\') ||
46             (SourceDirectory[wcslen(SourceDirectory) - 1] == L'/') )
47     {
48         SourceDirectory[wcslen(SourceDirectory) - 1] = L'\0';
49     }
50
51     /* remove last file separator if it does not exist */
52     if ( (DestinationDirectory[wcslen(DestinationDirectory) - 1] == L'\\') ||
53             (DestinationDirectory[wcslen(DestinationDirectory) - 1] == L'/') )
54     {
55         DestinationDirectory[wcslen(DestinationDirectory) - 1] = L'\0';
56     }
57
58     if (os_wcsicmp(DestinationDirectory, SourceDirectory) == 0)
59     {
60         SetLastError(ERROR_ACCESS_DENIED);
61         return 1;
62     }
63
64
65     return CopyDirectoryFunction_windows(DestinationDirectory, SourceDirectory);
66 }
67 /*--------------------------------------------------------------------------*/
68 static int CopyFileFunction_windows(wchar_t *DestinationFilename, wchar_t *SourceFilename)
69 {
70     BOOL bFailIfExists = FALSE;
71     if (!CopyFileW(SourceFilename, DestinationFilename, bFailIfExists))
72     {
73         return 1;
74     }
75     return 0;
76 }
77 /*--------------------------------------------------------------------------*/
78 static int CopyDirectoryFunction_windows(wchar_t *DestinationDirectory, wchar_t *SourceDirectory)
79 {
80     WIN32_FIND_DATAW dir_find_data;
81     wchar_t src_buffer[MAX_PATH * 2 + 1];
82     wchar_t dest_buffer[MAX_PATH * 2 + 1];
83     HANDLE find_handle = FindFirstFileW(DestinationDirectory, &dir_find_data);
84     BOOL ans;
85
86     if (find_handle && (find_handle != INVALID_HANDLE_VALUE))
87     {
88         /* destdir exists, now make sure it is a directory */
89         if (dir_find_data.dwFileAttributes & FILE_ATTRIBUTE_DIRECTORY)
90         {
91             ans = 1;
92         }
93         else
94         {
95             ans = 0;
96         }
97         FindClose(find_handle);
98     }
99     else
100     {
101         /* Create the destdir */
102         ans = createdirectoryW(DestinationDirectory);
103     }
104     FindClose(find_handle);
105
106     if (!ans)
107     {
108         return 1;
109     }
110
111     /* Get ready to do some copying */
112     wcscpy(src_buffer, SourceDirectory);
113     wcscat(src_buffer, L"\\*");
114
115     wcscpy(dest_buffer, DestinationDirectory);
116     wcscat(dest_buffer, L"\\");
117
118     find_handle = FindFirstFileW(src_buffer, &dir_find_data);
119     if (find_handle && find_handle != INVALID_HANDLE_VALUE)
120     {
121         do
122         {
123             if (wcscmp(dir_find_data.cFileName, L".") == 0)
124             {
125                 continue;
126             }
127             if (wcscmp(dir_find_data.cFileName, L"..") == 0)
128             {
129                 continue;
130             }
131
132             wcscpy(src_buffer + wcslen(SourceDirectory) + 1, dir_find_data.cFileName);
133             wcscpy(dest_buffer + wcslen(DestinationDirectory) + 1, dir_find_data.cFileName);
134
135             if (dir_find_data.dwFileAttributes & FILE_ATTRIBUTE_DIRECTORY)
136             {
137                 ans = (CopyDirectoryFunction_windows( dest_buffer, src_buffer) == 0);
138             }
139             else
140             {
141                 ans = CopyFileW(src_buffer, dest_buffer, FALSE);
142             }
143             if (!ans)
144             {
145                 break;
146             }
147         }
148         while (FindNextFileW(find_handle, &dir_find_data));
149         FindClose(find_handle);
150     }
151     return (ans ? 0 : 1);
152 }
153 /*--------------------------------------------------------------------------*/
154 #endif /* #ifdef _MSC_VER */
155 /*--------------------------------------------------------------------------*/