コード例 #1
0
ファイル: fileutils.py プロジェクト: vzhestkov/uyuni
def setPermsPath(path, user=None, group='root', chmod=int('0750', 8)):
    """chown user.group and set permissions to chmod"""
    if isSUSE() and user is None:
        user = '******'
    elif user is None:
        user = '******'

    if not os.path.exists(path):
        raise OSError("*** ERROR: Path doesn't exist (can't set permissions): %s" % path)

    # If non-root, don't bother to change owners
    if os.getuid() != 0:
        return

    gc = GecosCache()
    uid = gc.getuid(user)
    if uid is None:
        raise OSError("*** ERROR: user '%s' doesn't exist. Cannot set permissions properly." % user)

    gid = gc.getgid(group)
    if gid is None:
        raise OSError("*** ERROR: group '%s' doesn't exist. Cannot set permissions properly." % group)

    uid_, gid_ = os.stat(path)[4:6]
    if uid_ != uid or gid_ != gid:
        os.chown(path, uid, gid)
    os.chmod(path, chmod)
コード例 #2
0
ファイル: fileutils.py プロジェクト: vzhestkov/uyuni
def createPath(path, user=None, group=None, chmod=int('0755', 8)):
    """advanced makedirs

    Will create the path if necessary.
    Will chmod, and chown that path properly.
    Defaults for user/group to the apache user

    Uses the above makedirs() function.
    """
    if isSUSE():
        if user is None:
            user = '******'
        if group is None:
            group = 'www'
    else:
        if user is None:
            user = '******'
        if group is None:
            group = 'apache'

    path = cleanupAbsPath(path)
    if not os.path.exists(path):
        makedirs(path, mode=chmod, user=user, group=group)
    elif not os.path.isdir(path):
        raise ValueError("ERROR: createPath('%s'): path doesn't lead to a directory" % str(path))
    else:
        os.chmod(path, chmod)
        uid, gid = getUidGid(user, group)
        try:
            os.chown(path, uid, gid)
        except OSError:
            # Changing permissions failed; ignore the error
            sys.stderr.write("Changing owner for %s failed\n" % path)
コード例 #3
0
ファイル: rhnLog.py プロジェクト: uyuni-project/uyuni
    def __init__(self, log_file, level):
        self.level = level
        self.log_info = "0.0.0.0: "
        self.file = log_file
        self.pid = os.getpid()
        self.real = 0
        if self.file in ["stderr", "stdout"]:
            self.fd = getattr(sys, self.file)
            self.log_info = ""
            return

        newfileYN = 0
        if not os.path.exists(self.file):
            newfileYN = 1  # just used for the chown/chmod

        # else, open it as a real file, with locking and stuff
        try:
            # try to open it in line buffered mode
            self.fd = open(self.file, "a", 1)
            set_close_on_exec(self.fd)
            if newfileYN:
                if isSUSE():
                    apache_uid, apache_gid = getUidGid('wwwrun', 'www')
                else:
                    apache_uid, apache_gid = getUidGid('apache', 'apache')
                os.chown(self.file, apache_uid, apache_gid)
                os.chmod(self.file, int('0660', 8))
        except:
            log_stderr("ERROR LOG FILE: Couldn't open log file %s" % self.file,
                       sys.exc_info()[:2])
            self.file = "stderr"
            self.fd = sys.stderr
        else:
            self.real = 1
コード例 #4
0
 def cache_set(self, params, value):
     log_debug(4, params)
     last_modified = self._get_last_modified(params)
     key = self._get_key(params)
     user = '******'
     group = 'apache'
     if rhnLib.isSUSE():
         user = '******'
         group = 'www'
     return rhnCache.set(key, value, modified=last_modified,
                         raw=1, user=user, group=group, mode=int('0755', 8))
コード例 #5
0
def initLOG(log_file="stderr", level=0):
    global LOG

    # check if it already setup
    if LOG is not None:
        # We already have a logging object
        if log_file is None or LOG.file == log_file:
            # Keep the same logging object, change only the log level
            LOG.level = level
            return
        # We need a different type, so destroy the old one
        LOG = None
    elif log_file is None:
        log_file = "/dev/null"

    # attempt to create the path to the log file if neccessary
    log_path = os.path.dirname(log_file)
    if log_file not in ('stderr', 'stdout') \
            and log_path and not os.path.exists(os.path.dirname(log_file)):
        log_stderr(
            "WARNING: log path not found; attempting to create %s" % log_path,
            sys.exc_info()[:2])

        # fetch uid, gid so we can do a "chown ..."
        if isSUSE():
            apache_uid, apache_gid = getUidGid('wwwrun', 'www')
        else:
            apache_uid, apache_gid = getUidGid('apache', 'apache')

        try:
            os.makedirs(log_path)
            if os.getuid() == 0:
                os.chown(log_path, apache_uid, 0)
            else:
                os.chown(log_path, apache_uid, apache_gid)
        except:
            log_stderr("ERROR: unable to create log file path %s" % log_path,
                       sys.exc_info()[:2])
            return

    # At this point, LOG is None and log_file is not None
    # Get a new LOG
    LOG = rhnLog(log_file, level)
    return 0
コード例 #6
0
    def write_file(self, stream_in):
        """Writes the contents of stream_in to the filesystem
        Returns the file size(success) or raises FileCreationError"""
        dirname = os.path.dirname(self.full_path)
        createPath(dirname)
        stat = os.statvfs(dirname)

        f_bsize = stat[0]  # file system block size
        # misa: it's kind of icky whether to use f_bfree (free blocks) or
        # f_bavail (free blocks for non-root). f_bavail is more correct, since
        # you don't want to have the system out of disk space because of
        # satsync; but people would get confused when looking at the output of
        # df
        f_bavail = stat[4]  # free blocks
        freespace = f_bsize * float(f_bavail)
        if self.file_size is not None and self.file_size > freespace:
            msg = messages.not_enough_diskspace % (freespace / 1024)
            log(-1, msg, stream=sys.stderr)
            # pkilambi: As the metadata download does'nt check for unfetched rpms
            # abort the sync when it runs out of disc space
            sys.exit(-1)
            #raise FileCreationError(msg)
        if freespace < 5000 * 1024:  # arbitrary
            msg = messages.not_enough_diskspace % (freespace / 1024)
            log(-1, msg, stream=sys.stderr)
            # pkilambi: As the metadata download does'nt check for unfetched rpms
            # abort the sync when it runs out of disc space
            sys.exit(-1)
            #raise FileCreationError(msg)

        fout = open(self.full_path, 'wb')
        # setting file permissions; NOTE: rhnpush uses apache to write to disk,
        # hence the 6 setting.
        if rhnLib.isSUSE():
            setPermsPath(self.full_path,
                         user='******',
                         group='www',
                         chmod=int('0644', 8))
        else:
            setPermsPath(self.full_path,
                         user='******',
                         group='apache',
                         chmod=int('0644', 8))
        size = 0
        try:
            while 1:
                buf = stream_in.read(self.buffer_size)
                if not buf:
                    break
                buf_len = len(buf)
                fout.write(buf)
                size = size + buf_len
        except IOError:
            e = sys.exc_info()[1]
            msg = "IOError: %s" % e
            log(-1, msg, stream=sys.stderr)
            # Try not to leave garbage around
            try:
                os.unlink(self.full_path)
            except (OSError, IOError):
                pass
            raise_with_tb(FileCreationError(msg), sys.exc_info()[2])
        l_file_size = fout.tell()
        fout.close()

        if self.file_size is not None and self.file_size != l_file_size:
            # Something bad happened
            msg = "Error: file %s has wrong size. Expected %s bytes, got %s bytes" % (
                self.full_path, self.file_size, l_file_size)
            log(-1, msg, stream=sys.stderr)
            # Try not to leave garbage around
            try:
                os.unlink(self.full_path)
            except (OSError, IOError):
                pass
            raise FileCreationError(msg)

        os.utime(self.full_path, (self.timestamp, self.timestamp))
        return l_file_size