'system: the name of the system
name: the name of the file
size: size of contents (bytes)
data: base64 encoded file contents
offset: the offset of the chunk
files can be uploaded in chunks, if so the size describes
the chunk rather than the whole file. the offset indicates where
the chunk belongs
the special offset -1 is used to indicate the final chunk'

def __upload_file(self, sys_name, file, size, offset, data):

    contents = base64.decodestring(data)
    del data
    if (offset != (-1)):
        if (size is not None):
            if (size != len(contents)):
                return False
    tt = string.maketrans('/', '+')
    fn = string.translate(file, tt)
    if fn.startswith('..'):
        raise CX(('invalid    filename    used:    %s' % fn))
    udir = ('/var/log/cobbler/anamon/%s' % sys_name)
    if (not os.path.isdir(udir)):
        os.mkdir(udir, 493)
    fn = ('%s/%s' % (udir, fn))
    try:
        st = os.lstat(fn)
    except OSError as e:
        if (e.errno == errno.ENOENT):
            pass
        else:
            raise
    else:
        if (not stat.S_ISREG(st.st_mode)):
            raise CX(('destination    not    a    file:    %s' % fn))
    fd = os.open(fn, (os.O_RDWR | os.O_CREAT), 420)
    try:
        if ((offset == 0) or ((offset == (-1)) and (size == len(contents)))):
            fcntl.lockf(fd, (fcntl.LOCK_EX | fcntl.LOCK_NB))
            try:
                os.ftruncate(fd, 0)
            finally:
                fcntl.lockf(fd, fcntl.LOCK_UN)
        if (offset == (-1)):
            os.lseek(fd, 0, 2)
        else:
            os.lseek(fd, offset, 0)
        fcntl.lockf(fd, (fcntl.LOCK_EX | fcntl.LOCK_NB), len(contents), 0, 2)
        try:
            os.write(fd, contents)
        finally:
            fcntl.lockf(fd, fcntl.LOCK_UN, len(contents), 0, 2)
        if (offset == (-1)):
            if (size is not None):
                fcntl.lockf(fd, (fcntl.LOCK_EX | fcntl.LOCK_NB))
                try:
                    os.ftruncate(fd, size)
                finally:
                    fcntl.lockf(fd, fcntl.LOCK_UN)
    finally:
        os.close(fd)
    return True
