+
+
+/*
+ * precreate a folder
+ * this is only intended for folders in the volume root
+ * It will *not* work if the folder name contains extended characters
+ */
+static int create_special_folder (const struct vol *vol, const struct _special_folder *folder)
+{
+ char *p,*q,*r;
+ struct adouble ad;
+ u_int16_t attr;
+ struct stat st;
+ int ret;
+
+
+ p = (char *) malloc ( strlen(vol->v_path)+strlen(folder->name)+2);
+ if ( p == NULL) {
+ LOG(log_error, logtype_afpd,"malloc failed");
+ exit (EXITERR_SYS);
+ }
+
+ q=strdup(folder->name);
+ if ( q == NULL) {
+ LOG(log_error, logtype_afpd,"malloc failed");
+ exit (EXITERR_SYS);
+ }
+
+ strcpy(p, vol->v_path);
+ strcat(p, "/");
+
+ r=q;
+ while (*r) {
+ if ((vol->v_casefold & AFPVOL_MTOUUPPER))
+ *r=toupper(*r);
+ else if ((vol->v_casefold & AFPVOL_MTOULOWER))
+ *r=tolower(*r);
+ r++;
+ }
+ strcat(p, q);
+
+ if ( (ret = stat( p, &st )) < 0 ) {
+ if (folder->precreate) {
+ if (ad_mkdir(p, folder->mode)) {
+ LOG(log_debug, logtype_afpd,"Creating '%s' failed in %s: %s", p, vol->v_path, strerror(errno));
+ free(p);
+ free(q);
+ return -1;
+ }
+ ret = 0;
+ }
+ }
+
+ if ( !ret && folder->hide) {
+ /* Hide it */
+ ad_init(&ad, vol->v_adouble, vol->v_ad_options);
+ if (ad_open( p, vol_noadouble(vol) | ADFLAGS_HF|ADFLAGS_DIR,
+ O_RDWR|O_CREAT, 0666, &ad) < 0) {
+ free (p);
+ free(q);
+ return (-1);
+ }
+ if ((ad_get_HF_flags( &ad ) & O_CREAT) ) {
+ if (ad_getentryoff(&ad, ADEID_NAME)) {
+ ad_setentrylen( &ad, ADEID_NAME, strlen(folder->name));
+ memcpy(ad_entry( &ad, ADEID_NAME ), folder->name,
+ ad_getentrylen( &ad, ADEID_NAME ));
+ }
+ }
+
+ ad_getattr(&ad, &attr);
+ attr |= htons( ntohs( attr ) | ATTRBIT_INVISIBLE );
+ ad_setattr(&ad, attr);
+
+ /* do the same with the finder info */
+ if (ad_entry(&ad, ADEID_FINDERI)) {
+ memcpy(&attr, ad_entry(&ad, ADEID_FINDERI) + FINDERINFO_FRFLAGOFF, sizeof(attr));
+ attr |= htons(FINDERINFO_INVISIBLE);
+ memcpy(ad_entry(&ad, ADEID_FINDERI) + FINDERINFO_FRFLAGOFF,&attr, sizeof(attr));
+ }
+
+ ad_flush( &ad, ADFLAGS_HF );
+ ad_close( &ad, ADFLAGS_HF );
+ }
+ free(p);
+ free(q);
+ return 0;
+}
+
+static void handle_special_folders (const struct vol * vol)
+{
+ const _special_folder *p = &special_folders[0];
+
+ if ((vol->v_flags & AFPVOL_RO))
+ return;
+
+ for (; p->name != NULL; p++) {
+ create_special_folder (vol, p);
+ }
+}
+
+/*
+ * Save the volume options to a file, used by
+ * shell utilities.
+ * Writing the file everytime a volume is opened is
+ * unnecessary, but it shouldn't hurt much.
+ */
+static int savevoloptions (const struct vol *vol)
+{
+ char buf[16348];
+ char item[MAXPATHLEN];
+ int fd;
+ int ret = 0;
+ struct flock lock;
+ const _vol_opt_name *op = &vol_opt_names[0];
+ const _vol_opt_name *cf = &vol_opt_casefold[0];
+
+ strlcpy (item, vol->v_path, sizeof(item));
+ strlcat (item, "/.AppleDesktop/", sizeof(item));
+ strlcat (item, VOLINFOFILE, sizeof(item));
+
+ if ((fd = open( item, O_RDWR | O_CREAT , 0666)) <0 ) {
+ LOG(log_debug, logtype_afpd,"Error opening %s: %s", item, strerror(errno));
+ return (-1);
+ }
+
+ /* try to get a lock */
+ lock.l_start = 0;
+ lock.l_whence = SEEK_SET;
+ lock.l_len = 0;
+ lock.l_type = F_WRLCK;
+
+ if (fcntl(fd, F_SETLK, &lock) < 0) {
+ if (errno == EACCES || errno == EAGAIN) {
+ /* ignore, other process already writing the file */
+ return 0;
+ } else {
+ LOG(log_error, logtype_cnid, "savevoloptions: cannot get lock: %s", strerror(errno));
+ return (-1);
+ }
+ }
+
+ /* write volume options */
+ snprintf(buf, sizeof(buf), "MAC_CHARSET:%s\n", vol->v_maccodepage);
+ snprintf(item, sizeof(item), "VOL_CHARSET:%s\n", vol->v_volcodepage);
+ strlcat(buf, item, sizeof(buf));
+
+ switch (vol->v_adouble) {
+ case AD_VERSION1:
+ strlcat(buf, "ADOUBLE_VER:v1\n", sizeof(buf));
+ break;
+ case AD_VERSION2:
+ strlcat(buf, "ADOUBLE_VER:v2\n", sizeof(buf));
+ break;
+ case AD_VERSION2_OSX:
+ strlcat(buf, "ADOUBLE_VER:osx\n", sizeof(buf));
+ break;
+ }
+
+ strlcat(buf, "CNIDBACKEND:", sizeof(buf));
+ strlcat(buf, vol->v_cnidscheme, sizeof(buf));
+ strlcat(buf, "\n", sizeof(buf));
+
+ strlcat(buf, "CNIDDBDHOST:", sizeof(buf));
+ strlcat(buf, Cnid_srv, sizeof(buf));
+ strlcat(buf, "\n", sizeof(buf));
+
+ snprintf(item, sizeof(item), "CNIDDBDPORT:%u\n", Cnid_port);
+ strlcat(buf, item, sizeof(buf));
+
+ strcpy(item, "CNID_DBPATH:");
+ if (vol->v_dbpath)
+ strlcat(item, vol->v_dbpath, sizeof(item));
+ else
+ strlcat(item, vol->v_path, sizeof(item));
+ strlcat(item, "\n", sizeof(item));
+ strlcat(buf, item, sizeof(buf));
+
+ /* volume flags */
+ strcpy(item, "VOLUME_OPTS:");
+ for (;op->name; op++) {
+ if ( (vol->v_flags & op->option) ) {
+ strlcat(item, op->name, sizeof(item));
+ strlcat(item, " ", sizeof(item));
+ }
+ }
+ strlcat(item, "\n", sizeof(item));
+ strlcat(buf, item, sizeof(buf));
+
+ /* casefold flags */
+ strcpy(item, "VOLCASEFOLD:");
+ for (;cf->name; cf++) {
+ if ( (vol->v_casefold & cf->option) ) {
+ strlcat(item, cf->name, sizeof(item));
+ strlcat(item, " ", sizeof(item));
+ }
+ }
+ strlcat(item, "\n", sizeof(item));
+ strlcat(buf, item, sizeof(buf));
+
+ if (strlen(buf) >= sizeof(buf)-1)
+ LOG(log_debug, logtype_afpd,"Error writing .volinfo file: buffer too small, %s", buf);
+
+
+ if (write( fd, buf, strlen(buf)) < 0 || ftruncate(fd, strlen(buf)) < 0 ) {
+ LOG(log_debug, logtype_afpd,"Error writing .volinfo file: %s", strerror(errno));
+ }
+
+ lock.l_type = F_UNLCK;
+ fcntl(fd, F_SETLK, &lock);
+ close (fd);
+ return ret;
+}