static int afd_open(AFFILE *af) { if(af->fname==0 || strlen(af->fname)==0) return -1; // zero-length filenames aren't welcome /* If the name ends with a '/', remove it */ char *lastc = af->fname + strlen(af->fname) - 1; if(*lastc=='/') *lastc = '\000'; /* If the directory doesn't exist, make it (if we are O_CREAT) */ struct stat sb; af->exists = 1; // assume that the directory eixsts if(stat(af->fname,&sb)!=0){ if((af->openflags & O_CREAT) == 0){ // flag not set errno = ENOTDIR; return -1; } mode_t cmask = umask(0); // get the current umask umask(cmask & 077); // make sure we will be able to write the file mkdir(af->fname,af->openmode|0111); // make the directory umask(cmask); // put back the old mask af->exists = 0; // directory doesn't exist; we had to make it. if(stat(af->fname,&sb)) return -1; // error if we can't stat it } /* If this is a regular file, don't open it */ if(!S_ISDIR(sb.st_mode)){ errno = ENOTDIR; // needs to be a directory return -1; } af->maxsize = AFD_DEFAULT_MAXSIZE; af->vnodeprivate = (void *)calloc(1,sizeof(struct afd_private)); struct afd_private *ap = AFD_PRIVATE(af); ap->afs = (AFFILE **)malloc(sizeof(AFFILE *)); /* Open the directory and read all of the AFF files */ DIR *dirp = opendir(af->fname); if(!dirp){ return -1; // something is wrong... } struct dirent *dp; while ((dp = readdir(dirp)) != NULL){ if (af_ext_is(dp->d_name,"aff")){ char path[MAXPATHLEN+1]; strlcpy(path,af->fname,sizeof(path)); strlcat(path,"/",sizeof(path)); strlcat(path,dp->d_name,sizeof(path)); if(afd_add_file(af,path)){ return -1; } } } closedir(dirp); if(ap->num_afs==0 && af->exists){ snprintf(af->error_str,sizeof(af->error_str),".afd directory contains no .aff files!"); return -1; } return 0; // "we were successful" }
/* Return 1 if a file is an AFF file */ static int afd_identify_file(const char *filename,int exists) { if(filename==0 || strlen(filename)==0) return 0; // zero-length filenames aren't welcome if(strncmp(filename,"file://",7)==0){ /* Move file pointer past file:// then find a '/' and take the next character */ filename += 7; while(*filename && *filename!='/'){ filename++; } /* At this point if *filename==0 then we never found the end of the URL. * return 0, since it's not an AFF file. */ if(*filename==0) return 0; /* So *filename must == '/' */ assert(*filename == '/'); filename++; } if(exists && access(filename,R_OK)!=0) return 0; // needs to exist and it doesn't /* If it ends with a '/', remove it */ char *fn = (char *)malloc(strlen(filename)+1); strcpy(fn,filename); char *lastc = fn + strlen(fn) - 1; if(*lastc=='/') *lastc = '\000'; /* If filename exists and it is a dir, it needs to end afd */ struct stat sb; if(stat(fn,&sb)==0){ if((sb.st_mode & S_IFMT)==S_IFDIR){ if(af_ext_is(fn,"afd")){ free(fn); return 1; } } free(fn); return 0; // } /* Doesn't exist. Does it end .afd ? */ if(af_ext_is(fn,"afd")){ free(fn); return 1; } free(fn); return 0; }
/* Return 1 if a file is an AFF file */ static int aff_identify_file(const char *filename,int exists) { if(af_is_filestream(filename)==0) return 0; // not a file stream if(strncmp(filename,"file://",7)==0){ /* Move file pointer past file:// then find a '/' and take the next character */ filename += 7; while(*filename && *filename!='/'){ filename++; } /* At this point if *filename==0 then we never found the end of the URL. * return 0, since it's not an AFF file. */ if(*filename==0) return 0; /* So *filename must == '/' */ assert(*filename == '/'); filename++; } if(exists && access(filename,R_OK)!=0) return 0; // needs to exist and it doesn't int fd = open(filename,O_RDONLY | O_BINARY); if(fd<0){ /* File doesn't exist. Is this an AFF name? */ if(af_ext_is(filename,"aff")) return 1; return 0; } if(fd>0){ int len = strlen(AF_HEADER)+1; char buf[64]; int r = read(fd,buf,len); close(fd); if(r==len){ // if I could read the header if(strcmp(buf,AF_HEADER)==0) return 1; // must be an AFF file return 0; // not an AFF file } /* If it is a zero-length file and the file extension ends AFF, * then let it be an AFF file... */ if(r==0 && af_ext_is(filename,"aff")) return 1; return 0; // must not be an aff file } return 0; }
/* Return 1 if a file is a raw file... */ static int raw_identify_file(const char *filename,int /*exists*/) { return af_ext_is(filename, "raw"); }
/* Return 1 if a file has the AFF header or if the file doesn't * exist and its extension is .afm */ static int afm_identify_file(const char *filename,int exists) { if(exists && access(filename,R_OK)!=0) return 0; // needs to exist and it doesn't return af_ext_is(filename,"afm"); }