| 122 | } |
| 123 | |
| 124 | func (node *Node) SetFileFromPath(path string, action string) (err error) { |
| 125 | fileStat, err := os.Stat(path) |
| 126 | if err != nil { |
| 127 | return |
| 128 | } |
| 129 | node.File.Name = fileStat.Name() |
| 130 | node.File.Size = fileStat.Size() |
| 131 | node.File.CreatedOn = fileStat.ModTime() |
| 132 | |
| 133 | tmpPath := fmt.Sprintf("%s/temp/%d%d", conf.PATH_DATA, rand.Int(), rand.Int()) |
| 134 | |
| 135 | if action != "copy_file" && action != "move_file" && action != "keep_file" { |
| 136 | return errors.New("setting file from path requires action field equal to copy_file, move_file or keep_file") |
| 137 | } |
| 138 | |
| 139 | if action == "move_file" { |
| 140 | // Determine if device ID of src and target are the same before proceeding. |
| 141 | var devID1 uint64 |
| 142 | var f, _ = os.Open(path) |
| 143 | var fi, _ = f.Stat() |
| 144 | s := fi.Sys() |
| 145 | if s, ok := s.(*syscall.Stat_t); ok { |
| 146 | devID1 = uint64(s.Dev) |
| 147 | } else { |
| 148 | return errors.New("Could not determine device ID of data input file, try copy_file action instead.") |
| 149 | } |
| 150 | |
| 151 | var devID2 uint64 |
| 152 | var f2, _ = os.Open(tmpPath) |
| 153 | var fi2, _ = f2.Stat() |
| 154 | s2 := fi2.Sys() |
| 155 | if s2, ok := s2.(*syscall.Stat_t); ok { |
| 156 | devID2 = uint64(s2.Dev) |
| 157 | } else { |
| 158 | return errors.New("Could not complete move_file action, try copy_file action instead.") |
| 159 | } |
| 160 | |
| 161 | if devID1 != devID2 { |
| 162 | return errors.New("Will not be able to rename file because data input file is on a different device than Shock data directory, try copy_file action instead.") |
| 163 | } |
| 164 | } |
| 165 | |
| 166 | // Open file for reading. |
| 167 | f, err := os.Open(path) |
| 168 | if err != nil { |
| 169 | return err |
| 170 | } |
| 171 | defer f.Close() |
| 172 | |
| 173 | // Set writer |
| 174 | var dst io.Writer |
| 175 | md5h := md5.New() |
| 176 | |
| 177 | if action == "copy_file" { |
| 178 | tmpFile, err := os.Create(tmpPath) |
| 179 | if err != nil { |
| 180 | return err |
| 181 | } |