Only apply change to attribute if needed (idempotent)

Previous version always applied change when setting an attribute,
independently if the attribute was already set to the desired value.
This patch now check the existing value before applying change, and
will apply the change only if needed. This is particularly useful when
running in check to determine if changes to a file are needed.