2 * Copyright 2004-2015, Axel Dörfler, axeld@pinc-software.de.
3 * Copyright 2002, Sebastian Nozzi.
5 * Distributed under the terms of the MIT license.
11 #include <TypeConstants.h>
16 # include <parsedate.h>
29 writeAttrValue(int fd
, const char* name
, type_code type
, Type value
)
31 ssize_t bytes
= fs_write_attr(fd
, name
, type
, 0, &value
, sizeof(Type
));
39 /*! Writes an attribute to a node, taking the type into account and
40 converting the value accordingly
42 On success it will return the amount of bytes written
43 On failure it returns an error code (negative number)
46 writeAttr(int fd
, type_code type
, const char* name
, const char* value
, size_t length
)
48 uint64 uint64value
= 0;
50 double floatValue
= 0.0;
52 // parse number input at once
60 int64value
= strtoll(value
, NULL
, 0);
67 uint64value
= strtoull(value
, NULL
, 0);
72 floatValue
= strtod(value
, NULL
);
78 return writeAttrValue
<int8
>(fd
, name
, type
, (int8
)int64value
);
80 return writeAttrValue
<int16
>(fd
, name
, type
, (int16
)int64value
);
82 return writeAttrValue
<int32
>(fd
, name
, type
, (int32
)int64value
);
84 return writeAttrValue
<int64
>(fd
, name
, type
, int64value
);
87 return writeAttrValue
<uint8
>(fd
, name
, type
, (uint8
)uint64value
);
89 return writeAttrValue
<uint16
>(fd
, name
, type
, (uint16
)uint64value
);
91 return writeAttrValue
<uint32
>(fd
, name
, type
, (uint32
)uint64value
);
93 return writeAttrValue
<uint64
>(fd
, name
, type
, uint64value
);
96 return writeAttrValue
<float>(fd
, name
, type
, (float)floatValue
);
98 return writeAttrValue
<double>(fd
, name
, type
, (double)floatValue
);
104 if (!strcasecmp(value
, "true") || !strcasecmp(value
, "t")
105 || !strcasecmp(value
, "on") || !strcasecmp(value
, "enabled")
106 || (isdigit(value
[0]) && int64value
== 1))
108 else if (!strcasecmp(value
, "false") || !strcasecmp(value
, "f")
109 || !strcasecmp(value
, "off") || !strcasecmp(value
, "disabled")
110 || (isdigit(value
[0]) && int64value
== 0))
115 return writeAttrValue
<uint8
>(fd
, name
, B_BOOL_TYPE
, boolValue
);
121 time_t timeValue
= parsedate(value
, time(NULL
));
125 return writeAttrValue
<time_t>(fd
, name
, B_TIME_TYPE
, timeValue
);
130 case B_MIME_STRING_TYPE
:
132 // For string, mime-strings and any other type we just write the value
133 // Note that the trailing NULL is added. If a length was given, we write
134 // the value directly, though.
135 ssize_t bytes
= fs_write_attr(fd
, name
, type
, 0, value
,
136 length
? length
: strlen(value
) + 1);
145 /*! Adds an attribute to a file for the given type, name and value
146 Converts the value accordingly in case of numeric or boolean types
148 On success, it returns B_OK, or else an appropriate error code.
151 addAttr(const char* file
, type_code type
, const char* name
,
152 const char* value
, size_t length
, bool resolveLinks
)
154 int fd
= open(file
, O_RDONLY
| (resolveLinks
? 0 : O_NOTRAVERSE
));
158 fs_remove_attr(fd
, name
);
159 ssize_t bytes
= writeAttr(fd
, type
, name
, value
, length
);
163 return bytes
>= 0 ? B_OK
: bytes
;