int mkpath(const char *s, mode_t mode){ char *q, *r = NULL, *path = NULL, *up = NULL; int rv; rv = -1; if (strcmp(s, ".") == 0 || strcmp(s, "/") == 0) return (0); path=s_strdup(s); q = s_strdup(s); r = s_dirname(q); up = s_strdup(r); if ((mkpath(up, mode) == -1) && (errno != EEXIST)) goto out; if ((mkdir(path, mode) == -1) && (errno != EEXIST)) rv = -1; else rv = 0; out: if (up != NULL) free(up); free(q); free(path); free(r); return (rv); }
void check_inodes() { char *key, *value; int size; int ksize; DDSTAT *ddstat; DBT *data; unsigned long long inode; char *nfi = "NFI"; CRYPTO *crypto; struct stat stbuf; char *blockdatadir; if (NULL == config->blockdatabs) { blockdatadir = s_dirname(config->blockdata); stat(blockdatadir, &stbuf); free(blockdatadir); } else { stat(config->blockdata, &stbuf); } /* traverse records */ tchdbiterinit(dbp); while ((key = tchdbiternext(dbp, &ksize)) != NULL) { show_progress(); if (0 == memcmp(key, nfi, 3)) { value = tchdbget(dbp, key, strlen(key), &size); memcpy(&inode, value, sizeof(unsigned long long)); free(value); } else { memcpy(&inode, key, sizeof(unsigned long long)); data = search_dbdata(dbp, &inode, sizeof(unsigned long long)); if (inode == 0) { crypto = (CRYPTO *) data->data; } else { ddstat = value_to_ddstat(data); if ( 0 == check_inode_orphaned(ddstat)) { if (S_ISREG(ddstat->stbuf.st_mode)) { if ( NULL != config->blockdatabs ) { check_inode_structure(ddstat); } else { file_check_inode_structure(ddstat); } } } else { printf("Deleting corrupted inode %llu\n",(unsigned long long)ddstat->stbuf.st_ino); search_and_delete_dbdirent(ddstat); delete_key(dbp, key, ksize); } ddstatfree(ddstat); } DBTfree(data); } free(key); } }
int lost_mkdir(const char *path) { unsigned long long inode=0; char *rdir; char *pdir; int res; FUNC; inode=get_next_inode(); write_file_ent(path, inode, S_IFDIR | 0755, NULL, 0); rdir = as_sprintf("%s/.", path); inode=get_next_inode(); write_file_ent(rdir, inode, S_IFDIR | 0755, NULL, 0); free(rdir); rdir = as_sprintf("%s/..", path); inode=get_next_inode(); write_file_ent(rdir, inode, S_IFDIR | 0755, NULL, 0); free(rdir); /* Change ctime and mtime of the parentdir Posix std posix behavior */ pdir = s_dirname((char *) path); res = update_parent_time(pdir,1); free(pdir); return (res); }
int file_unlink_file(const char *path) { int res = 0; int haslinks = 0; int dir_links = 0; struct stat st; struct stat dirst; char *dname; char *bname; unsigned char *stiger; unsigned long long inode; unsigned long long counter = 0; INUSE *inuse; unsigned long long done = 0; time_t thetime; void *vdirnode; DBT *bdata; DBT *ddbuf; DBT *dataptr; DDSTAT *ddstat; DINOINO dinoino; INOBNO inobno; char *filename; FUNC; LDEBUG("unlink_file %s", path); res = dbstat(path, &st); if (res == -ENOENT) return (res); inode = st.st_ino; //release_global_lock(); //wait_io_pending(inode); //sync_flush_dtaq(); haslinks = st.st_nlink; thetime = time(NULL); dname = s_dirname((char *) path); /* Change ctime and mtime of the parentdir Posix std posix behavior */ res = update_parent_time(dname,0); bname = s_basename((char *) path); res = dbstat(dname, &dirst); if (S_ISLNK(st.st_mode) && haslinks == 1) { LDEBUG("unlink symlink %s inode %llu", path, inode); delete_key(dbs, &inode, sizeof(unsigned long long)); LDEBUG("unlink symlink done %s", path); } inobno.inode = inode; inobno.blocknr = counter; while (done < st.st_size) { get_dbb_lock(); bdata = search_memhash(dbbm, &inobno, sizeof(INOBNO)); if ( NULL == bdata ) { bdata = search_dbdata(dbb, &inobno, sizeof(INOBNO)); } release_dbb_lock(); if (bdata == NULL) { LDEBUG("%llu is sparse", counter); done = done + BLKSIZE; counter++; inobno.blocknr = counter; continue; } stiger = s_malloc(bdata->size); memcpy(stiger, bdata->data, bdata->size); loghash("search inuse for ", stiger); inuse = file_get_inuse(stiger); if (NULL != inuse) { if (haslinks == 1) { if (inuse->inuse == 1) { loghash("unlink_file delete dbu,dbdta for ", stiger); delete_inuse(stiger); put_on_freelist(inuse); } else { if (inuse->inuse > 1) inuse->inuse--; loghash("updateInUse dbu,dbdta for ", stiger); file_update_inuse(stiger, inuse); } } free(inuse); } else log_fatal_hash("unlink_file inuse not found",stiger); free(stiger); DBTfree(bdata); if (haslinks == 1) { LDEBUG("unlink_file : delete inode %llu - %llu", inobno.inode, inobno.blocknr); delete_dbb(&inobno); } counter++; inobno.blocknr = counter; done = done + BLKSIZE; } if (haslinks == 1) { if (0 != (res = btdelete_curkey(dbdirent, &dirst.st_ino, sizeof(unsigned long long), &inode, sizeof(unsigned long long)))) { free(bname); free(dname); return (res); } delete_key(dbp, (unsigned char *) &inode, sizeof(unsigned long long)); } else { dataptr = search_dbdata(dbp, (unsigned char *) &inode, sizeof(unsigned long long)); if (dataptr == NULL) { die_dataerr("Failed to find file %llu", inode); } ddstat = value_to_ddstat(dataptr); ddstat->stbuf.st_nlink--; ddstat->stbuf.st_ctim.tv_sec = thetime; ddstat->stbuf.st_ctim.tv_nsec=0; ddstat->stbuf.st_mtim.tv_sec = thetime; ddstat->stbuf.st_mtim.tv_nsec=0; dinoino.dirnode = dirst.st_ino; dinoino.inode = ddstat->stbuf.st_ino; dir_links = count_dirlinks(&dinoino, sizeof(DINOINO)); res = btdelete_curkey(dbl, &dinoino, sizeof(DINOINO), bname, strlen(bname)); btdelete_curkey(dbl, &ddstat->stbuf.st_ino, sizeof(unsigned long long), &dinoino, sizeof(DINOINO)); // Restore to regular file settings and clean up. if (ddstat->stbuf.st_nlink == 1) { vdirnode = btsearch_keyval(dbl, &ddstat->stbuf.st_ino, sizeof(unsigned long long), NULL, 0); memcpy(&dinoino, vdirnode, sizeof(DINOINO)); free(vdirnode); filename = (char *) btsearch_keyval(dbl, &dinoino, sizeof(DINOINO), NULL, 0); memcpy(&ddstat->filename, filename, strlen(filename) + 1); free(filename); LDEBUG ("unlink_file : Restore %s to regular file settings and clean up.", ddstat->filename); btdelete_curkey(dbl, &dinoino, sizeof(DINOINO), ddstat->filename, strlen(ddstat->filename)); btdelete_curkey(dbl, &ddstat->stbuf.st_ino, sizeof(unsigned long long), &dinoino, sizeof(DINOINO)); btdelete_curkey(dbl, &inode, sizeof(unsigned long long), &dinoino, sizeof(DINOINO)); res = 0; } if (dir_links == 1) { if (0 != (res = btdelete_curkey(dbdirent, &dirst.st_ino, sizeof(unsigned long long), &inode, sizeof(unsigned long long)))) { die_dataerr("unlink_file : Failed to delete record."); } } ddbuf = create_ddbuf(ddstat->stbuf, ddstat->filename); bin_write_dbdata(dbp, &inode, sizeof(unsigned long long), (void *) ddbuf->data, ddbuf->size); DBTfree(dataptr); DBTfree(ddbuf); ddstatfree(ddstat); } free(bname); free(dname); EFUNC; return (res); }