Exemplo n.º 1
0
 def touch(fpath, mode=0666):
     """Create a new file with passed *mode* in *fpath*.
     If file *fpath* exists, a IOError will be raised."""
     mode = int(mode)
     if not fcheck.mode_check(mode):
         raise ValueError("wrong mode: %r" % oct(mode))
     fd = -1
     try:
         fd = fcntl.open(fpath, fcntl.O_RDONLY | fcntl.O_CREAT, mode)
     finally:
         if fd >= 0:
             unistd.close(fd)
Exemplo n.º 2
0
 def touch(fpath, mode=0666):
     """Create a new file with passed *mode* in *fpath*.
     If file *fpath* exists, a IOError will be raised."""
     mode = int(mode)
     if not fcheck.mode_check(mode):
         raise ValueError("wrong mode: %r" % oct(mode))
     fd = -1
     try:
         fd = os.open(fpath, os.O_RDONLY | os.O_CREAT, mode)
     finally:
         if fd >= 0:
             os.close(fd)
Exemplo n.º 3
0
 def open(fpath, oflag, mode=0666):
     """Open a file descript for a regular file in fpath using the open mode
     specifie by *oflag* with *mode*"""
     oflag = int(oflag)
     if oflag not in FO_MODES:
         raise ValueError("unknown file open mode: %r" % oflag)
     mode = int(mode)
     if not fcheck.mode_check(mode):
         raise ValueError("wrong mode: %r" % oct(mode))
     fopen = _FOMODE2FUNC[oflag]
     fd = -1
     try:
         fd = fopen(fpath, mode)
         fd = File(fd)
         if mode in _FO_NEW_MODES and not fcheck.ino_check(int(fd)):
             raise OSError("not enough free inodes")
     except:
         if fd > -1:
             os.close(fd)
         raise
     return fd
Exemplo n.º 4
0
 def open(fpath, oflags, mode=0666):
     """Open a file descript for a regular file in fpath using the open mode
     specifie by *oflag* with *mode*"""
     _oflags = FOFLAGS2OFLAGS.get(int(oflags), None)
     if oflags is None:
         raise ValueError("unknown file open mode: %r" % oflags)
     mode = int(mode)
     if not fcheck.mode_check(mode):
         raise ValueError("wrong mode: %r" % oct(mode))
     fd = -1
     try:
         fd = fcntl.open(fpath, _oflags, mode) if oflags in _FO_NEW_FLAGS \
              else fcntl.open(fpath, _oflags)
         if oflags in _FO_NEW_FLAGS and not fcheck.ino_check(int(fd)):
             raise OSError("not enough free inodes")
         fd = File(fd)
     except:
         if fd > -1:
             unistd.close(fd)
         raise
     return fd
Exemplo n.º 5
0
 def open(fpath, oflags, mode=0666):
     """Open a file descript for a regular file in fpath using the open mode
     specifie by *oflag* with *mode*"""
     _oflags = FOFLAGS2OFLAGS.get(int(oflags), None)
     if oflags is None:
         raise ValueError("unknown file open mode: %r" % oflags)
     mode = int(mode)
     if not fcheck.mode_check(mode):
         raise ValueError("wrong mode: %r" % oct(mode))
     fd = -1
     try:
         fd = fcntl.open(fpath, _oflags, mode) if oflags in _FO_NEW_FLAGS \
              else fcntl.open(fpath, _oflags)
         if oflags in _FO_NEW_FLAGS and not fcheck.ino_check(int(fd)):
             raise OSError("not enough free inodes")
         fd = File(fd)
     except:
         if fd > -1:
             unistd.close(fd)
         raise
     return fd
Exemplo n.º 6
0
def mkstemp(dirpath, prefix='', suffix='', unlink=1, mode=0600):
    """Creates a file in directory *dir* using *prefix* and *suffix* to
    name it:
            (dir)/<prefix><random_string><postfix>
    Returns a couple of files (pysec.io.fd.File):
            (Read_File, Write_File)
    If *unlink* is true registers a unlink function at exit.
    """
    dirpath = os.path.abspath(dirpath)
    mode = int(mode)
    if not fcheck.mode_check(mode):
        raise ValueError("wrong mode: %r" % oct(mode))
    names = _get_candidate_names()
    for _ in xrange(TMP_MAX):
        name = names.next()
        fpath = os.path.join(dirpath, '%s%s%s' % (prefix, name, suffix))
        if unlink:
            atexit.register(os.unlink, fpath)
        fdr = fdw = -1
        try:
            fdr = os.open(fpath, os.O_RDONLY | os.O_CREAT | os.O_EXCL, mode)
            fdw = os.open(fpath, os.O_WRONLY, mode)
            _set_cloexec(fdr)
            _set_cloexec(fdw)
            return fd.File(fdr), fd.File(fdw)
        except OSError, ex:
            if fdr != -1:
                os.close(fdr)
            if fdw != -1:
                os.close(fdw)
            if ex.errno == errno.EEXIST:
                continue
            else:
                try:
                    os.unlink(fpath)
                except IOError:
                    pass
            raise
        except:
Exemplo n.º 7
0
def mkstemp(dirpath, prefix='', suffix='', unlink=1, mode=0600):
    """Creates a file in directory *dir* using *prefix* and *suffix* to
    name it:
            (dir)/<prefix><random_string><postfix>
    Returns a couple of files (pysec.io.fd.File):
            (Read_File, Write_File)
    If *unlink* is true registers a unlink function at exit.
    """
    dirpath = os.path.abspath(dirpath)
    mode = int(mode)
    if not fcheck.mode_check(mode):
        raise ValueError("wrong mode: %r" % oct(mode))
    names = _get_candidate_names()
    for _ in xrange(TMP_MAX):
        name = names.next()
        fpath = os.path.join(dirpath, '%s%s%s' % (prefix, name, suffix))
        if unlink:
            atexit.register(os.unlink, fpath)
        fdr = fdw = -1
        try:
            fdr = os.open(fpath, os.O_RDONLY | os.O_CREAT | os.O_EXCL, mode)
            fdw = os.open(fpath, os.O_WRONLY, mode)
            _set_cloexec(fdr)
            _set_cloexec(fdw)
            return fd.File(fdr), fd.File(fdw)
        except OSError, ex:
            if fdr != -1:
                os.close(fdr)
            if fdw != -1:
                os.close(fdw)
            if ex.errno == errno.EEXIST:
                continue
            else:
                try:
                    os.unlink(fpath)
                except IOError:
                    pass
            raise
        except:
Exemplo n.º 8
0
def mkdtemp(dirpath, prefix='', suffix='', mode=0700):
    """Creates a directory in directory *dir* using *prefix* and *suffix* to
    name it:
            (dir)/<prefix><random_string><postfix>
    Returns absolute path of directory.
    """
    dirpath = os.path.abspath(dirpath)
    names = _get_candidate_names()
    mode = int(mode)
    if not fcheck.mode_check(mode):
        raise ValueError("wrong mode: %r" % oct(mode))
    for _ in xrange(TMP_MAX):
        name = names.next()
        fpath = os.path.abspath(
            os.path.join(dirpath, '%s%s%s' % (prefix, name, suffix)))
        try:
            os.mkdir(fpath, mode)
            return fpath
        except OSError, ex:
            if ex.errno == errno.EEXIST:
                # try again
                continue
            raise
Exemplo n.º 9
0
def mkdtemp(dirpath, prefix='', suffix='', mode=0700):
    """Creates a directory in directory *dir* using *prefix* and *suffix* to
    name it:
            (dir)/<prefix><random_string><postfix>
    Returns absolute path of directory.
    """
    dirpath = os.path.abspath(dirpath)
    names = _get_candidate_names()
    mode = int(mode)
    if not fcheck.mode_check(mode):
        raise ValueError("wrong mode: %r" % oct(mode))
    for _ in xrange(TMP_MAX):
        name = names.next()
        fpath = os.path.abspath(os.path.join(dirpath, '%s%s%s'
                                % (prefix, name, suffix)))
        try:
            os.mkdir(fpath, mode)
            return fpath
        except OSError, ex:
            if ex.errno == errno.EEXIST:
                # try again
                continue
            raise