Linux 3.11 introduced O_TMPFILE as a flag to open() sys call. This would enable users to get a fd to an unnamed temporary file. As it's unnamed, it does not require the caller to devise unique names. It is also not accessible through any path. Hence, file creation is race-free. This file is initially unreachable. It is then populated with data(write), metadata(fsetxattr) and fsync'd before being atomically linked into the filesystem in a fully formed state using linkat() sys call. Only after a successful linkat() will the object file will be available for reference. Caveats * Unlike os.rename(), linkat() cannot overwrite destination path if it already exists. If path exists, we unlink and try again. * XFS support for O_TMPFILE was only added in Linux 3.15. * If client disconnects during object upload, although there is no incomplete/stale file on disk, the object directory would persist and is not cleaned up immediately. Change-Id: I8402439fab3aba5d7af449b5e465f89332f606ec Signed-off-by: Prashanth Pai <ppai@redhat.com>
77 lines
2.1 KiB
Python
77 lines
2.1 KiB
Python
# Copyright (c) 2016 OpenStack Foundation
|
|
#
|
|
# Licensed under the Apache License, Version 2.0 (the "License");
|
|
# you may not use this file except in compliance with the License.
|
|
# You may obtain a copy of the License at
|
|
#
|
|
# http://www.apache.org/licenses/LICENSE-2.0
|
|
#
|
|
# Unless required by applicable law or agreed to in writing, software
|
|
# distributed under the License is distributed on an "AS IS" BASIS,
|
|
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or
|
|
# implied.
|
|
# See the License for the specific language governing permissions and
|
|
# limitations under the License.
|
|
|
|
import os
|
|
import ctypes
|
|
from ctypes.util import find_library
|
|
|
|
__all__ = ['linkat']
|
|
|
|
|
|
class Linkat(object):
|
|
|
|
# From include/uapi/linux/fcntl.h
|
|
AT_FDCWD = -100
|
|
AT_SYMLINK_FOLLOW = 0x400
|
|
|
|
__slots__ = '_c_linkat'
|
|
|
|
def __init__(self):
|
|
libc = ctypes.CDLL(find_library('c'), use_errno=True)
|
|
|
|
try:
|
|
c_linkat = libc.linkat
|
|
except AttributeError:
|
|
self._c_linkat = None
|
|
return
|
|
|
|
c_linkat.argtypes = [ctypes.c_int, ctypes.c_char_p,
|
|
ctypes.c_int, ctypes.c_char_p,
|
|
ctypes.c_int]
|
|
c_linkat.restype = ctypes.c_int
|
|
|
|
def errcheck(result, func, arguments):
|
|
if result == -1:
|
|
errno = ctypes.set_errno(0)
|
|
raise IOError(errno, 'linkat: %s' % os.strerror(errno))
|
|
else:
|
|
return result
|
|
|
|
c_linkat.errcheck = errcheck
|
|
|
|
self._c_linkat = c_linkat
|
|
|
|
@property
|
|
def available(self):
|
|
return self._c_linkat is not None
|
|
|
|
def __call__(self, olddirfd, oldpath, newdirfd, newpath, flags):
|
|
"""
|
|
linkat() creates a new link (also known as a hard link)
|
|
to an existing file.
|
|
|
|
See `man 2 linkat` for more info.
|
|
"""
|
|
if not self.available:
|
|
raise EnvironmentError('linkat not available')
|
|
|
|
if not isinstance(olddirfd, int) or not isinstance(newdirfd, int):
|
|
raise TypeError("fd must be an integer.")
|
|
|
|
return self._c_linkat(olddirfd, oldpath, newdirfd, newpath, flags)
|
|
|
|
linkat = Linkat()
|
|
del Linkat
|