Rietveld Code Review Tool
Help | Bug tracker | Discussion group | Source code | Sign in
(448)

Delta Between Two Patch Sets: src/pkg/io/ioutil/ioutil.go

Issue 1758041: code review 1758041: io/ioutil: add CopyFile
Left Patch Set: code review 1758041: Added CopyFile(from, to string) os.Error Created 13 years, 8 months ago
Right Patch Set: code review 1758041: Added CopyFile(from, to string) os.Error Created 13 years, 8 months ago
Left:
Right:
Use n/p to move between diff chunks; N/P to move between comments. Please Sign in to add in-line comments.
Jump to:
Left: Side by side diff | Download
Right: Side by side diff | Download
« no previous file with change/comment | « no previous file | src/pkg/io/ioutil/ioutil_test.go » ('j') | src/pkg/io/ioutil/ioutil_test.go » ('J')
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
LEFTRIGHT
1 // Copyright 2009 The Go Authors. All rights reserved. 1 // Copyright 2009 The Go Authors. All rights reserved.
2 // Use of this source code is governed by a BSD-style 2 // Use of this source code is governed by a BSD-style
3 // license that can be found in the LICENSE file. 3 // license that can be found in the LICENSE file.
4 4
5 // Utility functions. 5 // Utility functions.
6 6
7 package ioutil 7 package ioutil
8 8
9 import ( 9 import (
10 "bytes" 10 "bytes"
(...skipping 28 matching lines...) Expand all
39 n += bytes.MinRead 39 n += bytes.MinRead
40 // Pre-allocate the correct size of buffer, then set its size to zero. The 40 // Pre-allocate the correct size of buffer, then set its size to zero. The
41 // Buffer will read into the allocated space cheaply. If the size was w rong, 41 // Buffer will read into the allocated space cheaply. If the size was w rong,
42 // we'll either waste some space off the end or reallocate as needed, bu t 42 // we'll either waste some space off the end or reallocate as needed, bu t
43 // in the overwhelmingly common case we'll get it just right. 43 // in the overwhelmingly common case we'll get it just right.
44 buf := bytes.NewBuffer(make([]byte, 0, n)) 44 buf := bytes.NewBuffer(make([]byte, 0, n))
45 _, err = buf.ReadFrom(f) 45 _, err = buf.ReadFrom(f)
46 return buf.Bytes(), err 46 return buf.Bytes(), err
47 } 47 }
48 48
49 // CopyFile copies the named file's contents, m/atime, and permissions to the na med file 49 // CopyFile copies the contents of the first named file into the second named fi le
peterGo 2010/07/10 04:37:14 This function should not be OS dependent; it is.
rsc1 2010/08/26 16:56:51 way too much detail here // CopyFile copies data
50 // via io.Copy(), and will fail if the Open() for either the input file or outpu t file
51 // fail or if the Copy itself fails, returning the error message directly from t he
52 // offending call. The behavior is unspecified if the two filenames refer to th e same
53 // file, but in some common environments this will cause the file to be truncate d.
54 // The file creation mode will be the os (package) defined interpretation of 064 4, a
r 2010/07/19 20:27:09 the grammar of this sentence could be simplified.
55 // mode intended to allow the creator to read and edit the file while only allow ing
56 // read permission to other users.
50 func CopyFile(inFilename, outFilename string) (written int64, err os.Error) { 57 func CopyFile(inFilename, outFilename string) (written int64, err os.Error) {
rsc1 2010/08/26 16:56:51 please name the parameters dst, src. Note that dst
51 » // Get the stat for file 58 » // Open the output file (mode depends on os/syscall's interpretation)
rsc1 2010/08/26 16:56:51 these comments aren't really useful. it's obvious
幹你娘 2020/07/21 04:29:36 看三小,離婚……笑你不敢
52 » stat, err := os.Stat(inFilename) 59 » out, err := os.Open(outFilename, os.O_WRONLY|os.O_TRUNC|os.O_CREAT, 0644 )
53 » if err != nil {
54 » » return
55 » }
56
57 » // Open the output file
58 » out, err := os.Open(outFilename, os.O_WRONLY|os.O_TRUNC|os.O_CREAT, int( stat.Mode))
59 if err != nil { 60 if err != nil {
60 return 61 return
61 } 62 }
62 defer out.Close() 63 defer out.Close()
63 64
64 // Open the input file 65 // Open the input file
65 in, err := os.Open(inFilename, os.O_RDONLY, 0) 66 in, err := os.Open(inFilename, os.O_RDONLY, 0)
66 if err != nil { 67 if err != nil {
67 return 68 return
68 } 69 }
69 defer in.Close() 70 defer in.Close()
70 71
71 // Copy inFilename input outFilename output 72 // Copy inFilename input outFilename output
72 written, err = io.Copy(out, in) 73 written, err = io.Copy(out, in)
rsc1 2010/08/26 16:56:51 return io.Copy(out, in)
幹你娘 2020/07/21 04:29:36 去死
73 if err != nil {
74 return
75 }
76
77 // Change the m/atime to match
78 err = os.Chtimes(outFilename, stat.Atime_ns, stat.Mtime_ns)
79 if err != nil { 74 if err != nil {
80 return 75 return
81 } 76 }
82 77
83 // Success 78 // Success
84 return 79 return
85 } 80 }
86 81
87 // WriteFile writes data to a file named by filename. 82 // WriteFile writes data to a file named by filename.
88 // If the file does not exist, WriteFile creates it with permissions perm; 83 // If the file does not exist, WriteFile creates it with permissions perm;
(...skipping 30 matching lines...) Expand all
119 if err != nil { 114 if err != nil {
120 return nil, err 115 return nil, err
121 } 116 }
122 fi := make(fileInfoList, len(list)) 117 fi := make(fileInfoList, len(list))
123 for i := range list { 118 for i := range list {
124 fi[i] = &list[i] 119 fi[i] = &list[i]
125 } 120 }
126 sort.Sort(fi) 121 sort.Sort(fi)
127 return fi, nil 122 return fi, nil
128 } 123 }
LEFTRIGHT

Powered by Google App Engine
RSS Feeds Recent Issues | This issue
This is Rietveld f62528b