# Path: Utils.py
# class FileUtils:
#     '''Utils.'''
# 
#     @staticmethod
#     def copydir(src, dst):
#         '''Copy directory.
# 
#         Args:
#             src (string): Source path.
#             dst (string): Destination path.
# 
#         Returns:
#             None
# 
#         '''
# 
#         def _copy_fn(src, dst, follow_symlinks=True):
#             '''Copytree helper function.
# 
#             Args:
#                 src (string): Source path.
#                 dst (string): Destination path.
#                 follow_symlinks: Follow symbolic link or not.
# 
#             Returns:
#                 None
# 
#             '''
# 
#             shutil.copy(src, dst, follow_symlinks=False)
# 
#         with StackContext(Privilege.fileaccess):
#             shutil.copytree(src, dst, symlinks=True, copy_function=_copy_fn)
# 
#     @staticmethod
#     def setperm(path, uid, gid, umask=0o777):
#         '''Set permission of the file or directory.
# 
#         Args:
#             path (string): File or directory path.
#             uid (int): UID of the files and directories.
#             gid (int): GID of the files and directories.
#             umask (int) optional: Umask of the files and directories.
# 
#         Returns:
#             None
# 
#         '''
# 
#         path_set = set([path])
#         with StackContext(Privilege.fileaccess):
#             for root, dirs, files in os.walk(path, followlinks=False):
#                 for name in dirs:
#                     path_set.add(os.path.abspath(os.path.join(root, name)))
#                 for name in files:
#                     path_set.add(os.path.abspath(os.path.join(root, name)))
#         with StackContext(Privilege.fullaccess):
#             for path in path_set:
#                 os.chown(path, uid, gid)
#                 os.chmod(path, os.stat(path).st_mode & umask)


import os
import shutil
import fcntl
import PyExt
import Privilege
import Config
from cffi import FFI
from tornado import gen, concurrent, process
from tornado.stack_context import StackContext
from tornado.ioloop import IOLoop
from Utils import FileUtils

        '''

        cache_hash = None
        cache_gid = None
        # Check if special judge needs to rebuild.
        if self.judge_typ in ['ioredir']:
            hashproc = process.Subprocess( \
                ['./HashDir.py', self.res_path + '/check'], \
                stdout=process.Subprocess.STREAM)
            dirhash = yield hashproc.stdout.read_until(b'\n')
            dirhash = int(dirhash.decode('utf-8').rstrip('\n'), 16)

            ret = StdChal.build_cache_find(self.res_path)
            if ret is not None and ret[0] == dirhash:
                cache_hash, cache_gid = ret
                judge_ioredir = IORedirJudge('container/standard', \
                    '/cache/%x'%cache_hash)

            else:
                cache_hash = dirhash
                _, cache_gid = StdChal.get_standard_ugid()
                build_ugid = StdChal.get_standard_ugid()
                build_relpath = '/cache/%x'%cache_hash
                build_path = 'container/standard' + build_relpath

                judge_ioredir = IORedirJudge('container/standard', \
                    build_relpath)
                if not (yield judge_ioredir.build(build_ugid, self.res_path)):
                    return [(0, 0, STATUS_ERR)] * len(self.test_list), ''

# Path: Utils.py
# class FileUtils:
#     '''Utils.'''
# 
#     @staticmethod
#     def copydir(src, dst):
#         '''Copy directory.
# 
#         Args:
#             src (string): Source path.
#             dst (string): Destination path.
# 
#         Returns:
#             None
# 
#         '''
# 
#         def _copy_fn(src, dst, follow_symlinks=True):
#             '''Copytree helper function.
# 
#             Args:
#                 src (string): Source path.
#                 dst (string): Destination path.
#                 follow_symlinks: Follow symbolic link or not.
# 
#             Returns:
#                 None
# 
#             '''
# 
#             shutil.copy(src, dst, follow_symlinks=False)
# 
#         with StackContext(Privilege.fileaccess):
#             shutil.copytree(src, dst, symlinks=True, copy_function=_copy_fn)
# 
#     @staticmethod
#     def setperm(path, uid, gid, umask=0o777):
#         '''Set permission of the file or directory.
# 
#         Args:
#             path (string): File or directory path.
#             uid (int): UID of the files and directories.
#             gid (int): GID of the files and directories.
#             umask (int) optional: Umask of the files and directories.
# 
#         Returns:
#             None
# 
#         '''
# 
#         path_set = set([path])
#         with StackContext(Privilege.fileaccess):
#             for root, dirs, files in os.walk(path, followlinks=False):
#                 for name in dirs:
#                     path_set.add(os.path.abspath(os.path.join(root, name)))
#                 for name in files:
#                     path_set.add(os.path.abspath(os.path.join(root, name)))
#         with StackContext(Privilege.fullaccess):
#             for path in path_set:
#                 os.chown(path, uid, gid)
#                 os.chmod(path, os.stat(path).st_mode & umask)


# Path: StdChal.py
import os
import shutil
import fcntl
import PyExt
import Privilege
import Config
from cffi import FFI
from tornado import gen, concurrent, process
from tornado.stack_context import StackContext
from tornado.ioloop import IOLoop
from Utils import FileUtils


        '''

        cache_hash = None
        cache_gid = None
        # Check if special judge needs to rebuild.
        if self.judge_typ in ['ioredir']:
            hashproc = process.Subprocess( \
                ['./HashDir.py', self.res_path + '/check'], \
                stdout=process.Subprocess.STREAM)
            dirhash = yield hashproc.stdout.read_until(b'\n')
            dirhash = int(dirhash.decode('utf-8').rstrip('\n'), 16)

            ret = StdChal.build_cache_find(self.res_path)
            if ret is not None and ret[0] == dirhash:
                cache_hash, cache_gid = ret
                judge_ioredir = IORedirJudge('container/standard', \
                    '/cache/%x'%cache_hash)

            else:
                cache_hash = dirhash
                _, cache_gid = StdChal.get_standard_ugid()
                build_ugid = StdChal.get_standard_ugid()
                build_relpath = '/cache/%x'%cache_hash
                build_path = 'container/standard' + build_relpath

                judge_ioredir = IORedirJudge('container/standard', \
                    build_relpath)
                if not (yield judge_ioredir.build(build_ugid, self.res_path)):
                    return [(0, 0, STATUS_ERR)] * len(self.test_list), ''

                FileUtils.setperm(build_path, \