예제 #1
0
        /// <exception cref="System.IO.IOException"/>
        internal static IList <XAttr> SetINodeXAttrs(FSDirectory fsd, IList <XAttr> existingXAttrs
                                                     , IList <XAttr> toSet, EnumSet <XAttrSetFlag> flag)
        {
            // Check for duplicate XAttrs in toSet
            // We need to use a custom comparator, so using a HashSet is not suitable
            for (int i = 0; i < toSet.Count; i++)
            {
                for (int j = i + 1; j < toSet.Count; j++)
                {
                    if (toSet[i].EqualsIgnoreValue(toSet[j]))
                    {
                        throw new IOException("Cannot specify the same XAttr to be set " + "more than once"
                                              );
                    }
                }
            }
            // Count the current number of user-visible XAttrs for limit checking
            int userVisibleXAttrsNum = 0;
            // Number of user visible xAttrs
            // The XAttr list is copied to an exactly-sized array when it's stored,
            // so there's no need to size it precisely here.
            int newSize = (existingXAttrs != null) ? existingXAttrs.Count : 0;

            newSize += toSet.Count;
            IList <XAttr> xAttrs = Lists.NewArrayListWithCapacity(newSize);

            // Check if the XAttr already exists to validate with the provided flag
            foreach (XAttr xAttr in toSet)
            {
                bool exist = false;
                if (existingXAttrs != null)
                {
                    foreach (XAttr a in existingXAttrs)
                    {
                        if (a.EqualsIgnoreValue(xAttr))
                        {
                            exist = true;
                            break;
                        }
                    }
                }
                XAttrSetFlag.Validate(xAttr.GetName(), exist, flag);
                // add the new XAttr since it passed validation
                xAttrs.AddItem(xAttr);
                if (IsUserVisible(xAttr))
                {
                    userVisibleXAttrsNum++;
                }
            }
            // Add the existing xattrs back in, if they weren't already set
            if (existingXAttrs != null)
            {
                foreach (XAttr existing in existingXAttrs)
                {
                    bool alreadySet = false;
                    foreach (XAttr set in toSet)
                    {
                        if (set.EqualsIgnoreValue(existing))
                        {
                            alreadySet = true;
                            break;
                        }
                    }
                    if (!alreadySet)
                    {
                        xAttrs.AddItem(existing);
                        if (IsUserVisible(existing))
                        {
                            userVisibleXAttrsNum++;
                        }
                    }
                }
            }
            if (userVisibleXAttrsNum > fsd.GetInodeXAttrsLimit())
            {
                throw new IOException("Cannot add additional XAttr to inode, " + "would exceed limit of "
                                      + fsd.GetInodeXAttrsLimit());
            }
            return(xAttrs);
        }