@@ -31,8 +31,8 @@ package compile
31
31
32
32
import (
33
33
"fmt"
34
+ "io"
34
35
"os"
35
- "os/exec"
36
36
"path/filepath"
37
37
"sort"
38
38
"strings"
@@ -256,12 +256,41 @@ func run(cmd *cobra.Command, args []string) {
256
256
outputPath := ctx .BuildProperties .ExpandPropsInString ("{build.path}/{recipe.output.tmp_file}" )
257
257
ext := filepath .Ext (outputPath )
258
258
fqbn = strings .Replace (fqbn , ":" , "." , - 1 )
259
- if err := exec .Command ("cp" , outputPath , sketch .Name + "." + fqbn + ext ).Run (); err != nil {
260
- formatter .PrintError (err , "Error copying output file." )
259
+
260
+ // Copy .hex file to sketch folder
261
+ src := outputPath
262
+ dst := filepath .Join (sketchPath , sketch .Name + "." + fqbn + ext )
263
+ if err = copyFile (src , dst ); err != nil {
264
+ formatter .PrintError (err , "Error copying hex file." )
261
265
os .Exit (commands .ErrGeneric )
262
266
}
263
- if err := exec .Command ("cp" , outputPath [:len (outputPath )- 3 ]+ "elf" , sketch .Name + "." + fqbn + ".elf" ).Run (); err != nil {
264
- formatter .PrintError (err , "Error copying output file." )
267
+
268
+ // Copy .elf file to sketch folder
269
+ src = outputPath [:len (outputPath )- 3 ] + "elf"
270
+ dst = filepath .Join (sketchPath , sketch .Name + "." + fqbn + ".elf" )
271
+ if err = copyFile (src , dst ); err != nil {
272
+ formatter .PrintError (err , "Error copying elf file." )
265
273
os .Exit (commands .ErrGeneric )
266
274
}
267
275
}
276
+
277
+ // copyFile copies the src file to dst. Any existing file will be overwritten and will not copy file attributes.
278
+ func copyFile (src , dst string ) error {
279
+ in , err := os .Open (src )
280
+ if err != nil {
281
+ return err
282
+ }
283
+ defer in .Close ()
284
+
285
+ out , err := os .Create (dst )
286
+ if err != nil {
287
+ return err
288
+ }
289
+ defer out .Close ()
290
+
291
+ _ , err = io .Copy (out , in )
292
+ if err != nil {
293
+ return err
294
+ }
295
+ return out .Close ()
296
+ }
0 commit comments