github.com/slspeek/camlistore_namedsearch@v0.0.0-20140519202248-ed6f70f7721a/pkg/blobserver/localdisk/receive_windows.go (about)

     1  /*
     2  Copyright 2011 Google Inc.
     3  
     4  Licensed under the Apache License, Version 2.0 (the "License");
     5  you may not use this file except in compliance with the License.
     6  You may obtain a copy of the License at
     7  
     8       http://www.apache.org/licenses/LICENSE-2.0
     9  
    10  Unless required by applicable law or agreed to in writing, software
    11  distributed under the License is distributed on an "AS IS" BASIS,
    12  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
    13  See the License for the specific language governing permissions and
    14  limitations under the License.
    15  */
    16  
    17  package localdisk
    18  
    19  import (
    20  	"fmt"
    21  	"os"
    22  	"syscall"
    23  )
    24  
    25  // mapRenameError returns nil if and only if
    26  // 1) the input err is the error returned on windows when trying to rename
    27  // a file over one that already exists
    28  // 2) oldfile and newfile are the same files (i.e have the same size)
    29  func mapRenameError(err error, oldfile, newfile string) error {
    30  	linkErr, ok := err.(*os.LinkError)
    31  	if !ok {
    32  		return err
    33  	}
    34  	if linkErr.Err != error(syscall.ERROR_ALREADY_EXISTS) {
    35  		return err
    36  	}
    37  	// TODO(mpl): actually on linux at least, os.Rename apparently
    38  	// erases the destination with no error even if it is different
    39  	// from the source.
    40  	// So why don't we allow the same for windows? and if needed,
    41  	// do the check size before renaming?
    42  	statNew, err := os.Stat(newfile)
    43  	if err != nil {
    44  		return err
    45  	}
    46  	statOld, err := os.Stat(oldfile)
    47  	if err != nil {
    48  		return err
    49  	}
    50  	if statNew.Size() != statOld.Size() {
    51  		return fmt.Errorf("Will not overwrite destination file %v with source file %v, as they are different.", newfile, oldfile)
    52  	}
    53  	return nil
    54  }