0

The following is always meeting the ENOENT condition at the end. I would like to print ENODATA. What do I need to change in setxattr?

#include <sys/xattr.h>
#include <stdio.h>
#include <errno.h>
#include <string.h>

int main() {
  int ret;
  const char * file = "non_existent_file.txt";
  const char * name = "user.test";
  const char * value = "value";
  ssize_t size = strlen(value);
  printf("Setting Xattr\n");
  ret = setxattr(file, name, value, size, XATTR_REPLACE);
  if (ret == -1) {
    printf("Xattr set failed: %s\n", strerror(errno));
    perror("");
  }
  if (errno == ENODATA) {
    printf("ENODATA\n");
  }
  if (errno == ENOENT) {
    printf("ENOENT\n");
  }
  return 0;
} 

I get “ENOENT” printed from last condition end statements

I tried adding the XATTR_REPLACE flag. I’ve removed valid paths for the file path

Edit:

Target OS: Ubuntu/ Linux

Charles Duffy
  • 280,126
  • 43
  • 390
  • 441
dodobirdie
  • 13
  • 3
  • 1
    How could you get ENODATA when you're only testing with non-existing filenames? Trying to find the file happens before any look at which xattrs are available for that file. – Charles Duffy Feb 13 '23 at 21:33
  • 1
    Mind, the details of which error is returned when is OS-dependent -- you should really specify a particular target OS here; but the above is likely to apply everywhere. – Charles Duffy Feb 13 '23 at 21:37
  • @CharlesDuffy what changes would you make? My target system is Ubuntu/ Linux. It should be giving me ENODATA. I tried putting in a valid path and making other things non existent but that didn’t work. – dodobirdie Feb 13 '23 at 21:56
  • 1
    "Should be giving me ENODATA" -- no, it shouldn't, if the file doesn't exist. If you still get the same problem with a filename that _does_ exist and XATTR_REPLACE, _show us that_. Right now, the obvious interpretation is that you're misremembering the order of operations you performed your tests in, and tested with a filename that _did_ exist only **before** you added `XATTR_REPLACE`. – Charles Duffy Feb 14 '23 at 00:16
  • See https://replit.com/@CharlesDuffy2/WearableTwinActivecontent#main.c, successfully returning ENODATA; the only change I made was to refer to a file that actually does exist. – Charles Duffy Feb 14 '23 at 00:32
  • Also, `errno` is valid *only* after a call to a function documented to set `errno` fails. If you call a function *not* documented to set `errno`, it's free to change the value of `errno`. You can't make a series of calls to `printf()`, for example, and expect to retain a value in `errno` for the call that failed three or four function calls previous. – Andrew Henle Feb 14 '23 at 00:35

0 Answers0