1
by brian
clean slate |
1 |
/* Copyright (C) 2000 MySQL AB
|
2 |
||
3 |
This program is free software; you can redistribute it and/or modify
|
|
4 |
it under the terms of the GNU General Public License as published by
|
|
5 |
the Free Software Foundation; version 2 of the License.
|
|
6 |
||
7 |
This program is distributed in the hope that it will be useful,
|
|
8 |
but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
9 |
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
10 |
GNU General Public License for more details.
|
|
11 |
||
12 |
You should have received a copy of the GNU General Public License
|
|
13 |
along with this program; if not, write to the Free Software
|
|
14 |
Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA */
|
|
15 |
||
16 |
#include "mysys_priv.h" |
|
685.1.3
by Monty Taylor
Turned off stdinc - and then fixed the carnage. |
17 |
|
212.5.18
by Monty Taylor
Moved m_ctype, m_string and my_bitmap. Removed t_ctype. |
18 |
#include <mystrings/m_string.h> |
1
by brian
clean slate |
19 |
#if defined(HAVE_UTIME_H)
|
20 |
#include <utime.h> |
|
21 |
#elif defined(HAVE_SYS_UTIME_H)
|
|
22 |
#include <sys/utime.h> |
|
23 |
#elif !defined(HPUX10)
|
|
24 |
#include <time.h> |
|
25 |
struct utimbuf { |
|
26 |
time_t actime; |
|
27 |
time_t modtime; |
|
28 |
};
|
|
29 |
#endif
|
|
30 |
||
492.1.7
by Monty Taylor
Moved test() to its own file. |
31 |
#include <drizzled/util/test.h> |
1
by brian
clean slate |
32 |
|
33 |
/*
|
|
34 |
int my_copy(const char *from, const char *to, myf MyFlags)
|
|
35 |
||
36 |
NOTES
|
|
37 |
Ordinary ownership and accesstimes are copied from 'from-file'
|
|
38 |
If MyFlags & MY_HOLD_ORIGINAL_MODES is set and to-file exists then
|
|
39 |
the modes of to-file isn't changed
|
|
40 |
If MyFlags & MY_DONT_OVERWRITE_FILE is set, we will give an error
|
|
41 |
if the file existed.
|
|
42 |
||
43 |
WARNING
|
|
44 |
Don't set MY_FNABP or MY_NABP bits on when calling this function !
|
|
45 |
||
46 |
RETURN
|
|
47 |
0 ok
|
|
48 |
# Error
|
|
49 |
||
50 |
*/
|
|
51 |
||
52 |
int my_copy(const char *from, const char *to, myf MyFlags) |
|
53 |
{
|
|
482
by Brian Aker
Remove uint. |
54 |
uint32_t Count; |
146
by Brian Aker
my_bool cleanup. |
55 |
bool new_file_stat= 0; /* 1 if we could stat "to" */ |
1
by brian
clean slate |
56 |
int create_flag; |
57 |
File from_file,to_file; |
|
481
by Brian Aker
Remove all of uchar. |
58 |
unsigned char buff[IO_SIZE]; |
15
by brian
Fix for stat, NETWARE removal |
59 |
struct stat stat_buff,new_stat_buff; |
1
by brian
clean slate |
60 |
|
61 |
from_file=to_file= -1; |
|
51.3.20
by Jay Pipes
Phase 6 - Remove DBUG from mysys |
62 |
assert(!(MyFlags & (MY_FNABP | MY_NABP))); /* for my_read/my_write */ |
1
by brian
clean slate |
63 |
if (MyFlags & MY_HOLD_ORIGINAL_MODES) /* Copy stat if possible */ |
15
by brian
Fix for stat, NETWARE removal |
64 |
new_file_stat= test(!stat((char*) to, &new_stat_buff)); |
1
by brian
clean slate |
65 |
|
492.1.13
by Monty Taylor
Removed O_SHARE. I think it was only for OS/2. |
66 |
if ((from_file=my_open(from,O_RDONLY,MyFlags)) >= 0) |
1
by brian
clean slate |
67 |
{
|
15
by brian
Fix for stat, NETWARE removal |
68 |
if (stat(from, &stat_buff)) |
1
by brian
clean slate |
69 |
{
|
70 |
my_errno=errno; |
|
71 |
goto err; |
|
72 |
}
|
|
73 |
if (MyFlags & MY_HOLD_ORIGINAL_MODES && new_file_stat) |
|
74 |
stat_buff=new_stat_buff; |
|
75 |
create_flag= (MyFlags & MY_DONT_OVERWRITE_FILE) ? O_EXCL : O_TRUNC; |
|
76 |
||
77 |
if ((to_file= my_create(to,(int) stat_buff.st_mode, |
|
492.1.14
by Monty Taylor
Removed O_BINARY and FILE_BINARY. |
78 |
O_WRONLY | create_flag, |
1
by brian
clean slate |
79 |
MyFlags)) < 0) |
80 |
goto err; |
|
81 |
||
82 |
while ((Count=my_read(from_file, buff, sizeof(buff), MyFlags)) != 0) |
|
83 |
{
|
|
895
by Brian Aker
Completion (?) of uint conversion. |
84 |
if (Count == (uint32_t) -1 || |
1
by brian
clean slate |
85 |
my_write(to_file,buff,Count,MYF(MyFlags | MY_NABP))) |
86 |
goto err; |
|
87 |
}
|
|
88 |
||
89 |
if (my_close(from_file,MyFlags) | my_close(to_file,MyFlags)) |
|
51.3.20
by Jay Pipes
Phase 6 - Remove DBUG from mysys |
90 |
return(-1); /* Error on close */ |
1
by brian
clean slate |
91 |
|
92 |
/* Copy modes if possible */
|
|
93 |
||
94 |
if (MyFlags & MY_HOLD_ORIGINAL_MODES && !new_file_stat) |
|
51.3.20
by Jay Pipes
Phase 6 - Remove DBUG from mysys |
95 |
return(0); /* File copyed but not stat */ |
398.1.10
by Monty Taylor
Actually removed VOID() this time. |
96 |
chmod(to, stat_buff.st_mode & 07777); /* Copy modes */ |
512.1.1
by Stewart Smith
a few warnings that show up on ubuntu 8.10: unchecked return codes, not fromat string |
97 |
if(chown(to, stat_buff.st_uid,stat_buff.st_gid)!=0) |
98 |
return 0; |
|
1
by brian
clean slate |
99 |
if (MyFlags & MY_COPYTIME) |
100 |
{
|
|
101 |
struct utimbuf timep; |
|
102 |
timep.actime = stat_buff.st_atime; |
|
103 |
timep.modtime = stat_buff.st_mtime; |
|
398.1.10
by Monty Taylor
Actually removed VOID() this time. |
104 |
utime((char*) to, &timep); /* last accessed and modified times */ |
1
by brian
clean slate |
105 |
}
|
51.3.20
by Jay Pipes
Phase 6 - Remove DBUG from mysys |
106 |
return(0); |
1
by brian
clean slate |
107 |
}
|
108 |
||
109 |
err: |
|
398.1.10
by Monty Taylor
Actually removed VOID() this time. |
110 |
if (from_file >= 0) my_close(from_file,MyFlags); |
1
by brian
clean slate |
111 |
if (to_file >= 0) |
112 |
{
|
|
398.1.10
by Monty Taylor
Actually removed VOID() this time. |
113 |
my_close(to_file, MyFlags); |
1
by brian
clean slate |
114 |
/* attempt to delete the to-file we've partially written */
|
398.1.10
by Monty Taylor
Actually removed VOID() this time. |
115 |
my_delete(to, MyFlags); |
1
by brian
clean slate |
116 |
}
|
51.3.20
by Jay Pipes
Phase 6 - Remove DBUG from mysys |
117 |
return(-1); |
1
by brian
clean slate |
118 |
} /* my_copy */ |