drizzle
C++ MySQL-protocol-compatible database server
Loading...
Searching...
No Matches
my_copy.cc
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., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA */
15
16#include <config.h>
17
18#include <drizzled/internal/my_sys.h>
19
20#include <fcntl.h>
21
22#include <drizzled/internal/m_string.h>
23#include <utime.h>
24
25# include <sys/stat.h>
26
27#include <drizzled/util/test.h>
28
29namespace drizzled
30{
31namespace internal
32{
33
34/*
35 int my_copy(const char *from, const char *to, myf MyFlags)
36
37 NOTES
38 Ordinary ownership and accesstimes are copied from 'from-file'
39 If MyFlags & MY_HOLD_ORIGINAL_MODES is set and to-file exists then
40 the modes of to-file isn't changed
41 If MyFlags & MY_DONT_OVERWRITE_FILE is set, we will give an error
42 if the file existed.
43
44 WARNING
45 Don't set MY_FNABP or MY_NABP bits on when calling this function !
46
47 RETURN
48 0 ok
49 # Error
50
51*/
52
53int my_copy(const char *from, const char *to, myf MyFlags)
54{
55 uint32_t Count;
56 bool new_file_stat= 0; /* 1 if we could stat "to" */
57 int create_flag;
58 int from_file,to_file;
59 unsigned char buff[IO_SIZE];
60 struct stat stat_buff,new_stat_buff;
61
62 from_file=to_file= -1;
63 assert(!(MyFlags & (MY_FNABP | MY_NABP))); /* for my_read/my_write */
64 if (MyFlags & MY_HOLD_ORIGINAL_MODES) /* Copy stat if possible */
65 new_file_stat= test(!stat((char*) to, &new_stat_buff));
66
67 if ((from_file=my_open(from,O_RDONLY,MyFlags)) >= 0)
68 {
69 if (stat(from, &stat_buff))
70 {
71 errno=errno;
72 goto err;
73 }
74 if (MyFlags & MY_HOLD_ORIGINAL_MODES && new_file_stat)
75 stat_buff=new_stat_buff;
76 create_flag= (MyFlags & MY_DONT_OVERWRITE_FILE) ? O_EXCL : O_TRUNC;
77
78 if ((to_file= my_create(to,(int) stat_buff.st_mode,
79 O_WRONLY | create_flag,
80 MyFlags)) < 0)
81 goto err;
82
83 while ((Count= static_cast<uint32_t>(my_read(from_file, buff,
84 sizeof(buff), MyFlags))) != 0)
85 {
86 if (Count == (uint32_t) -1 ||
87 my_write(to_file,buff,Count,MYF(MyFlags | MY_NABP)))
88 goto err;
89 }
90
91 if (my_close(from_file,MyFlags) | my_close(to_file,MyFlags))
92 return(-1); /* Error on close */
93
94 /* Copy modes if possible */
95
96 if (MyFlags & MY_HOLD_ORIGINAL_MODES && !new_file_stat)
97 return 0; /* File copyed but not stat */
98 chmod(to, stat_buff.st_mode & 07777); /* Copy modes */
99 if(chown(to, stat_buff.st_uid,stat_buff.st_gid)!=0)
100 return 0;
101 if (MyFlags & MY_COPYTIME)
102 {
103 struct utimbuf timep;
104 timep.actime = stat_buff.st_atime;
105 timep.modtime = stat_buff.st_mtime;
106 utime((char*) to, &timep); /* last accessed and modified times */
107 }
108 return 0;
109 }
110
111err:
112 if (from_file >= 0) my_close(from_file,MyFlags);
113 if (to_file >= 0)
114 {
115 my_close(to_file, MyFlags);
116 /* attempt to delete the to-file we've partially written */
117 my_delete(to, MyFlags);
118 }
119 return(-1);
120} /* my_copy */
121
122} /* namespace internal */
123} /* namespace drizzled */
TODO: Rename this file - func.h is stupid.