Advertisement
Guest User

appendattr.cpp

a guest
Oct 31st, 2014
161
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 1.35 KB | None | 0 0
  1. #include <Node.h>
  2. #include <TypeConstants.h>
  3.  
  4. #include <fs_attr.h>
  5.  
  6. #include <stdio.h>
  7. #include <string.h>
  8.  
  9.  
  10. int
  11. main(int argc, char *argv[])
  12. {
  13.     if (argc < 3) {
  14.         printf("usage: %s <fileName> <attributeName> <valueToAppend>\n",
  15.             argv[0]);
  16.         exit(1);
  17.     }
  18.  
  19.     const char *fileName = argv[1];
  20.     const char *attrName = argv[2];
  21.     const char *value = argv[3];
  22.  
  23.     BNode node(fileName);
  24.     status_t result = node.InitCheck();
  25.     if (result != B_OK) {
  26.         printf("failed to open node \"%s\": %s\n", fileName, strerror(result));
  27.         exit(2);
  28.     }
  29.  
  30.     attr_info attrInfo;
  31.     result = node.GetAttrInfo(attrName, &attrInfo);
  32.     if (result == B_ENTRY_NOT_FOUND) {
  33.         // No attribute yet, pretend it's there but has length 0
  34.         attrInfo.type = B_STRING_TYPE;
  35.         attrInfo.size = 0;
  36.     } else if (result != B_OK) {
  37.         printf("failed to get info for attribute \"%s\": %s\n", attrName,
  38.             strerror(result));
  39.         exit(3);
  40.     }
  41.  
  42.     if (attrInfo.type != B_STRING_TYPE) {
  43.         printf("attribute \"%s\" isn't a string, aborting\n", attrName);
  44.         exit(4);
  45.     }
  46.  
  47.     ssize_t written = node.WriteAttr(attrName, attrInfo.type, attrInfo.size,
  48.         value, strlen(value));
  49.     if (written < 0) {
  50.         printf("failed to append to attribute: %s\n", strerror(written));
  51.         exit(5);
  52.     }
  53.  
  54.     printf("appended %" B_PRIdSSIZE " bytes to to attribute \"%s\" of \"%s\"\n",
  55.         written, attrName, fileName);
  56.     exit(0);
  57. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement