~drizzle-trunk/drizzle/development

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
1280.1.10 by Monty Taylor
Put everything in drizzled into drizzled namespace.
16
#include "config.h"
17
18
#include "drizzled/internal/my_sys.h"
685.1.3 by Monty Taylor
Turned off stdinc - and then fixed the carnage.
19
1241.9.1 by Monty Taylor
Removed global.h. Fixed all the headers.
20
#include <fcntl.h>
21
1241.9.64 by Monty Taylor
Moved remaining non-public portions of mysys and mystrings to drizzled/internal.
22
#include "drizzled/internal/m_string.h"
1 by brian
clean slate
23
#if defined(HAVE_UTIME_H)
24
#include <utime.h>
25
#elif defined(HAVE_SYS_UTIME_H)
26
#include <sys/utime.h>
27
#elif !defined(HPUX10)
28
#include <time.h>
29
struct utimbuf {
30
  time_t actime;
31
  time_t modtime;
32
};
33
#endif
34
1241.9.1 by Monty Taylor
Removed global.h. Fixed all the headers.
35
#ifdef HAVE_SYS_STAT_H
36
# include <sys/stat.h>
37
#endif
38
492.1.7 by Monty Taylor
Moved test() to its own file.
39
#include <drizzled/util/test.h>
1 by brian
clean slate
40
1280.1.10 by Monty Taylor
Put everything in drizzled into drizzled namespace.
41
namespace drizzled
42
{
43
namespace internal
44
{
45
1 by brian
clean slate
46
/*
47
  int my_copy(const char *from, const char *to, myf MyFlags)
48
49
  NOTES
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
54
    if the file existed.
55
56
  WARNING
57
    Don't set MY_FNABP or MY_NABP bits on when calling this function !
58
59
  RETURN
60
    0	ok
61
    #	Error
62
63
*/
64
65
int my_copy(const char *from, const char *to, myf MyFlags)
66
{
482 by Brian Aker
Remove uint.
67
  uint32_t Count;
146 by Brian Aker
my_bool cleanup.
68
  bool new_file_stat= 0; /* 1 if we could stat "to" */
1 by brian
clean slate
69
  int create_flag;
1241.9.1 by Monty Taylor
Removed global.h. Fixed all the headers.
70
  int from_file,to_file;
481 by Brian Aker
Remove all of uchar.
71
  unsigned char buff[IO_SIZE];
15 by brian
Fix for stat, NETWARE removal
72
  struct stat stat_buff,new_stat_buff;
1 by brian
clean slate
73
74
  from_file=to_file= -1;
51.3.20 by Jay Pipes
Phase 6 - Remove DBUG from mysys
75
  assert(!(MyFlags & (MY_FNABP | MY_NABP))); /* for my_read/my_write */
1 by brian
clean slate
76
  if (MyFlags & MY_HOLD_ORIGINAL_MODES)		/* Copy stat if possible */
15 by brian
Fix for stat, NETWARE removal
77
    new_file_stat= test(!stat((char*) to, &new_stat_buff));
1 by brian
clean slate
78
492.1.13 by Monty Taylor
Removed O_SHARE. I think it was only for OS/2.
79
  if ((from_file=my_open(from,O_RDONLY,MyFlags)) >= 0)
1 by brian
clean slate
80
  {
15 by brian
Fix for stat, NETWARE removal
81
    if (stat(from, &stat_buff))
1 by brian
clean slate
82
    {
1241.9.57 by Monty Taylor
Oy. Bigger change than I normally like - but this stuff is all intertwined.
83
      errno=errno;
1 by brian
clean slate
84
      goto err;
85
    }
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;
89
90
    if ((to_file=  my_create(to,(int) stat_buff.st_mode,
492.1.14 by Monty Taylor
Removed O_BINARY and FILE_BINARY.
91
			     O_WRONLY | create_flag,
1 by brian
clean slate
92
			     MyFlags)) < 0)
93
      goto err;
94
1126.8.1 by Joe Daly
changes to allow -Wconversion flag to be turned on
95
    while ((Count= static_cast<uint32_t>(my_read(from_file, buff,
96
            sizeof(buff), MyFlags))) != 0)
1 by brian
clean slate
97
    {
895 by Brian Aker
Completion (?) of uint conversion.
98
	if (Count == (uint32_t) -1 ||
1 by brian
clean slate
99
	    my_write(to_file,buff,Count,MYF(MyFlags | MY_NABP)))
100
	goto err;
101
    }
102
103
    if (my_close(from_file,MyFlags) | my_close(to_file,MyFlags))
51.3.20 by Jay Pipes
Phase 6 - Remove DBUG from mysys
104
      return(-1);				/* Error on close */
1 by brian
clean slate
105
106
    /* Copy modes if possible */
107
108
    if (MyFlags & MY_HOLD_ORIGINAL_MODES && !new_file_stat)
51.3.20 by Jay Pipes
Phase 6 - Remove DBUG from mysys
109
	return(0);			/* File copyed but not stat */
398.1.10 by Monty Taylor
Actually removed VOID() this time.
110
    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
111
    if(chown(to, stat_buff.st_uid,stat_buff.st_gid)!=0)
112
        return 0;
1 by brian
clean slate
113
    if (MyFlags & MY_COPYTIME)
114
    {
115
      struct utimbuf timep;
116
      timep.actime  = stat_buff.st_atime;
117
      timep.modtime = stat_buff.st_mtime;
398.1.10 by Monty Taylor
Actually removed VOID() this time.
118
      utime((char*) to, &timep); /* last accessed and modified times */
1 by brian
clean slate
119
    }
51.3.20 by Jay Pipes
Phase 6 - Remove DBUG from mysys
120
    return(0);
1 by brian
clean slate
121
  }
122
123
err:
398.1.10 by Monty Taylor
Actually removed VOID() this time.
124
  if (from_file >= 0) my_close(from_file,MyFlags);
1 by brian
clean slate
125
  if (to_file >= 0)
126
  {
398.1.10 by Monty Taylor
Actually removed VOID() this time.
127
    my_close(to_file, MyFlags);
1 by brian
clean slate
128
    /* attempt to delete the to-file we've partially written */
398.1.10 by Monty Taylor
Actually removed VOID() this time.
129
    my_delete(to, MyFlags);
1 by brian
clean slate
130
  }
51.3.20 by Jay Pipes
Phase 6 - Remove DBUG from mysys
131
  return(-1);
1 by brian
clean slate
132
} /* my_copy */
1280.1.10 by Monty Taylor
Put everything in drizzled into drizzled namespace.
133
134
} /* namespace internal */
135
} /* namespace drizzled */