def update_file(n_errors, name, data, file_time=None, content=None, agents=None): # Full path full_path = common.ossec_path + name error_updating_file = False # Cluster items information: write mode and umask w_mode = cluster_items[data['cluster_item_key']]['write_mode'] umask = cluster_items[data['cluster_item_key']]['umask'] if content is None: zip_path = "{}/{}".format(zip_dir_path, name) with open(zip_path, 'rb') as f: content = f.read() lock_full_path = "{}/queue/cluster/lockdir/{}.lock".format( common.ossec_path, os.path.basename(full_path)) lock_file = open(lock_full_path, 'a+') try: fcntl.lockf(lock_file, fcntl.LOCK_EX) _update_file(file_path=name, new_content=content, umask_int=umask, mtime=file_time, w_mode=w_mode, tmp_dir=tmp_path, whoami='master', agents=agents) except WazuhException as e: logger.debug2("{}: Warning updating file '{}': {}".format( tag, name, e)) error_tag = 'warnings' error_updating_file = True except Exception as e: logger.debug2("{}: Error updating file '{}': {}".format( tag, name, e)) error_tag = 'errors' error_updating_file = True if error_updating_file: n_errors[error_tag][data['cluster_item_key']] = 1 if not n_errors[error_tag].get(data['cluster_item_key']) \ else n_errors[error_tag][data['cluster_item_key']] + 1 fcntl.lockf(lock_file, fcntl.LOCK_UN) lock_file.close() return n_errors, error_updating_file
def overwrite_or_create_files(filename, data, content=None): # Cluster items information: write mode and umask cluster_item_key = data['cluster_item_key'] w_mode = cluster_items[cluster_item_key]['write_mode'] umask = cluster_items[cluster_item_key]['umask'] if content is None: # Full path file_path = common.ossec_path + filename zip_path = "{}/{}".format(zip_path_dir, filename) # File content and time with open(zip_path, 'r') as f: file_data = f.read() else: file_data = content tmp_path='/queue/cluster/tmp_files' _update_file(file_path=filename, new_content=file_data, umask_int=umask, w_mode=w_mode, tmp_dir=tmp_path, whoami='worker')