12
12
You should have received a copy of the GNU General Public License
13
13
along with this program; if not, write to the Free Software
14
Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA */
18
#include "drizzled/internal/my_sys.h"
22
#include "drizzled/internal/m_string.h"
14
Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA */
16
#include "mysys_priv.h"
17
#include <my_dir.h> /* for stat */
23
19
#if defined(HAVE_UTIME_H)
25
21
#elif defined(HAVE_SYS_UTIME_H)
65
51
int my_copy(const char *from, const char *to, myf MyFlags)
68
bool new_file_stat= 0; /* 1 if we could stat "to" */
54
my_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;
56
File from_file,to_file;
58
MY_STAT stat_buff,new_stat_buff;
59
DBUG_ENTER("my_copy");
60
DBUG_PRINT("my",("from %s to %s MyFlags %d", from, to, MyFlags));
74
62
from_file=to_file= -1;
75
assert(!(MyFlags & (MY_FNABP | MY_NABP))); /* for my_read/my_write */
63
DBUG_ASSERT(!(MyFlags & (MY_FNABP | MY_NABP))); /* for my_read/my_write */
76
64
if (MyFlags & MY_HOLD_ORIGINAL_MODES) /* Copy stat if possible */
77
new_file_stat= test(!stat((char*) to, &new_stat_buff));
65
new_file_stat= test(my_stat((char*) to, &new_stat_buff, MYF(0)));
79
if ((from_file=my_open(from,O_RDONLY,MyFlags)) >= 0)
67
if ((from_file=my_open(from,O_RDONLY | O_SHARE,MyFlags)) >= 0)
81
if (stat(from, &stat_buff))
69
if (!my_stat(from, &stat_buff, MyFlags))
86
74
if (MyFlags & MY_HOLD_ORIGINAL_MODES && new_file_stat)
88
76
create_flag= (MyFlags & MY_DONT_OVERWRITE_FILE) ? O_EXCL : O_TRUNC;
90
78
if ((to_file= my_create(to,(int) stat_buff.st_mode,
91
O_WRONLY | create_flag,
79
O_WRONLY | create_flag | O_BINARY | O_SHARE,
95
while ((Count= static_cast<uint32_t>(my_read(from_file, buff,
96
sizeof(buff), MyFlags))) != 0)
83
while ((Count=my_read(from_file, buff, sizeof(buff), MyFlags)) != 0)
98
if (Count == (uint32_t) -1 ||
85
if (Count == (uint) -1 ||
99
86
my_write(to_file,buff,Count,MYF(MyFlags | MY_NABP)))
103
90
if (my_close(from_file,MyFlags) | my_close(to_file,MyFlags))
104
return(-1); /* Error on close */
91
DBUG_RETURN(-1); /* Error on close */
106
93
/* Copy modes if possible */
108
95
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)
96
DBUG_RETURN(0); /* File copyed but not stat */
97
VOID(chmod(to, stat_buff.st_mode & 07777)); /* Copy modes */
98
VOID(chown(to, stat_buff.st_uid,stat_buff.st_gid)); /* Copy ownership */
113
99
if (MyFlags & MY_COPYTIME)
115
101
struct utimbuf timep;
116
102
timep.actime = stat_buff.st_atime;
117
103
timep.modtime = stat_buff.st_mtime;
118
utime((char*) to, &timep); /* last accessed and modified times */
104
VOID(utime((char*) to, &timep)); /* last accessed and modified times */
124
if (from_file >= 0) my_close(from_file,MyFlags);
110
if (from_file >= 0) VOID(my_close(from_file,MyFlags));
125
111
if (to_file >= 0)
127
my_close(to_file, MyFlags);
113
VOID(my_close(to_file, MyFlags));
128
114
/* attempt to delete the to-file we've partially written */
129
my_delete(to, MyFlags);
115
VOID(my_delete(to, MyFlags));
134
} /* namespace internal */
135
} /* namespace drizzled */