7b576479d535a21da661ff1c6c59b910d4680aeavboxsync/* $Id$ */
7b576479d535a21da661ff1c6c59b910d4680aeavboxsync/** @file
7b576479d535a21da661ff1c6c59b910d4680aeavboxsync * IPRT - Assertions, generic RTPathIsSame.
7b576479d535a21da661ff1c6c59b910d4680aeavboxsync */
7b576479d535a21da661ff1c6c59b910d4680aeavboxsync
7b576479d535a21da661ff1c6c59b910d4680aeavboxsync/*
7b576479d535a21da661ff1c6c59b910d4680aeavboxsync * Copyright (C) 2013 Oracle Corporation
7b576479d535a21da661ff1c6c59b910d4680aeavboxsync *
7b576479d535a21da661ff1c6c59b910d4680aeavboxsync * This file is part of VirtualBox Open Source Edition (OSE), as
7b576479d535a21da661ff1c6c59b910d4680aeavboxsync * available from http://www.virtualbox.org. This file is free software;
7b576479d535a21da661ff1c6c59b910d4680aeavboxsync * you can redistribute it and/or modify it under the terms of the GNU
7b576479d535a21da661ff1c6c59b910d4680aeavboxsync * General Public License (GPL) as published by the Free Software
7b576479d535a21da661ff1c6c59b910d4680aeavboxsync * Foundation, in version 2 as it comes in the "COPYING" file of the
7b576479d535a21da661ff1c6c59b910d4680aeavboxsync * VirtualBox OSE distribution. VirtualBox OSE is distributed in the
7b576479d535a21da661ff1c6c59b910d4680aeavboxsync * hope that it will be useful, but WITHOUT ANY WARRANTY of any kind.
7b576479d535a21da661ff1c6c59b910d4680aeavboxsync *
7b576479d535a21da661ff1c6c59b910d4680aeavboxsync * The contents of this file may alternatively be used under the terms
7b576479d535a21da661ff1c6c59b910d4680aeavboxsync * of the Common Development and Distribution License Version 1.0
7b576479d535a21da661ff1c6c59b910d4680aeavboxsync * (CDDL) only, as it comes in the "COPYING.CDDL" file of the
7b576479d535a21da661ff1c6c59b910d4680aeavboxsync * VirtualBox OSE distribution, in which case the provisions of the
7b576479d535a21da661ff1c6c59b910d4680aeavboxsync * CDDL are applicable instead of those of the GPL.
7b576479d535a21da661ff1c6c59b910d4680aeavboxsync *
7b576479d535a21da661ff1c6c59b910d4680aeavboxsync * You may elect to license modified versions of this file under the
7b576479d535a21da661ff1c6c59b910d4680aeavboxsync * terms and conditions of either the GPL or the CDDL or both.
7b576479d535a21da661ff1c6c59b910d4680aeavboxsync */
7b576479d535a21da661ff1c6c59b910d4680aeavboxsync
7b576479d535a21da661ff1c6c59b910d4680aeavboxsync
7b576479d535a21da661ff1c6c59b910d4680aeavboxsync/*******************************************************************************
7b576479d535a21da661ff1c6c59b910d4680aeavboxsync* Header Files *
7b576479d535a21da661ff1c6c59b910d4680aeavboxsync*******************************************************************************/
7b576479d535a21da661ff1c6c59b910d4680aeavboxsync#include <iprt/path.h>
7b576479d535a21da661ff1c6c59b910d4680aeavboxsync#include "internal/iprt.h"
7b576479d535a21da661ff1c6c59b910d4680aeavboxsync
7b576479d535a21da661ff1c6c59b910d4680aeavboxsync#include <iprt/string.h>
7b576479d535a21da661ff1c6c59b910d4680aeavboxsync
7b576479d535a21da661ff1c6c59b910d4680aeavboxsync
7b576479d535a21da661ff1c6c59b910d4680aeavboxsyncRTDECL(int) RTPathIsSame(const char *pszPath1, const char *pszPath2)
7b576479d535a21da661ff1c6c59b910d4680aeavboxsync{
7b576479d535a21da661ff1c6c59b910d4680aeavboxsync /*
7b576479d535a21da661ff1c6c59b910d4680aeavboxsync * Simple checks based on the path values.
7b576479d535a21da661ff1c6c59b910d4680aeavboxsync */
7b576479d535a21da661ff1c6c59b910d4680aeavboxsync if (pszPath1 == pszPath2)
7b576479d535a21da661ff1c6c59b910d4680aeavboxsync return true;
7b576479d535a21da661ff1c6c59b910d4680aeavboxsync if (!pszPath1)
7b576479d535a21da661ff1c6c59b910d4680aeavboxsync return false;
7b576479d535a21da661ff1c6c59b910d4680aeavboxsync if (!pszPath2)
7b576479d535a21da661ff1c6c59b910d4680aeavboxsync return false;
7b576479d535a21da661ff1c6c59b910d4680aeavboxsync if (!strcmp(pszPath1, pszPath2))
7b576479d535a21da661ff1c6c59b910d4680aeavboxsync return true;
7b576479d535a21da661ff1c6c59b910d4680aeavboxsync
7b576479d535a21da661ff1c6c59b910d4680aeavboxsync /*
7b576479d535a21da661ff1c6c59b910d4680aeavboxsync * If the files exist, try use the attributes.
7b576479d535a21da661ff1c6c59b910d4680aeavboxsync */
7b576479d535a21da661ff1c6c59b910d4680aeavboxsync RTFSOBJINFO ObjInfo1, ObjInfo2;
7b576479d535a21da661ff1c6c59b910d4680aeavboxsync int rc = RTPathQueryInfoEx(pszPath1, &ObjInfo1, RTFSOBJATTRADD_UNIX, RTPATH_F_ON_LINK);
7b576479d535a21da661ff1c6c59b910d4680aeavboxsync if (RT_SUCCESS(rc))
7b576479d535a21da661ff1c6c59b910d4680aeavboxsync rc = RTPathQueryInfoEx(pszPath2, &ObjInfo2, RTFSOBJATTRADD_UNIX, RTPATH_F_ON_LINK);
7b576479d535a21da661ff1c6c59b910d4680aeavboxsync if (RT_SUCCESS(rc))
7b576479d535a21da661ff1c6c59b910d4680aeavboxsync {
7b576479d535a21da661ff1c6c59b910d4680aeavboxsync if ((ObjInfo1.Attr.fMode & RTFS_TYPE_MASK) != (ObjInfo2.Attr.fMode & RTFS_TYPE_MASK))
7b576479d535a21da661ff1c6c59b910d4680aeavboxsync return false;
7b576479d535a21da661ff1c6c59b910d4680aeavboxsync if (ObjInfo1.Attr.u.Unix.INodeIdDevice != ObjInfo2.Attr.u.Unix.INodeIdDevice)
7b576479d535a21da661ff1c6c59b910d4680aeavboxsync return false;
7b576479d535a21da661ff1c6c59b910d4680aeavboxsync if (ObjInfo1.Attr.u.Unix.INodeId != ObjInfo2.Attr.u.Unix.INodeId)
7b576479d535a21da661ff1c6c59b910d4680aeavboxsync return false;
7b576479d535a21da661ff1c6c59b910d4680aeavboxsync if (ObjInfo1.Attr.u.Unix.GenerationId != ObjInfo2.Attr.u.Unix.GenerationId)
7b576479d535a21da661ff1c6c59b910d4680aeavboxsync return false;
7b576479d535a21da661ff1c6c59b910d4680aeavboxsync if ( ObjInfo1.Attr.u.Unix.INodeIdDevice != 0
7b576479d535a21da661ff1c6c59b910d4680aeavboxsync && ObjInfo1.Attr.u.Unix.INodeId != 0)
7b576479d535a21da661ff1c6c59b910d4680aeavboxsync return true;
7b576479d535a21da661ff1c6c59b910d4680aeavboxsync }
7b576479d535a21da661ff1c6c59b910d4680aeavboxsync
7b576479d535a21da661ff1c6c59b910d4680aeavboxsync /*
7b576479d535a21da661ff1c6c59b910d4680aeavboxsync * Fallback, compare absolute/real paths. Return failure on paths that are
7b576479d535a21da661ff1c6c59b910d4680aeavboxsync * too long.
7b576479d535a21da661ff1c6c59b910d4680aeavboxsync */
7b576479d535a21da661ff1c6c59b910d4680aeavboxsync char szPath1[RTPATH_MAX];
50370d07616e900f524ff4011cd74a66d64ea212vboxsync rc = RTPathAbs(pszPath1, szPath1, sizeof(szPath1));
7b576479d535a21da661ff1c6c59b910d4680aeavboxsync AssertRCReturn(rc, VERR_FILENAME_TOO_LONG);
7b576479d535a21da661ff1c6c59b910d4680aeavboxsync
7b576479d535a21da661ff1c6c59b910d4680aeavboxsync char szPath2[RTPATH_MAX];
50370d07616e900f524ff4011cd74a66d64ea212vboxsync rc = RTPathAbs(pszPath2, szPath2, sizeof(szPath2)); AssertRC(rc);
7b576479d535a21da661ff1c6c59b910d4680aeavboxsync AssertRCReturn(rc, VERR_FILENAME_TOO_LONG);
7b576479d535a21da661ff1c6c59b910d4680aeavboxsync
7b576479d535a21da661ff1c6c59b910d4680aeavboxsync if (RTPathCompare(szPath1, szPath2) == 0)
7b576479d535a21da661ff1c6c59b910d4680aeavboxsync return true;
7b576479d535a21da661ff1c6c59b910d4680aeavboxsync
7b576479d535a21da661ff1c6c59b910d4680aeavboxsync /** @todo Relsolve any symbolic links in the paths. Too lazy for that right
7b576479d535a21da661ff1c6c59b910d4680aeavboxsync * now. */
7b576479d535a21da661ff1c6c59b910d4680aeavboxsync return false;
7b576479d535a21da661ff1c6c59b910d4680aeavboxsync}
7b576479d535a21da661ff1c6c59b910d4680aeavboxsyncRT_EXPORT_SYMBOL(RTPathIsSame);
7b576479d535a21da661ff1c6c59b910d4680aeavboxsync