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 *  getFinderFlag.c
25 *  bless
26 *
27 *  Created by Shantonu Sen <ssen@apple.com> on Thu Jul 05 2001.
28 *  Copyright (c) 2001-2007 Apple Inc. All Rights Reserved.
29 *
30 *  $Id: BLSetFinderFlag.c,v 1.17 2006/02/20 22:49:54 ssen Exp $
31 *
32 */
33
34#include <CoreFoundation/CoreFoundation.h>
35
36#include <sys/types.h>
37#include <sys/attr.h>
38#include <unistd.h>
39
40#include "bless.h"
41#include "bless_private.h"
42
43struct TwoUInt16 {
44    uint16_t first;
45    uint16_t second;
46};
47
48struct fileinfobuf {
49  uint32_t info_length;
50  uint32_t finderinfo[8];
51};
52
53
54int BLSetFinderFlag(BLContextPtr context, const char * path, uint16_t flag, int setval) {
55    struct attrlist		alist;
56    struct fileinfobuf finfo;
57    struct TwoUInt16 *twoUint = (struct TwoUInt16 *)&finfo.finderinfo[2];
58    int err;
59
60    alist.bitmapcount = 5;
61    alist.reserved = 0;
62    alist.commonattr = ATTR_CMN_FNDRINFO;
63    alist.volattr = 0;
64    alist.dirattr = 0;
65    alist.fileattr = 0;
66    alist.forkattr = 0;
67
68	err = getattrlist(path, &alist, &finfo, sizeof(finfo), 0);
69    if(err) {
70        contextprintf(context, kBLLogLevelError,  "Can't file information for %s\n", path );
71        return 1;
72    }
73
74    if(setval) {
75        /* we want to set the bit. so OR with the flag  */
76        twoUint->first |= CFSwapInt16HostToBig(flag);
77    } else {
78        /* AND with a mask  that excludes flag*/
79        uint16_t mask = CFSwapInt16HostToBig(flag);;
80	mask = ~mask;
81        twoUint->first &= mask;
82    }
83
84	err = setattrlist(path, &alist, &finfo.finderinfo, sizeof(finfo.finderinfo), 0);
85    if(err) {
86        contextprintf(context, kBLLogLevelError,  "Error while setting file information for %s\n", path );
87        return 2;
88    }
89
90    return 0;
91}
92