4 * The contents of this file are subject to the terms of the
5 * Common Development and Distribution License (the "License").
6 * You may not use this file except in compliance with the License.
8 * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
9 * or http://www.opensolaris.org/os/licensing.
10 * See the License for the specific language governing permissions
11 * and limitations under the License.
13 * When distributing Covered Code, include this CDDL HEADER in each
14 * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
15 * If applicable, add the following below this CDDL HEADER, with the
16 * fields enclosed by brackets "[]" replaced with your own identifying
17 * information: Portions Copyright [yyyy] [name of copyright owner]
22 * Copyright 2010 Sun Microsystems, Inc. All rights reserved.
23 * Use is subject to license terms.
29 #include <sys/fs_reparse.h>
30 #include <smbsrv/libsmb.h>
34 static int smb_reparse_init(const char *, nvlist_t
**);
35 static void smb_reparse_free(nvlist_t
*);
36 static int smb_reparse_set(const char *, nvlist_t
*);
39 * Checks the status of the object specified by 'path'
41 * Returns 0 and fills 'stat' with the proper status on
42 * success, otherwise returns an error code.
45 smb_reparse_stat(const char *path
, uint32_t *stat
)
48 char symbuf
[MAXREPARSELEN
];
51 if (lstat(path
, &statbuf
) != 0) {
52 if (errno
== ENOENT
) {
53 *stat
= SMB_REPARSE_NOTFOUND
;
59 if ((statbuf
.st_mode
& S_IFMT
) != S_IFLNK
) {
60 *stat
= SMB_REPARSE_NOTREPARSE
;
64 bzero(symbuf
, MAXREPARSELEN
);
65 if (readlink(path
, symbuf
, MAXREPARSELEN
) == -1)
68 rptaglen
= strlen(FS_REPARSE_TAG_STR
);
69 if (strncmp(symbuf
, FS_REPARSE_TAG_STR
, rptaglen
) != 0)
70 *stat
= SMB_REPARSE_NOTREPARSE
;
72 *stat
= SMB_REPARSE_ISREPARSE
;
78 * If the reparse point specified by the path already exists
79 * it is updated by given service type and its data. Update means
80 * that if such service type does not already exist, it is added
81 * otherwise it is overwritten by given data.
83 * If the reparse point does not exist, one is created with given
84 * service type and its data.
87 smb_reparse_svcadd(const char *path
, const char *svctype
, const char *svcdata
)
92 if ((rc
= smb_reparse_init(path
, &nvl
)) != 0)
95 if ((rc
= reparse_add(nvl
, svctype
, svcdata
)) != 0) {
96 smb_reparse_free(nvl
);
100 rc
= smb_reparse_set(path
, nvl
);
101 smb_reparse_free(nvl
);
107 * Removes the entry for the given service type from the
108 * specified reparse point. If there is no service entry
109 * left, the reparse point object will be deleted.
112 smb_reparse_svcdel(const char *path
, const char *svctype
)
117 if ((rc
= smb_reparse_init(path
, &nvl
)) != 0)
120 if ((rc
= reparse_remove(nvl
, svctype
)) != 0) {
121 smb_reparse_free(nvl
);
125 if (nvlist_next_nvpair(nvl
, NULL
) == NULL
) {
126 /* list is empty remove the object */
127 rc
= reparse_delete(path
);
128 if ((rc
!= 0) && (rc
== ENOENT
))
131 rc
= smb_reparse_set(path
, nvl
);
134 smb_reparse_free(nvl
);
139 * Obtains data of the given service type from the specified
140 * reparse point. Function allocates the memory needed to hold
141 * the service data so the caller must free this memory by
144 * If 'svcdata' is NULL, successful return means that the reparse
145 * point contains a record for the given service type.
148 smb_reparse_svcget(const char *path
, const char *svctype
, char **svcdata
)
155 if ((rc
= smb_reparse_init(path
, &nvl
)) != 0)
159 nvp
= nvlist_next_nvpair(nvl
, NULL
);
161 while (nvp
!= NULL
) {
162 stype
= nvpair_name(nvp
);
164 if ((stype
!= NULL
) && (strcasecmp(stype
, svctype
) == 0)) {
165 if ((rc
= nvpair_value_string(nvp
, &sdata
)) != 0)
168 if (svcdata
!= NULL
) {
169 if ((*svcdata
= strdup(sdata
)) == NULL
)
176 nvp
= nvlist_next_nvpair(nvl
, nvp
);
179 smb_reparse_free(nvl
);
184 * Initializes the given nvpair list.
186 * This function assumes that the object specified by this path
187 * is a reparse point, so it does not do any verification.
189 * If specified reparse point does not exist the function
190 * returns successfully with an empty nvpair list.
192 * If the object exists and readlink is successful then nvpair
193 * list is polulated with the reparse service information, otherwise
194 * an error code is returned.
197 smb_reparse_init(const char *path
, nvlist_t
**nvl
)
199 char rp_data
[MAXREPARSELEN
];
202 if ((*nvl
= reparse_init()) == NULL
)
205 bzero(rp_data
, MAXREPARSELEN
);
206 if ((rc
= readlink(path
, rp_data
, MAXREPARSELEN
)) == -1) {
214 if ((rc
= reparse_parse(rp_data
, *nvl
)) != 0) {
226 smb_reparse_free(nvlist_t
*nvl
)
232 * Create a reparse point with given services in the passed
233 * nvlist. If the reparse point already exists, it will be
234 * deleted and a new one with the given data is created.
237 smb_reparse_set(const char *path
, nvlist_t
*nvl
)
242 if ((rc
= reparse_unparse(nvl
, &rp_data
)) != 0)
245 rc
= reparse_delete(path
);
246 if ((rc
!= 0) && (rc
!= ENOENT
)) {
251 rc
= reparse_create(path
, rp_data
);