1 | /* $Id: RTPathUserDocuments-darwin.cpp 56290 2015-06-09 14:01:31Z vboxsync $ */
|
---|
2 | /** @file
|
---|
3 | * IPRT - RTPathUserDocuments, darwin ring-3.
|
---|
4 | */
|
---|
5 |
|
---|
6 | /*
|
---|
7 | * Copyright (C) 2011-2015 Oracle Corporation
|
---|
8 | *
|
---|
9 | * This file is part of VirtualBox Open Source Edition (OSE), as
|
---|
10 | * available from http://www.virtualbox.org. This file is free software;
|
---|
11 | * you can redistribute it and/or modify it under the terms of the GNU
|
---|
12 | * General Public License (GPL) as published by the Free Software
|
---|
13 | * Foundation, in version 2 as it comes in the "COPYING" file of the
|
---|
14 | * VirtualBox OSE distribution. VirtualBox OSE is distributed in the
|
---|
15 | * hope that it will be useful, but WITHOUT ANY WARRANTY of any kind.
|
---|
16 | *
|
---|
17 | * The contents of this file may alternatively be used under the terms
|
---|
18 | * of the Common Development and Distribution License Version 1.0
|
---|
19 | * (CDDL) only, as it comes in the "COPYING.CDDL" file of the
|
---|
20 | * VirtualBox OSE distribution, in which case the provisions of the
|
---|
21 | * CDDL are applicable instead of those of the GPL.
|
---|
22 | *
|
---|
23 | * You may elect to license modified versions of this file under the
|
---|
24 | * terms and conditions of either the GPL or the CDDL or both.
|
---|
25 | */
|
---|
26 |
|
---|
27 | /*******************************************************************************
|
---|
28 | * Header Files *
|
---|
29 | *******************************************************************************/
|
---|
30 | #include <iprt/path.h>
|
---|
31 | #include "internal/iprt.h"
|
---|
32 |
|
---|
33 | #include <iprt/assert.h>
|
---|
34 | #include <iprt/string.h>
|
---|
35 | #include <iprt/err.h>
|
---|
36 |
|
---|
37 | #include <NSSystemDirectories.h>
|
---|
38 | #include <sys/syslimits.h>
|
---|
39 | #ifdef IPRT_USE_CORE_SERVICE_FOR_USER_DOCUMENTS
|
---|
40 | # include <CoreServices/CoreServices.h>
|
---|
41 | #endif
|
---|
42 |
|
---|
43 |
|
---|
44 | RTDECL(int) RTPathUserDocuments(char *pszPath, size_t cchPath)
|
---|
45 | {
|
---|
46 | /*
|
---|
47 | * Validate input
|
---|
48 | */
|
---|
49 | AssertPtrReturn(pszPath, VERR_INVALID_POINTER);
|
---|
50 | AssertReturn(cchPath, VERR_INVALID_PARAMETER);
|
---|
51 |
|
---|
52 | /*
|
---|
53 | * Try NSSystemDirectories first since that works for directories that doesn't exist.
|
---|
54 | */
|
---|
55 | int rc = VERR_PATH_NOT_FOUND;
|
---|
56 | NSSearchPathEnumerationState EnmState = NSStartSearchPathEnumeration(NSDocumentDirectory, NSUserDomainMask);
|
---|
57 | if (EnmState != 0)
|
---|
58 | {
|
---|
59 | char szTmp[PATH_MAX];
|
---|
60 | szTmp[0] = szTmp[PATH_MAX - 1] = '\0';
|
---|
61 | EnmState = NSGetNextSearchPathEnumeration(EnmState, szTmp);
|
---|
62 | if (EnmState != 0)
|
---|
63 | {
|
---|
64 | size_t cchTmp = strlen(szTmp);
|
---|
65 | if (cchTmp >= cchPath)
|
---|
66 | return VERR_BUFFER_OVERFLOW;
|
---|
67 |
|
---|
68 | if (szTmp[0] == '~' && szTmp[1] == '/')
|
---|
69 | {
|
---|
70 | /* Expand tilde. */
|
---|
71 | rc = RTPathUserHome(pszPath, cchPath - cchTmp + 2);
|
---|
72 | if (RT_FAILURE(rc))
|
---|
73 | return rc;
|
---|
74 | rc = RTPathAppend(pszPath, cchPath, &szTmp[2]);
|
---|
75 | }
|
---|
76 | else
|
---|
77 | rc = RTStrCopy(pszPath, cchPath, szTmp);
|
---|
78 | return rc;
|
---|
79 | }
|
---|
80 | }
|
---|
81 |
|
---|
82 | #ifdef IPRT_USE_CORE_SERVICE_FOR_USER_DOCUMENTS
|
---|
83 | /*
|
---|
84 | * Fall back on FSFindFolder in case the above should fail...
|
---|
85 | */
|
---|
86 | FSRef ref;
|
---|
87 | OSErr err = FSFindFolder(kOnAppropriateDisk, kDocumentsFolderType, false /* createFolder */, &ref);
|
---|
88 | if (err == noErr)
|
---|
89 | {
|
---|
90 | err = FSRefMakePath(&ref, (UInt8*)pszPath, cchPath);
|
---|
91 | if (err == noErr)
|
---|
92 | return VINF_SUCCESS;
|
---|
93 | }
|
---|
94 | #endif
|
---|
95 | Assert(RT_FAILURE_NP(rc));
|
---|
96 | return rc;
|
---|
97 | }
|
---|
98 |
|
---|