~ubuntu-branches/ubuntu/precise/mysql-5.1/precise

« back to all changes in this revision

Viewing changes to mysys/my_copy.c

  • Committer: Bazaar Package Importer
  • Author(s): Norbert Tretkowski
  • Date: 2010-03-17 14:56:02 UTC
  • Revision ID: james.westby@ubuntu.com-20100317145602-x7e30l1b2sb5s6w6
Tags: upstream-5.1.45
ImportĀ upstreamĀ versionĀ 5.1.45

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
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"
 
17
#include <my_dir.h> /* for stat */
 
18
#include <m_string.h>
 
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
 
 
31
 
 
32
/*
 
33
  int my_copy(const char *from, const char *to, myf MyFlags)
 
34
 
 
35
  NOTES
 
36
    Ordinary ownership and accesstimes are copied from 'from-file'
 
37
    If MyFlags & MY_HOLD_ORIGINAL_MODES is set and to-file exists then
 
38
    the modes of to-file isn't changed
 
39
    If MyFlags & MY_DONT_OVERWRITE_FILE is set, we will give an error
 
40
    if the file existed.
 
41
 
 
42
  WARNING
 
43
    Don't set MY_FNABP or MY_NABP bits on when calling this function !
 
44
 
 
45
  RETURN
 
46
    0   ok
 
47
    #   Error
 
48
 
 
49
*/
 
50
 
 
51
int my_copy(const char *from, const char *to, myf MyFlags)
 
52
{
 
53
  size_t Count;
 
54
  my_bool new_file_stat= 0; /* 1 if we could stat "to" */
 
55
  int create_flag;
 
56
  File from_file,to_file;
 
57
  uchar buff[IO_SIZE];
 
58
  MY_STAT stat_buff,new_stat_buff;
 
59
  int res;
 
60
  DBUG_ENTER("my_copy");
 
61
  DBUG_PRINT("my",("from %s to %s MyFlags %d", from, to, MyFlags));
 
62
 
 
63
  from_file=to_file= -1;
 
64
  DBUG_ASSERT(!(MyFlags & (MY_FNABP | MY_NABP))); /* for my_read/my_write */
 
65
  if (MyFlags & MY_HOLD_ORIGINAL_MODES)         /* Copy stat if possible */
 
66
    new_file_stat= test(my_stat((char*) to, &new_stat_buff, MYF(0)));
 
67
 
 
68
  if ((from_file=my_open(from,O_RDONLY | O_SHARE,MyFlags)) >= 0)
 
69
  {
 
70
    if (!my_stat(from, &stat_buff, MyFlags))
 
71
    {
 
72
      my_errno=errno;
 
73
      goto err;
 
74
    }
 
75
    if (MyFlags & MY_HOLD_ORIGINAL_MODES && new_file_stat)
 
76
      stat_buff=new_stat_buff;
 
77
    create_flag= (MyFlags & MY_DONT_OVERWRITE_FILE) ? O_EXCL : O_TRUNC;
 
78
 
 
79
    if ((to_file=  my_create(to,(int) stat_buff.st_mode,
 
80
                             O_WRONLY | create_flag | O_BINARY | O_SHARE,
 
81
                             MyFlags)) < 0)
 
82
      goto err;
 
83
 
 
84
    while ((Count=my_read(from_file, buff, sizeof(buff), MyFlags)) != 0)
 
85
    {
 
86
        if (Count == (uint) -1 ||
 
87
            my_write(to_file,buff,Count,MYF(MyFlags | MY_NABP)))
 
88
        goto err;
 
89
    }
 
90
 
 
91
    /* sync the destination file */
 
92
    if (MyFlags & MY_SYNC)
 
93
    {
 
94
      if (my_sync(to_file, MyFlags))
 
95
        goto err;
 
96
    }
 
97
 
 
98
    if (my_close(from_file,MyFlags) | my_close(to_file,MyFlags))
 
99
      DBUG_RETURN(-1);                          /* Error on close */
 
100
 
 
101
    /* Copy modes if possible */
 
102
 
 
103
    if (MyFlags & MY_HOLD_ORIGINAL_MODES && !new_file_stat)
 
104
        DBUG_RETURN(0);                 /* File copyed but not stat */
 
105
    res= chmod(to, stat_buff.st_mode & 07777); /* Copy modes */
 
106
#if !defined(__WIN__) && !defined(__NETWARE__)
 
107
    res= chown(to, stat_buff.st_uid,stat_buff.st_gid); /* Copy ownership */
 
108
#endif
 
109
#if !defined(VMS) && !defined(__ZTC__)
 
110
    if (MyFlags & MY_COPYTIME)
 
111
    {
 
112
      struct utimbuf timep;
 
113
      timep.actime  = stat_buff.st_atime;
 
114
      timep.modtime = stat_buff.st_mtime;
 
115
      VOID(utime((char*) to, &timep)); /* last accessed and modified times */
 
116
    }
 
117
#endif
 
118
    DBUG_RETURN(0);
 
119
  }
 
120
 
 
121
err:
 
122
  if (from_file >= 0) VOID(my_close(from_file,MyFlags));
 
123
  if (to_file >= 0)
 
124
  {
 
125
    VOID(my_close(to_file, MyFlags));
 
126
    /* attempt to delete the to-file we've partially written */
 
127
    VOID(my_delete(to, MyFlags));
 
128
  }
 
129
  DBUG_RETURN(-1);
 
130
} /* my_copy */