Esempio n. 1
0
class Fs(CrawlJob):
    def __init__(self, cfg, db_pool, data):
        super(Fs, self).__init__(cfg, db_pool, data)
        self.cfg = cfg
        self.data = data

        self.protocol = 'filesystem'

        self.creator = None

        self.fs = None
        self.connection_closed = False

    def create(self, path=None):
        self.fs = FilePath(path)
        try:
            return self.fs.getPermissions()
        except:
            return False

    @defer.inlineCallbacks
    def getDirectory(self, path='/'):

        self.fs = yield FilePath(path)

        if not self.fs.getPermissions():
            defer.returnValue(False)

        files = []

        for f in self.fs.listdir():
            if f == '/':
                continue

            fp = path+f
            fs = FilePath(fp)

            # dont follow symlinks
            if fs.realpath().path != fp:
                continue

            perm = None
            isdir = fs.isdir()
            size = fs.getsize()
            modified = datetime.utcfromtimestamp(fs.getModificationTime())

            df = DiscoveredFile(
                resource_id=self.data['resource_id'],
                file_path=path,
                file_name=f,
                file_isdir=isdir,
                file_size=size,
                file_modified=modified,
                file_perm=perm
            )

            print '[%s] LIST %s.' % (self.data['resource_name'], fp if not fp.endswith('.') else fp)
            files.append(df)

        defer.returnValue(files)
Esempio n. 2
0
 def test_config_file_is_world_readable(self):
     patch_dns_config_path(self)
     dns_zone_config = DNSForwardZoneConfig(factory.make_string(),
                                            serial=random.randint(1, 100))
     dns_zone_config.write_config()
     filepath = FilePath(dns_zone_config.zone_info[0].target_path)
     self.assertTrue(filepath.getPermissions().other.read)
Esempio n. 3
0
File: file.py Progetto: hagna/mold
def inspect(doc):
    data = json.loads(doc)
    path = FilePath(data['path'])
    ret = {'kind': 'file', 'path': path.path, 'exists': path.exists()}
    if not ret['exists']:
        return ret
    
    if path.isdir():
        ret['filetype'] = 'dir'
    elif path.isfile():
        ret['filetype'] = 'file'
        ret['size'] = path.statinfo.st_size
        h = sha1()
        fh = open(path.path, 'r')
        while True:
            data = fh.read(4096)
            if not data:
                break
            h.update(data)
        ret['sha1'] = h.hexdigest()

    ret['owner'] = pwd.getpwuid(path.getUserID()).pw_name
    ret['group'] = grp.getgrgid(path.getGroupID()).gr_name
    ret['perms'] = permsString(path.getPermissions())
    ret['ctime'] = int(path.statinfo.st_ctime)
    ret['mtime'] = int(path.statinfo.st_mtime)
    ret['atime'] = int(path.statinfo.st_atime)
    return ret
Esempio n. 4
0
 def test_permissions(self):
     """
     The directory is created with restrictive permissions.
     """
     path = FilePath(self.mktemp())
     DockerPluginScript()._create_listening_directory(path)
     path.restat()
     self.assertEqual(path.getPermissions().shorthand(), "rwx------")
Esempio n. 5
0
 def test_permissions(self):
     """
     The directory is created with restrictive permissions.
     """
     path = FilePath(self.mktemp())
     DockerPluginScript()._create_listening_directory(path)
     path.restat()
     self.assertEqual(path.getPermissions().shorthand(), "rwx------")
Esempio n. 6
0
 def wrapper(case, *args, **kwargs):
     test_file = FilePath(case.mktemp())
     test_file.touch()
     test_file.chmod(0o000)
     permissions = test_file.getPermissions()
     test_file.chmod(0o777)
     if permissions != Permissions(0o000):
         raise SkipTest("Can't run test on filesystem with broken permissions.")
     return test_method(case, *args, **kwargs)
Esempio n. 7
0
 def test_reverse_config_file_is_world_readable(self):
     self.patch(DNSReverseZoneConfig, 'target_dir', self.make_dir())
     dns_zone_config = DNSReverseZoneConfig(
         factory.getRandomString(), serial=random.randint(1, 100),
         dns_ip=factory.getRandomIPAddress(),
         network=factory.getRandomNetwork())
     dns_zone_config.write_config()
     filepath = FilePath(dns_zone_config.target_path)
     self.assertTrue(filepath.getPermissions().other.read)
Esempio n. 8
0
 def test_reverse_config_file_is_world_readable(self):
     patch_dns_config_path(self)
     dns_zone_config = DNSReverseZoneConfig(
         factory.make_string(),
         serial=random.randint(1, 100),
         network=factory.make_ipv4_network())
     dns_zone_config.write_config()
     for tgt in [zi.target_path for zi in dns_zone_config.zone_info]:
         filepath = FilePath(tgt)
         self.assertTrue(filepath.getPermissions().other.read)
Esempio n. 9
0
 def test_install_dir_normalises_permissions(self):
     # install_dir() normalises directory permissions to 0755 and file
     # permissions to 0644.
     target_dir = FilePath(self.make_dir())
     new_dir = FilePath(self.make_dir())
     new_dir.chmod(0700)
     new_image = new_dir.child("image")
     new_image.touch()
     new_image.chmod(0600)
     install_dir(new_dir.path, target_dir.path)
     self.assertEqual("rwxr-xr-x", target_dir.getPermissions().shorthand())
     self.assertEqual(
         "rw-r--r--",
         target_dir.child("image").getPermissions().shorthand())
Esempio n. 10
0
 def test_install_dir_normalises_permissions(self):
     # install_dir() normalises directory permissions to 0755 and file
     # permissions to 0644.
     target_dir = FilePath(self.make_dir())
     new_dir = FilePath(self.make_dir())
     new_dir.chmod(0700)
     new_image = new_dir.child("image")
     new_image.touch()
     new_image.chmod(0600)
     install_dir(new_dir.path, target_dir.path)
     self.assertEqual(
         "rwxr-xr-x",
         target_dir.getPermissions().shorthand())
     self.assertEqual(
         "rw-r--r--",
         target_dir.child("image").getPermissions().shorthand())
Esempio n. 11
0
 def test_write_config_makes_config_world_readable(self):
     target_dir = patch_dns_config_path(self)
     DNSConfig().write_config()
     config_file = FilePath(os.path.join(target_dir, MAAS_NAMED_CONF_NAME))
     self.assertTrue(config_file.getPermissions().other.read)
Esempio n. 12
0
 def test_write_config_makes_config_world_readable(self):
     target_dir = self.make_dir()
     self.patch(DNSConfig, 'target_dir', target_dir)
     DNSConfig().write_config()
     config_file = FilePath(os.path.join(target_dir, MAAS_NAMED_CONF_NAME))
     self.assertTrue(config_file.getPermissions().other.read)