home *** CD-ROM | disk | FTP | other *** search
- /* setdesc.c - Set a file description
- *
- * Copyright (c) 1991, 1992 Tim Cook.
- * Non-profit distribution allowed. See README for details.
- */
-
- static char rcsid[] = "$Id: setdesc.c,v 1.3 1992/12/02 03:51:00 tim Exp $";
-
- #include "config.h"
- #include <fcntl.h>
- #include <sys/stat.h>
- #include <sys/errno.h>
-
- extern int errno ;
- extern int _desc_parse_path () ;
- extern char *_desc_pathname ;
- extern char *_desc_directory ;
- extern char *_desc_name ;
- extern int _initdesc () ;
- #ifdef NDBM
- extern DBM *_desc_database ;
- #endif
-
-
- int setdesc (pathname, directory, name, inode, description)
- char *pathname ;
- char *directory ;
- char *name ;
- ino_t inode ;
- char *description ;
- {
- datum key, content ;
-
- if (! _desc_parse_path (pathname, directory, name)) {
- errno = EINVAL ;
- return FALSE ; }
-
- if (! inode) {
- struct stat file_status ;
-
- if (stat (_desc_pathname, &file_status) == 0)
- inode = file_status.st_ino ; }
-
- if (description == NULL_CP || *description == EOS) {
- /* Set the description to null? This means delete */
-
- if (! _initdesc (_desc_directory, O_RDWR))
- /* If there is no database, that's OK */
- return (errno == ENOENT) ;
-
- if (inode) {
- key.dptr = (char *) &inode ;
- key.dsize = sizeof (ino_t) ;
- DBM_delete (_desc_database, key) ; }
- key.dptr = _desc_name ;
- key.dsize = strlen (_desc_name) ;
- if (key.dsize == sizeof (ino_t))
- key.dsize++ ;
- DBM_delete (_desc_database, key) ;
- return TRUE ; }
-
- if (! _initdesc (_desc_directory, O_RDWR | O_CREAT))
- return FALSE ;
-
- /* Store the description, indexed by the file name */
-
- key.dptr = _desc_name ;
- key.dsize = strlen (_desc_name) ;
-
- /*
- * Name and inode keys are distinguished by the key length. Inode
- * keys are all "sizeof (ino_t)" long, while all other keys are
- * name keys.
- */
-
- if (key.dsize == sizeof (ino_t))
- /*
- * To distinguish this from an inode key, we'll store the
- * terminating null byte as well
- */
- key.dsize++ ;
-
- content.dptr = description ;
- content.dsize = strlen (description) + 1 ;
- if (DBM_store (_desc_database, key, content) < 0)
- return FALSE ;
-
- if (inode) {
-
- /* Store the file name, indexed by the inode number */
-
- key.dptr = (char *) &inode ;
- key.dsize = sizeof (ino_t) ;
- content.dptr = _desc_name ;
- content.dsize = strlen (_desc_name) ;
- if (DBM_store (_desc_database, key, content) < 0)
- return FALSE ;
- }
- return TRUE ;
- }
-