libisofs  1.5.4
About: libisofs is a library to create an ISO 9660 filesystem, supports extensions like RockRidge or Joliet, makes bootable ISO 9660, and records file attributes which are of interest for data backups.
  Fossies Dox: libisofs-1.5.4.tar.gz  ("unofficial" and yet experimental doxygen-generated source code documentation)  

aaip-os-freebsd.c File Reference
#include <ctype.h>
#include <sys/types.h>
#include <unistd.h>
#include <stdlib.h>
#include <string.h>
#include <stdio.h>
#include <sys/stat.h>
#include <errno.h>
#include <sys/statvfs.h>
Include dependency graph for aaip-os-freebsd.c:

Go to the source code of this file.

Functions

int aaip_local_attr_support (int flag)
 
int aaip_get_acl_text (char *path, char **text, int flag)
 
int aaip_get_attr_list (char *path, size_t *num_attrs, char ***names, size_t **value_lengths, char ***values, int flag)
 
int aaip_set_acl_text (char *path, char *text, int flag)
 
static void register_errno (int *errnos, int i, int in_errno)
 
int aaip_set_attr_list (char *path, size_t num_attrs, char **names, size_t *value_lengths, char **values, int *errnos, int flag)
 

Function Documentation

◆ aaip_get_acl_text()

int aaip_get_acl_text ( char *  path,
char **  text,
int  flag 
)

Definition at line 126 of file aaip-os-freebsd.c.

127 {
128 #ifdef Libisofs_with_aaip_acL
129  acl_t acl= NULL;
130 #endif
131  struct stat stbuf;
132  int ret;
133 
134  if(flag & (1 << 15)) {
135  if(*text != NULL)
136 #ifdef Libisofs_with_aaip_acL
137  acl_free(*text);
138 #else
139  free(*text);
140 #endif
141  *text= NULL;
142  return(1);
143  }
144 
145  *text= NULL;
146  if(flag & 32)
147  ret= stat(path, &stbuf);
148  else
149  ret= lstat(path, &stbuf);
150  if(ret == -1)
151  return(-1);
152  if((stbuf.st_mode & S_IFMT) == S_IFLNK) {
153  if(flag & 16)
154  return(2);
155  return(-2);
156  }
157 
158  /* Note: no ACL_TYPE_DEFAULT in FreeBSD */
159  if(flag & 1)
160  return(0);
161 
162 #ifdef Libisofs_with_aaip_acL
163 
164  acl= acl_get_file(path, ACL_TYPE_ACCESS);
165 
166  if(acl == NULL) {
167  if(errno == EOPNOTSUPP) {
168  /* filesystem does not support ACL */
169  if(flag & 16)
170  return(2);
171 
172  /* >>> ??? fake ACL from POSIX permissions ? */;
173 
174  return(0);
175  }
176  return(-1);
177  }
178  *text= acl_to_text(acl, NULL);
179  acl_free(acl);
180 
181 #else /* Libisofs_with_aaip_acL */
182 
183  /* ??? >>> Fake ACL */;
184 
185  return(0);
186 
187 #endif /* ! Libisofs_with_aaip_acL */
188 
189  if(*text == NULL)
190  return(-1);
191  if(flag & 16) {
192  ret = aaip_cleanout_st_mode(*text, &(stbuf.st_mode), 2);
193  if(!(ret & (7 | 64)))
194  (*text)[0]= 0;
195  if((*text)[0] == 0 || strcmp(*text, "\n") == 0) {
196 #ifdef Libisofs_with_aaip_acL
197  acl_free(*text);
198 #else
199  free(*text);
200 #endif
201  *text= NULL;
202  return(2);
203  }
204  }
205  return(1);
206 }
int aaip_cleanout_st_mode(char *acl_text, mode_t *in_st_mode, int flag)
Definition: aaip_0_2.c:720

References aaip_cleanout_st_mode().

Referenced by aaip_get_attr_list().

◆ aaip_get_attr_list()

int aaip_get_attr_list ( char *  path,
size_t *  num_attrs,
char ***  names,
size_t **  value_lengths,
char ***  values,
int  flag 
)

Definition at line 399 of file aaip-os-freebsd.c.

401 {
402  int ret;
403  ssize_t i, num_names= 0;
404 
405 #ifdef Libisofs_with_aaip_acL
406  unsigned char *a_acl= NULL;
407  char *a_acl_text= NULL;
408  size_t a_acl_len= 0;
409 #endif
410 #ifdef Libisofs_with_freebsd_extattR
411  char *list= NULL, *user_list= NULL, *sys_list= NULL;
412  ssize_t value_ret, list_size= 0, user_list_size= 0;
413  ssize_t sys_list_size= 0;
414  int acl_names= 0;
415 #endif
416  int no_perm_for_system= 0;
417 
418  if(flag & (1 << 15)) { /* Free memory */
419  {ret= 1; goto ex;}
420  }
421 
422  *num_attrs= 0;
423  *names= NULL;
424  *value_lengths= NULL;
425  *values= NULL;
426 
427  /* Set up arrays */
428 
429 #ifdef Libisofs_with_freebsd_extattR
430 
431  if(!(flag & 4)) { /* Get extattr names */
432 
433  /* Linux : Names are encoded as name NUL
434  FreeBSD: Names are encoded as length_byte:chars (no NUL)
435  AAIP demands names not to contain NUL bytes.
436  */
437 
438  /* Obtain lists of names
439  Must be done separately for namespaces. See man 9 extattr :
440  EXTATTR_NAMESPACE_USER , EXTATTR_NAMESPACE_SYSTEM
441  Must then be marked by "user." and "system." for libisofs use.
442  */
443  ret= aaip_extattr_make_list(path, EXTATTR_NAMESPACE_USER,
444  &user_list, &user_list_size, flag & 32);
445  if(ret <= 0)
446  {ret= -1; goto ex;}
447  if(flag & 8) {
448  ret= aaip_extattr_make_list(path, EXTATTR_NAMESPACE_SYSTEM,
449  &sys_list, &sys_list_size, flag & 32);
450  if(ret <= 0)
451  {ret= -1; goto ex;}
452  if(ret == 3)
453  no_perm_for_system= 1;
454  }
455 
456  /* Check for NUL in names, convert into a linuxish list of namespace.name */
457  ret= aaip_extattr_make_namelist(path, "user", user_list, user_list_size,
458  &list, &list_size, &num_names, 0);
459  if(ret <= 0)
460  goto ex;
461  ret= aaip_extattr_make_namelist(path, "system", sys_list, sys_list_size,
462  &list, &list_size, &num_names, 1);
463  if(ret <= 0)
464  goto ex;
465  }
466 
467 #endif /* Libisofs_with_freebsd_extattR */
468 
469 #ifdef Libisofs_with_aaip_acL
470  if(flag & 1) {
471  num_names++;
472 
473 #ifdef Libisofs_with_freebsd_extattR
474  acl_names= 1;
475 #endif
476 
477  }
478 #endif
479 
480  if(num_names == 0)
481  {ret= 1; goto ex;}
482  (*names)= calloc(num_names, sizeof(char *));
483  (*value_lengths)= calloc(num_names, sizeof(size_t));
484  (*values)= calloc(num_names, sizeof(char *));
485  if(*names == NULL || *value_lengths == NULL || *values == NULL)
486  {ret= -1; goto ex;}
487 
488  for(i= 0; i < num_names; i++) {
489  (*names)[i]= NULL;
490  (*values)[i]= NULL;
491  (*value_lengths)[i]= 0;
492  }
493 
494 #ifdef Libisofs_with_freebsd_extattR
495 
496  if(!(flag & 4)) { /* Get xattr values */
497  for(i= 0; i < list_size && (size_t) num_names - acl_names > *num_attrs;
498  i+= strlen(list + i) + 1) {
499  if(!(flag & 8))
500  if(strncmp(list + i, "user.", 5))
501  continue;
502  (*names)[(*num_attrs)++]= strdup(list + i);
503  if((*names)[(*num_attrs) - 1] == NULL)
504  {ret= -1; goto ex;}
505  }
506 
507  for(i= 0; (size_t) i < *num_attrs; i++) {
508  value_ret= get_single_attr(path, (*names)[i], *value_lengths + i,
509  *values + i, flag & (8 | 32));
510  if(value_ret <= 0)
511  {ret= -1; goto ex;}
512  }
513  }
514 
515 #endif /* Libisofs_with_freebsd_extattR */
516 
517 #ifdef Libisofs_with_aaip_acL
518 
519  if(flag & 1) { /* Obtain ACL */
520  /* access-ACL */
521  aaip_get_acl_text(path, &a_acl_text, flag & (16 | 32));
522  if(a_acl_text == NULL) {
523  /* empty ACL / only st_mode info was found in ACL */
524  ret= 1 + no_perm_for_system;
525  goto ex;
526  }
527  ret= aaip_encode_acl(a_acl_text, (mode_t) 0, &a_acl_len, &a_acl, flag & 2);
528  if(ret <= 0)
529  goto ex;
530 
531  /* Note: There are no default-ACL in FreeBSD */
532 
533  /* Set as attribute with empty name */;
534  (*names)[*num_attrs]= strdup("");
535  if((*names)[*num_attrs] == NULL)
536  {ret= -1; goto ex;}
537  (*values)[*num_attrs]= (char *) a_acl;
538  a_acl= NULL;
539  (*value_lengths)[*num_attrs]= a_acl_len;
540  (*num_attrs)++;
541  }
542 
543 #endif /* Libisofs_with_aaip_acL */
544 
545  ret= 1 + no_perm_for_system;
546 ex:;
547 #ifdef Libisofs_with_aaip_acL
548  if(a_acl != NULL)
549  free(a_acl);
550  if(a_acl_text != NULL)
551  aaip_get_acl_text("", &a_acl_text, 1 << 15); /* free */
552 #endif
553 #ifdef Libisofs_with_freebsd_extattR
554  if(list != NULL)
555  free(list);
556  if(user_list != NULL)
557  free(user_list);
558  if(sys_list != NULL)
559  free(sys_list);
560 #endif
561 
562  if(ret <= 0 || (flag & (1 << 15))) {
563  if(*names != NULL) {
564  for(i= 0; (size_t) i < *num_attrs; i++)
565  free((*names)[i]);
566  free(*names);
567  }
568  *names= NULL;
569  if(*value_lengths != NULL)
570  free(*value_lengths);
571  *value_lengths= NULL;
572  if(*values != NULL) {
573  for(i= 0; (size_t) i < *num_attrs; i++)
574  free((*values)[i]);
575  free(*values);
576  }
577  *values= NULL;
578  *num_attrs= 0;
579  }
580  return(ret);
581 }
int aaip_get_acl_text(char *path, char **text, int flag)
int aaip_encode_acl(char *acl_text, mode_t st_mode, size_t *result_len, unsigned char **result, int flag)
Definition: aaip_0_2.c:292

References aaip_encode_acl(), and aaip_get_acl_text().

◆ aaip_local_attr_support()

int aaip_local_attr_support ( int  flag)

Definition at line 63 of file aaip-os-freebsd.c.

64 {
65  int ret= 0;
66 
67 #ifdef Libisofs_with_aaip_acL
68  if(flag & 1)
69  ret|= 1;
70 #endif
71 #ifdef Libisofs_with_freebsd_extattR
72  if(flag & 2)
73  ret|= 2;
74 #endif
75 
76  return(ret);
77 }

◆ aaip_set_acl_text()

int aaip_set_acl_text ( char *  path,
char *  text,
int  flag 
)

Definition at line 725 of file aaip-os-freebsd.c.

726 {
727 
728 #ifdef Libisofs_with_aaip_acL
729 
730  int ret;
731  acl_t acl= NULL;
732  struct stat stbuf;
733 
734  if(flag & 32)
735  ret= stat(path, &stbuf);
736  else
737  ret= lstat(path, &stbuf);
738  if(ret == -1)
739  return(-1);
740  if((stbuf.st_mode & S_IFMT) == S_IFLNK)
741  return(-2);
742 
743  acl= acl_from_text(text);
744  if(acl == NULL) {
745  ret= -1; goto ex;
746  }
747 
748  /* Note: no ACL_TYPE_DEFAULT in FreeBSD */
749  if(flag & 1)
750  {ret= 0; goto ex;}
751 
752  ret= acl_set_file(path, ACL_TYPE_ACCESS, acl);
753 
754  if(ret == -1)
755  goto ex;
756  ret= 1;
757 ex:
758  if(acl != NULL)
759  acl_free(acl);
760  return(ret);
761 
762 #else /* Libisofs_with_aaip_acL */
763 
764  return(0);
765 
766 #endif /* ! Libisofs_with_aaip_acL */
767 
768 }

Referenced by aaip_set_attr_list().

◆ aaip_set_attr_list()

int aaip_set_attr_list ( char *  path,
size_t  num_attrs,
char **  names,
size_t *  value_lengths,
char **  values,
int *  errnos,
int  flag 
)

Definition at line 837 of file aaip-os-freebsd.c.

840 {
841  int ret, has_default_acl= 0, end_ret= 1;
842  size_t i, consumed, acl_text_fill, acl_idx= 0;
843  char *acl_text= NULL;
844 #ifdef Libisofs_with_freebsd_extattR
845  char *user_list= NULL, *sys_list= NULL, *namept, *old_value;
846  ssize_t user_list_size= 0, sys_list_size= 0, value_ret;
847  int attrnamespace;
848  size_t old_value_l;
849  int skip;
850 #endif
851 
852  for(i= 0; i < num_attrs; i++)
853  errnos[i]= 0;
854 
855 #ifdef Libisofs_with_freebsd_extattR
856 
857  if(flag & 2) { /* Delete all file attributes */
858  ret= aaip_extattr_make_list(path, EXTATTR_NAMESPACE_USER,
859  &user_list, &user_list_size, flag & 32);
860  if(ret <= 0)
861  {ret= -1; goto ex;}
862  ret= aaip_extattr_delete_names(path, EXTATTR_NAMESPACE_USER,
863  user_list, user_list_size, flag & 32);
864  if(ret <= 0)
865  {ret= -5; goto ex;}
866  if(flag & 8) {
867  ret= aaip_extattr_make_list(path, EXTATTR_NAMESPACE_SYSTEM,
868  &sys_list, &sys_list_size, flag & 32);
869  if(ret <= 0)
870  {ret= -5; goto ex;}
871  ret= aaip_extattr_delete_names(path, EXTATTR_NAMESPACE_SYSTEM,
872  sys_list, sys_list_size, flag & 32);
873  if(ret <= 0)
874  {ret= -5; goto ex;}
875  }
876  }
877 
878 #endif /* Libisofs_with_freebsd_extattR */
879 
880  for(i= 0; i < num_attrs; i++) {
881  if(names[i] == NULL || values[i] == NULL)
882  continue;
883  if(names[i][0] == 0) { /* ACLs */
884  if(flag & 1)
885  acl_idx= i + 1;
886  continue;
887  }
888  /* Extended Attribute */
889  if(flag & 4)
890  continue;
891 
892 #ifdef Libisofs_with_freebsd_extattR
893 
894  if(strncmp(names[i], "user.", 5) == 0) {
895  attrnamespace= EXTATTR_NAMESPACE_USER;
896  namept= names[i] + 5;
897  } else if(strncmp(names[i], "isofs.", 6) == 0 || !(flag & 8)) {
898  continue;
899  } else if(strncmp(names[i], "system.", 7) == 0) {
900  attrnamespace= EXTATTR_NAMESPACE_SYSTEM;
901  namept= names[i] + 7;
902  } else {
903  register_errno(errnos, i, (int) EFAULT);
904  end_ret= -8;
905  continue;
906  }
907  skip= 0;
908  if(flag & 128) {
909  value_ret= get_single_attr(path, names[i], &old_value_l,
910  &old_value, flag & (8 | 32));
911  if(value_ret > 0 && old_value_l == value_lengths[i]) {
912  if(memcmp(old_value, values[i], value_lengths[i]) == 0)
913  skip= 1;
914  }
915  if(old_value != NULL)
916  free(old_value);
917  }
918  if(!skip) {
919  if(flag & 32)
920  ret= extattr_set_file(path, attrnamespace, namept,
921  values[i], value_lengths[i]);
922  else
923  ret= extattr_set_link(path, attrnamespace, namept,
924  values[i], value_lengths[i]);
925  if(ret == -1) {
926  register_errno(errnos, i, errno);
927  if(end_ret != 1)
928  end_ret= -4;
929  continue;
930  }
931  }
932 
933 #else
934 
935  if(strncmp(names[i], "user.", 5) == 0)
936  ;
937  else if(strncmp(names[i], "isofs.", 6) == 0 || !(flag & 8))
938  continue;
939  {ret= -6; goto ex;}
940 
941 #endif /* Libisofs_with_freebsd_extattR */
942 
943  }
944 
945  /* Decode ACLs */
946  /* It is important that this happens after restoring xattr which might be
947  representations of ACL, too. If isofs ACL are enabled, then they shall
948  override the xattr ones.
949  */
950  if(acl_idx == 0)
951  {ret= end_ret; goto ex;}
952  i= acl_idx - 1;
953 
954  ret= aaip_decode_acl((unsigned char *) values[i], value_lengths[i],
955  &consumed, NULL, 0, &acl_text_fill, 1);
956  if(ret < -3)
957  goto ex;
958  if(ret <= 0)
959  {ret= -2; goto ex;}
960  acl_text= calloc(acl_text_fill, 1);
961  if(acl_text == NULL)
962  {ret= -1; goto ex;}
963  ret= aaip_decode_acl((unsigned char *) values[i], value_lengths[i],
964  &consumed, acl_text, acl_text_fill, &acl_text_fill, 0);
965  if(ret < -3)
966  goto ex;
967  if(ret <= 0)
968  {ret= -2; goto ex;}
969  has_default_acl= (ret == 2);
970 
971 #ifdef Libisofs_with_aaip_acL
972  ret= aaip_set_acl_text(path, acl_text, flag & (32 | 64));
973  if(ret == -1)
974  register_errno(errnos, i, errno);
975  if(ret <= 0)
976  {ret= -3; goto ex;}
977 #else
978  {ret= -7; goto ex;}
979 #endif
980 
981  if(has_default_acl && !(flag & 64))
982  {ret= -3; goto ex;}
983 
984  ret= end_ret;
985 ex:;
986  if(acl_text != NULL)
987  free(acl_text);
988 #ifdef Libisofs_with_freebsd_extattR
989  if(user_list != NULL)
990  free(user_list);
991  if(sys_list != NULL)
992  free(sys_list);
993 #endif /* Libisofs_with_freebsd_extattR */
994  return(ret);
995 }
int aaip_set_acl_text(char *path, char *text, int flag)
static void register_errno(int *errnos, int i, int in_errno)
int aaip_decode_acl(unsigned char *data, size_t num_data, size_t *consumed, char *acl_text, size_t acl_text_size, size_t *acl_text_fill, int flag)
Definition: aaip_0_2.c:2103

References aaip_decode_acl(), aaip_set_acl_text(), and register_errno().

◆ register_errno()

static void register_errno ( int *  errnos,
int  i,
int  in_errno 
)
static

Definition at line 804 of file aaip-os-freebsd.c.

805 {
806  if(in_errno > 0)
807  errnos[i]= in_errno;
808  else
809  errnos[i]= -1;
810 }

Referenced by aaip_set_attr_list().