#!/bin/bash

# -------------------------------------------------------------------------- #
# Copyright 2002-2012, OpenNebula Project Leads (OpenNebula.org)             #
#                                                                            #
# Licensed under the Apache License, Version 2.0 (the "License"); you may    #
# not use this file except in compliance with the License. You may obtain    #
# a copy of the License at                                                   #
#                                                                            #
# http://www.apache.org/licenses/LICENSE-2.0                                 #
#                                                                            #
# Unless required by applicable law or agreed to in writing, software        #
# distributed under the License is distributed on an "AS IS" BASIS,          #
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.   #
# See the License for the specific language governing permissions and        #
# limitations under the License.                                             #
#--------------------------------------------------------------------------- #

#------------------------------------------------------------------------------
# Configuration File for File-System based Image Repositories
#   - IMAGE_REPOSITORY: Path where the images will be stored
#   - RESTRICTED_DIRS: Paths that can not be used to register images
#   - SAFE_DIRS: Paths that are safe to specify image paths 
#------------------------------------------------------------------------------
if [ -z "${ONE_LOCATION}" ]; then
    VAR_LOCATION=/var/lib/one/
    ETC_LOCATION=/etc/one/
else
    VAR_LOCATION=$ONE_LOCATION/var/
    ETC_LOCATION=$ONE_LOCATION/etc/
fi

CONF_FILE=$ETC_LOCATION/image/fs.conf

source $CONF_FILE

if [ -z "${IMAGE_REPOSITORY_PATH}" ]; then
	if [ -z "${ONE_LOCATION}" ]; then
	    IMAGE_REPOSITORY_PATH=/var/lib/one/images
	else
	    IMAGE_REPOSITORY_PATH=$ONE_LOCATION/var/images
	fi
fi

RESTRICTED_DIRS="$VAR_LOCATION $ETC_LOCATION $HOME/ $RESTRICTED_DIRS"

export IMAGE_REPOSITORY_PATH
export RESTRICTED_DIRS
export SAFE_DIRS

#------------------------------------------------------------------------------
# Function used to generate Image names, you should not need to override this
#------------------------------------------------------------------------------
function generate_image_path {

CANONICAL_STR="`$DATE +%s`:$ID"

CANONICAL_MD5=$($MD5SUM - << EOF
$CANONICAL_STR
EOF
)

echo "$IMAGE_REPOSITORY_PATH/`echo $CANONICAL_MD5 | cut -d ' ' -f1`"
}

function fs_du {
	if [ -d "$1" ]; then
		SIZE=`du -s "$1" | cut -f1`
		error=$?
	else
		SIZE=`stat -c %s "$1"`
		error=$?
	fi

	if [ $error -ne 0 ]; then
		SIZE=0
	else
		SIZE=$(($SIZE/1048576))
	fi

	echo "$SIZE"
}

function check_restricted {
	for path in $SAFE_DIRS ; do
		if [ -n "`readlink -f $1 | grep -E "^$path"`" ] ; then
			echo 0
			return
		fi
	done

	for path in $RESTRICTED_DIRS ; do
		if [ -n "`readlink -f $1 | grep -E "^$path"`" ] ; then
			echo 1
			return
		fi
    done

  	echo 0
}
