func runWithTimeout(cmd *exec.Cmd) error { err := proc.RunWithTimeout(int(*ConfChildTimeout/time.Second), cmd) if err != nil { logger.Error("msg", fmt.Sprintf("runWithTimeout(%s)", cmd.Args), "error", err) } return err }
func runWithContext(ctx context.Context, cmd *exec.Cmd) error { select { case <-ctx.Done(): return ctx.Err() default: } timeout := *ConfChildTimeout deadline, ok := ctx.Deadline() if ok { timeout = deadline.Sub(time.Now()) } seconds := int(timeout / time.Second) if seconds <= 0 { return cmd.Run() } return proc.RunWithTimeout(int(timeout/time.Second), cmd) }
// Convert converts from srcFn to dstFn, into the given format. // Either filenames can be empty or "-" which treated as stdin/stdout func Convert(srcFn, dstFn, format string) error { tempDir, err := ioutil.TempDir("", filepath.Base(srcFn)) if err != nil { return fmt.Errorf("cannot create temporary directory: %s", err) } defer os.RemoveAll(tempDir) if srcFn == "-" || srcFn == "" { srcFn = filepath.Join(tempDir, "source") fh, err := os.Create(srcFn) if err != nil { return fmt.Errorf("error creating temp file %q: %s", srcFn, err) } if _, err = io.Copy(fh, os.Stdin); err != nil { fh.Close() return fmt.Errorf("error writing stdout to %q: %s", srcFn, err) } fh.Close() } c := exec.Command(Loffice, "--nolockcheck", "--norestore", "--headless", "--convert-to", format, "--outdir", tempDir, srcFn) c.Stderr = os.Stderr c.Stdout = c.Stderr Log.Info("calling", "args", c.Args) if err = proc.RunWithTimeout(Timeout, c); err != nil { return fmt.Errorf("error running %q: %s", c.Args, err) } dh, err := os.Open(tempDir) if err != nil { return fmt.Errorf("error opening dest dir %q: %s", tempDir, err) } defer dh.Close() names, err := dh.Readdirnames(3) if err != nil { return fmt.Errorf("error listing %q: %s", tempDir, err) } if len(names) > 2 { return fmt.Errorf("too many files in %q: %q", tempDir, names) } var tfn string for _, fn := range names { if fn != "source" { tfn = filepath.Join(dh.Name(), fn) break } } src, err := os.Open(tfn) if err != nil { return fmt.Errorf("cannot open %q: %s", tfn, err) } defer src.Close() var dst = io.WriteCloser(os.Stdout) if !(dstFn == "-" || dstFn == "") { if dst, err = os.Create(dstFn); err != nil { return fmt.Errorf("cannot create dest file %q: %s", dstFn, err) } } if _, err = io.Copy(dst, src); err != nil { return fmt.Errorf("error copying from %v to %v: %v", src, dst, err) } return nil }