1/*
2 * Copyright (c) 2001-2007 Apple Inc. All Rights Reserved.
3 *
4 * @APPLE_LICENSE_HEADER_START@
5 *
6 * This file contains Original Code and/or Modifications of Original Code
7 * as defined in and that are subject to the Apple Public Source License
8 * Version 2.0 (the 'License'). You may not use this file except in
9 * compliance with the License. Please obtain a copy of the License at
10 * http://www.opensource.apple.com/apsl/ and read it before using this
11 * file.
12 *
13 * The Original Code and all software distributed under the License are
14 * distributed on an 'AS IS' basis, WITHOUT WARRANTY OF ANY KIND, EITHER
15 * EXPRESS OR IMPLIED, AND APPLE HEREBY DISCLAIMS ALL SUCH WARRANTIES,
16 * INCLUDING WITHOUT LIMITATION, ANY WARRANTIES OF MERCHANTABILITY,
17 * FITNESS FOR A PARTICULAR PURPOSE, QUIET ENJOYMENT OR NON-INFRINGEMENT.
18 * Please see the License for the specific language governing rights and
19 * limitations under the License.
20 *
21 * @APPLE_LICENSE_HEADER_END@
22 */
23/*
24 *  BLGetCommonMountPoint.c
25 *  bless
26 *
27 *  Created by Shantonu Sen <ssen@apple.com> on Tue Apr 17 2001.
28 *  Copyright (c) 2001-2007 Apple Inc. All Rights Reserved.
29 *
30 *  $Id: BLGetCommonMountPoint.c,v 1.14 2006/02/20 22:49:56 ssen Exp $
31 *
32 */
33
34#include <stdio.h>
35#include <string.h>
36#include <sys/stat.h>
37#include <sys/param.h>
38#include <sys/mount.h>
39
40#include "bless.h"
41#include "bless_private.h"
42
43int BLGetCommonMountPoint(BLContextPtr context, const char * f1,
44    const char * f2, char * mountp) {
45
46    struct statfs fsinfo;
47    int err;
48    char f2mount[MNAMELEN];
49
50    if(f1[0] != '\0') {
51		err = statfs(f1, &fsinfo);
52      if(err) {
53        contextprintf(context, kBLLogLevelError,  "No mount point for %s\n", f1 );
54        return 1;
55      } else {
56	strncpy(mountp, fsinfo.f_mntonname, MNAMELEN-1);
57	mountp[MNAMELEN-1] = '\0';
58	contextprintf(context, kBLLogLevelVerbose,  "Mount point for %s is %s\n", f1, mountp );
59      }
60    }
61
62    if(f2[0] != '\0') {
63		err = statfs(f2, &fsinfo);
64      if(err) {
65        contextprintf(context, kBLLogLevelError,  "No mount point for %s\n", f2 );
66        return 2;
67      } else {
68	strncpy(f2mount, fsinfo.f_mntonname, MNAMELEN-1);
69	f2mount[MNAMELEN-1] = '\0';
70	contextprintf(context, kBLLogLevelVerbose,  "Mount point for %s is %s\n", f2, f2mount );
71      }
72    }
73
74    /* Now we have the mount points of any folders that were passed
75     * in. We must determine:
76     * 1) if f1 && f2, find a common mount point or err
77     * 2) if f2 && !f1, copy f2mount -> mountp
78     * 3) if f1 && !f2, just return success
79     */
80
81    if(f2[0] != '\0') {
82      /* Case 1, 2 */
83      if(f1[0] != '\0') {
84	/* Case 1 */
85	if(strcmp(mountp, f2mount)) {
86	  /* no common */
87	  mountp[0] = '\0';
88	  return 3;
89	} else {
90	  /* yay common */
91	  return 0;
92	}
93      } else {
94	/* Case 2 */
95
96	/* We know each buffer is <MNAMELEN and 0-terminated */
97	strncpy(mountp, f2mount, MNAMELEN);
98	return 0;
99      }
100    } else {
101      /* Case 3 */
102      return 0;
103    }
104
105    contextprintf(context, kBLLogLevelError,  "No folders specified" );
106    return 4;
107}
108