<commit_before>/**
 *  Copyright (C) 2010 Cloud.com, Inc.  All rights reserved.
 * 
 * This software is licensed under the GNU General Public License v3 or later.
 * 
 * It is free software: you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation, either version 3 of the License, or any later version.
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 * 
 * You should have received a copy of the GNU General Public License
 * along with this program.  If not, see <http://www.gnu.org/licenses/>.
 * 
 */
package com.cloud.storage.template;

import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.util.Arrays;
import java.util.Map;

import javax.naming.ConfigurationException;

import org.apache.log4j.Logger;

import com.cloud.exception.InternalErrorException;
import com.cloud.storage.StorageLayer;
import com.cloud.storage.Storage.ImageFormat;
import com.cloud.utils.NumbersUtil;

/**
 * VhdProcessor processes the downloaded template for VHD.  It
 * currently does not handle any type of template conversion
 * into the VHD format.
 *
 */
public class VhdProcessor implements Processor {
    
    private static final Logger s_logger = Logger.getLogger(VhdProcessor.class);
    String _name;
    StorageLayer _storage;
    private int vhd_footer_size = 512;
    private int vhd_footer_creator_app_offset = 28;
    private int vhd_footer_creator_ver_offset = 32;
    private int vhd_footer_current_size_offset = 48;
    private byte[][] citrix_creator_app = {{0x74, 0x61, 0x70, 0x00},{0x43, 0x54, 0x58, 0x53}}; /*"tap ", and "CTXS"*/

    @Override
    public FormatInfo process(String templatePath, ImageFormat format, String templateName) throws InternalErrorException {
        if (format != null) {
            s_logger.debug("We currently don't handle conversion from " + format + " to VHD.");
            return null;
        }
        
        String vhdPath = templatePath + File.separator + templateName + "." + ImageFormat.VHD.getFileExtension();
       
        if (!_storage.exists(vhdPath)) {
            s_logger.debug("Unable to find the vhd file: " + vhdPath);
            return null;
        }
        
        FormatInfo info = new FormatInfo();
        info.format = ImageFormat.VHD;
        info.filename = templateName + "." + ImageFormat.VHD.getFileExtension();
        
        File vhdFile = _storage.getFile(vhdPath);
        
        info.size = _storage.getSize(vhdPath);
        FileInputStream strm = null;
        byte[] currentSize = new byte[8];
        byte[] creatorApp = new byte[4];
        try {
            strm = new FileInputStream(vhdFile);
            strm.skip(info.size - vhd_footer_size + vhd_footer_creator_app_offset);
            strm.read(creatorApp);
            strm.skip(vhd_footer_current_size_offset - vhd_footer_creator_ver_offset);
            strm.read(currentSize);           
        } catch (Exception e) {
            s_logger.warn("Unable to read vhd file " + vhdPath, e);
            throw new InternalErrorException("Unable to read vhd file " + vhdPath + ": " + e);
        } finally {
            if (strm != null) {
                try {
                    strm.close();
                } catch (IOException e) {
                }
            }
        }
        
        boolean findKnownCreator = false;
        for (int i = 0; i < citrix_creator_app.length; i++) {
        	if (Arrays.equals(creatorApp, citrix_creator_app[i])) {
        		findKnownCreator = true;
        		break;
        	}
        }
        if (!findKnownCreator) {
        	/*Only support VHD image created by citrix xenserver, and xenconverter*/
        	String readableCreator = "";
        	for (int j = 0; j < creatorApp.length; j++) {
        		readableCreator += (char)creatorApp[j];
        	}
        	throw new InternalErrorException("Image creator is:" + readableCreator +", is not supported");
        }
		
        
        long templateSize = NumbersUtil.bytesToLong(currentSize);
        info.virtualSize = templateSize;

        return info;
    }

    @Override
    public boolean configure(String name, Map<String, Object> params) throws ConfigurationException {
        _name = name;
        _storage = (StorageLayer)params.get(StorageLayer.InstanceConfigKey);
        if (_storage == null) {
            throw new ConfigurationException("Unable to get storage implementation");
        }
        
        return true;
    }

    @Override
    public String getName() {
        return _name;
    }

    @Override
    public boolean start() {
        return true;
    }

    @Override
    public boolean stop() {
        return true;
    }
}<commit_msg>Remove the VHD template signature check when downdloading template. There are lots tools can create VHD disk: xenserver, xenconverter, hyper-v, platespin, etc. And some tools didn't create a correct signature.<commit_after>/**
 *  Copyright (C) 2010 Cloud.com, Inc.  All rights reserved.
 * 
 * This software is licensed under the GNU General Public License v3 or later.
 * 
 * It is free software: you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation, either version 3 of the License, or any later version.
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 * 
 * You should have received a copy of the GNU General Public License
 * along with this program.  If not, see <http://www.gnu.org/licenses/>.
 * 
 */
package com.cloud.storage.template;

import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.util.Arrays;
import java.util.Map;

import javax.naming.ConfigurationException;

import org.apache.log4j.Logger;

import com.cloud.exception.InternalErrorException;
import com.cloud.storage.StorageLayer;
import com.cloud.storage.Storage.ImageFormat;
import com.cloud.utils.NumbersUtil;

/**
 * VhdProcessor processes the downloaded template for VHD.  It
 * currently does not handle any type of template conversion
 * into the VHD format.
 *
 */
public class VhdProcessor implements Processor {
    
    private static final Logger s_logger = Logger.getLogger(VhdProcessor.class);
    String _name;
    StorageLayer _storage;
    private int vhd_footer_size = 512;
    private int vhd_footer_creator_app_offset = 28;
    private int vhd_footer_creator_ver_offset = 32;
    private int vhd_footer_current_size_offset = 48;
    private byte[][] citrix_creator_app = {{0x74, 0x61, 0x70, 0x00},{0x43, 0x54, 0x58, 0x53}}; /*"tap ", and "CTXS"*/

    @Override
    public FormatInfo process(String templatePath, ImageFormat format, String templateName) throws InternalErrorException {
        if (format != null) {
            s_logger.debug("We currently don't handle conversion from " + format + " to VHD.");
            return null;
        }
        
        String vhdPath = templatePath + File.separator + templateName + "." + ImageFormat.VHD.getFileExtension();
       
        if (!_storage.exists(vhdPath)) {
            s_logger.debug("Unable to find the vhd file: " + vhdPath);
            return null;
        }
        
        FormatInfo info = new FormatInfo();
        info.format = ImageFormat.VHD;
        info.filename = templateName + "." + ImageFormat.VHD.getFileExtension();
        
        File vhdFile = _storage.getFile(vhdPath);
        
        info.size = _storage.getSize(vhdPath);
        FileInputStream strm = null;
        byte[] currentSize = new byte[8];
        byte[] creatorApp = new byte[4];
        try {
            strm = new FileInputStream(vhdFile);
            strm.skip(info.size - vhd_footer_size + vhd_footer_creator_app_offset);
            strm.read(creatorApp);
            strm.skip(vhd_footer_current_size_offset - vhd_footer_creator_ver_offset);
            strm.read(currentSize);           
        } catch (Exception e) {
            s_logger.warn("Unable to read vhd file " + vhdPath, e);
            throw new InternalErrorException("Unable to read vhd file " + vhdPath + ": " + e);
        } finally {
            if (strm != null) {
                try {
                    strm.close();
                } catch (IOException e) {
                }
            }
        }
        
        //imageSignatureCheck(creatorApp);
        
        long templateSize = NumbersUtil.bytesToLong(currentSize);
        info.virtualSize = templateSize;

        return info;
    }

    @Override
    public boolean configure(String name, Map<String, Object> params) throws ConfigurationException {
        _name = name;
        _storage = (StorageLayer)params.get(StorageLayer.InstanceConfigKey);
        if (_storage == null) {
            throw new ConfigurationException("Unable to get storage implementation");
        }
        
        return true;
    }

    @Override
    public String getName() {
        return _name;
    }

    @Override
    public boolean start() {
        return true;
    }

    @Override
    public boolean stop() {
        return true;
    }
    
    private void imageSignatureCheck(byte[] creatorApp) throws InternalErrorException {
    	boolean findKnownCreator = false;
    	for (int i = 0; i < citrix_creator_app.length; i++) {
    		if (Arrays.equals(creatorApp, citrix_creator_app[i])) {
    			findKnownCreator = true;
    			break;
    		}
    	}
    	if (!findKnownCreator) {
    		/*Only support VHD image created by citrix xenserver, and xenconverter*/
    		String readableCreator = "";
    		for (int j = 0; j < creatorApp.length; j++) {
    			readableCreator += (char)creatorApp[j];
    		}
    		throw new InternalErrorException("Image creator is:" + readableCreator +", is not supported");
    	}
    }
}
{'fraction_non_alphanumeric': 0.09004133044676245, 'fraction_numerical': 0.008659712654989176, 'mean_word_length': 2.511748445058742, 'pattern_counts': {'":': 2, '<': 11, '<?xml version=': 0, '>': 7, 'https://': 0, 'lorem ipsum': 0, 'www.': 2, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 2, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '7482716', 'n_tokens_mistral': 2949, 'n_tokens_neox': 2824, 'n_words': 858}