diff --git a/common.c b/common.c index cec48de..7a4b32b 100644 --- a/common.c +++ b/common.c @@ -54,3 +54,26 @@ nvlist_ptr new_property_nvlist() { int property_nvlist_add(nvlist_ptr list, const char *prop, const char *value) { return nvlist_add_string(list, prop, value); } + +int redirect_libzfs_stdout(int to) { + int save, res; + save = dup(STDOUT_FILENO); + if (save < 0) { + return save; + } + res = dup2(to, STDOUT_FILENO); + if (res < 0) { + return res; + } + return save; +} + +int restore_libzfs_stdout(int saved) { + int res; + fflush(stdout); + res = dup2(saved, STDOUT_FILENO); + if (res < 0) { + return res; + } + close(saved); +} diff --git a/common.go b/common.go index 02678d8..6f55639 100644 --- a/common.go +++ b/common.go @@ -9,7 +9,7 @@ package zfs /* -#cgo CFLAGS: -I /usr/include/libzfs -I /usr/include/libspl -DHAVE_IOCTL_IN_SYS_IOCTL_H +#cgo CFLAGS: -I /usr/include/libzfs -I /usr/include/libspl -DHAVE_IOCTL_IN_SYS_IOCTL_H -D_GNU_SOURCE #cgo LDFLAGS: -lzfs -lzpool -lnvpair #include diff --git a/common.h b/common.h index 105554f..f4d33dd 100644 --- a/common.h +++ b/common.h @@ -2,6 +2,9 @@ * using libzfs from go language, make go code shorter and more readable. */ +#ifndef loff_t + #define loff_t off_t +#endif #define INT_MAX_NAME 256 #define INT_MAX_VALUE 1024 #define ZAP_OLDMAXVALUELEN 1024 @@ -35,3 +38,6 @@ void free_properties(property_list_t *root); nvlist_ptr new_property_nvlist(); int property_nvlist_add(nvlist_ptr ptr, const char* prop, const char *value); +int redirect_libzfs_stdout(int to); +int restore_libzfs_stdout(int saved); + diff --git a/sendrecv.go b/sendrecv.go index 0bc5cf0..f57d82e 100644 --- a/sendrecv.go +++ b/sendrecv.go @@ -230,13 +230,12 @@ func (d *Dataset) SendSize(FromName string, flags SendFlags) (size int64, err er defer r.Close() go func() { var tmpe error - saveOut := C.dup(C.fileno(C.stdout)) - if res := C.dup2(C.int(w.Fd()), C.fileno(C.stdout)); res < 0 { - tmpe = fmt.Errorf("Redirection of zfslib stdout failed %d", res) + saveOut := C.redirect_libzfs_stdout(C.int(w.Fd())) + if saveOut < 0 { + tmpe = fmt.Errorf("Redirection of zfslib stdout failed %d", saveOut) } else { tmpe = d.send(FromName, w, &flags) - C.fflush(C.stdout) - C.dup2(saveOut, C.fileno(C.stdout)) + C.restore_libzfs_stdout(saveOut) } w.Close() errch <- tmpe diff --git a/zpool.h b/zpool.h index 33d416f..4b7808d 100644 --- a/zpool.h +++ b/zpool.h @@ -79,7 +79,7 @@ nvlist_ptr get_zpool_vdev_tree(nvlist_ptr nv); nvlist_ptr go_zpool_search_import(libzfs_handle_ptr zfsh, int paths, char **path, boolean_t do_scan); -__uint64_t set_zpool_vdev_online(zpool_list_t *pool, const char *path, int flags); +uint64_t set_zpool_vdev_online(zpool_list_t *pool, const char *path, int flags); int set_zpool_vdev_offline(zpool_list_t *pool, const char *path, boolean_t istmp, boolean_t force); int do_zpool_clear(zpool_list_t *pool, const char *device, u_int32_t rewind_policy); diff --git a/zpool_vdev.c b/zpool_vdev.c index 67bdbde..3d4427a 100644 --- a/zpool_vdev.c +++ b/zpool_vdev.c @@ -8,7 +8,7 @@ #include "zpool.h" -__uint64_t set_zpool_vdev_online(zpool_list_t *pool, const char *path, int flags) { +uint64_t set_zpool_vdev_online(zpool_list_t *pool, const char *path, int flags) { vdev_state_t newstate = VDEV_STATE_UNKNOWN; zpool_vdev_online(pool->zph, path, flags, &newstate); return newstate;