1
/* Copyright (C) 2000 MySQL AB
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.
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.
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 */
18
#include "drizzled/internal/my_sys.h"
22
#include "drizzled/internal/m_string.h"
23
#if defined(HAVE_UTIME_H)
25
#elif defined(HAVE_SYS_UTIME_H)
26
#include <sys/utime.h>
27
#elif !defined(HPUX10)
35
#ifdef HAVE_SYS_STAT_H
36
# include <sys/stat.h>
39
#include <drizzled/util/test.h>
47
int my_copy(const char *from, const char *to, myf MyFlags)
50
Ordinary ownership and accesstimes are copied from 'from-file'
51
If MyFlags & MY_HOLD_ORIGINAL_MODES is set and to-file exists then
52
the modes of to-file isn't changed
53
If MyFlags & MY_DONT_OVERWRITE_FILE is set, we will give an error
57
Don't set MY_FNABP or MY_NABP bits on when calling this function !
65
int my_copy(const char *from, const char *to, myf MyFlags)
68
bool new_file_stat= 0; /* 1 if we could stat "to" */
70
int from_file,to_file;
71
unsigned char buff[IO_SIZE];
72
struct stat stat_buff,new_stat_buff;
74
from_file=to_file= -1;
75
assert(!(MyFlags & (MY_FNABP | MY_NABP))); /* for my_read/my_write */
76
if (MyFlags & MY_HOLD_ORIGINAL_MODES) /* Copy stat if possible */
77
new_file_stat= test(!stat((char*) to, &new_stat_buff));
79
if ((from_file=my_open(from,O_RDONLY,MyFlags)) >= 0)
81
if (stat(from, &stat_buff))
86
if (MyFlags & MY_HOLD_ORIGINAL_MODES && new_file_stat)
87
stat_buff=new_stat_buff;
88
create_flag= (MyFlags & MY_DONT_OVERWRITE_FILE) ? O_EXCL : O_TRUNC;
90
if ((to_file= my_create(to,(int) stat_buff.st_mode,
91
O_WRONLY | create_flag,
95
while ((Count= static_cast<uint32_t>(my_read(from_file, buff,
96
sizeof(buff), MyFlags))) != 0)
98
if (Count == (uint32_t) -1 ||
99
my_write(to_file,buff,Count,MYF(MyFlags | MY_NABP)))
103
if (my_close(from_file,MyFlags) | my_close(to_file,MyFlags))
104
return(-1); /* Error on close */
106
/* Copy modes if possible */
108
if (MyFlags & MY_HOLD_ORIGINAL_MODES && !new_file_stat)
109
return(0); /* File copyed but not stat */
110
chmod(to, stat_buff.st_mode & 07777); /* Copy modes */
111
if(chown(to, stat_buff.st_uid,stat_buff.st_gid)!=0)
113
if (MyFlags & MY_COPYTIME)
115
struct utimbuf timep;
116
timep.actime = stat_buff.st_atime;
117
timep.modtime = stat_buff.st_mtime;
118
utime((char*) to, &timep); /* last accessed and modified times */
124
if (from_file >= 0) my_close(from_file,MyFlags);
127
my_close(to_file, MyFlags);
128
/* attempt to delete the to-file we've partially written */
129
my_delete(to, MyFlags);
134
} /* namespace internal */
135
} /* namespace drizzled */