1// Copyright 2016 The etcd Authors
2//
3// Licensed under the Apache License, Version 2.0 (the "License");
4// you may not use this file except in compliance with the License.
5// You may obtain a copy of the License at
6//
7//     http://www.apache.org/licenses/LICENSE-2.0
8//
9// Unless required by applicable law or agreed to in writing, software
10// distributed under the License is distributed on an "AS IS" BASIS,
11// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12// See the License for the specific language governing permissions and
13// limitations under the License.
14
15// +build !windows
16
17package wal
18
19import (
20	"os"
21
22	"github.com/coreos/etcd/pkg/fileutil"
23)
24
25func (w *WAL) renameWal(tmpdirpath string) (*WAL, error) {
26	// On non-Windows platforms, hold the lock while renaming. Releasing
27	// the lock and trying to reacquire it quickly can be flaky because
28	// it's possible the process will fork to spawn a process while this is
29	// happening. The fds are set up as close-on-exec by the Go runtime,
30	// but there is a window between the fork and the exec where another
31	// process holds the lock.
32
33	if err := os.RemoveAll(w.dir); err != nil {
34		return nil, err
35	}
36	if err := os.Rename(tmpdirpath, w.dir); err != nil {
37		return nil, err
38	}
39
40	w.fp = newFilePipeline(w.dir, SegmentSizeBytes)
41	df, err := fileutil.OpenDir(w.dir)
42	w.dirFile = df
43	return w, err
44}
45