int kernel_unlink(char* path) { struct inode *ip, *dp; struct dirent de; char name[DIRSIZ]; uint off; // if(argstr(0, &path) < 0) // return -1; if((dp = nameiparent(path, name)) == 0) return -1; begin_trans(); ilock(dp); // Cannot unlink "." or "..". if(namecmp(name, ".") == 0 || namecmp(name, "..") == 0) goto bad; if((ip = dirlookup(dp, name, &off)) == 0) goto bad; ilock(ip); if(ip->nlink < 1) panic("unlink: nlink < 1"); /* if(ip->type == T_DIR && !isdirempty(ip)){ iunlockput(ip); goto bad; } */ memset(&de, 0, sizeof(de)); if(writei(dp, (char*)&de, off, sizeof(de)) != sizeof(de)) panic("unlink: writei"); if(ip->type == T_DIR){ dp->nlink--; iupdate(dp); } iunlockput(dp); ip->nlink--; iupdate(ip); iunlockput(ip); commit_trans(); return 0; bad: iunlockput(dp); commit_trans(); return -1; }
// Close file f. (Decrement ref count, close when reaches 0.) void fileclose(struct file *f) { struct file ff; //acquire(&ftable.lock); if(f->ref < 1) printf("fileclose"); if(--f->ref > 0){ //release(&ftable.lock); return; } ff = *f; f->ref = 0; f->type = FD_NONE; //release(&ftable.lock); if(ff.type == FD_PIPE) //pipeclose(ff.pipe, ff.writable); ; else if(ff.type == FD_INODE){ begin_trans(); iput(ff.ip); commit_trans(); } }
// --------------------------------------------------------------------------- // Program entry point // --------------------------------------------------------------------------- int main(int argc, char* argv[]) { if (parser_param(argc, argv) == 1) return 1; sqlite3_open("dblp.db", &db); memset(sql, '\0', sizeof(sql)); strcpy(sql, "create table id_tb (id INTEGER PRIMARY KEY, title TEXT, author TEXT)"); sqlite3_exec(db, sql, NULL, NULL, NULL); begin_trans(); const char* insertSQL = "INSERT INTO id_tb (title, author) VALUES (?,?)"; if (sqlite3_prepare_v2(db, insertSQL, strlen(insertSQL), &stmt2, NULL) != SQLITE_OK) { if (stmt2) sqlite3_finalize(stmt2); sqlite3_close(db); return 0; } xml_initial(); xml_parser(); xml_terminate(); commit_trans(); sqlite3_close(db); if (errorOccurred) return 4; else return 0; }
//PAGEBREAK! // Write to file f. int filewrite(struct file *f, char *addr, int n) { int r; if (f->writable == 0) { return -1; } if (f->type == FD_PIPE) { return pipewrite(f->pipe, addr, n); } if (f->type == FD_INODE) { // write a few blocks at a time to avoid exceeding // the maximum log transaction size, including // i-node, indirect block, allocation blocks, // and 2 blocks of slop for non-aligned writes. // this really belongs lower down, since writei() // might be writing a device like the console. int max = ((LOGSIZE - 1 - 1 - 2) / 2) * 512; int i = 0; while (i < n) { int n1 = n - i; if (n1 > max) { n1 = max; } begin_trans(); ilock(f->ip); if ((r = writei(f->ip, addr + i, f->off, n1)) > 0) { f->off += r; } iunlock(f->ip); commit_trans(); if (r < 0) { break; } if (r != n1) { panic("short filewrite"); } i += r; } return i == n ? n : -1; } panic("filewrite"); }
/*@ requires \true; @ behavior a : @ ensures 0<=\result<=1; */ int main(){ cpt=3; status=0; /*@ loop invariant i : @ 0<=status<=1 @ && 0<=cpt<=3 @ && (cpt==0 ==> status==0); */ while (cpt>0) { status=init_trans(); if (status && (status=commit_trans())) goto label_ok; cpt--; } return 0; label_ok: return 1; }
struct file* kernel_open(char* path, int omode){ int fd; struct file *f; struct inode *ip; if(omode & O_CREATE){ begin_trans(); ip = kernel_create(path, T_FILE, 0, 0); commit_trans(); if(ip == 0) return 0; } else { // cprintf("kernel_open - path is %s\n",path); if((ip = namei(path)) == 0) return 0; //cprintf("kernel_open - path is %s passed namei\n",path); ilock(ip); if(ip->type == T_DIR && omode != O_RDONLY){ iunlockput(ip); return 0; } } // cprintf("kernel_open - before filealloc path %s\n",path); if((f = filealloc()) == 0 || (fd = kernel_fdalloc(f)) < 0){ if(f) fileclose(f); iunlockput(ip); return 0; } iunlock(ip); f->type = FD_INODE; f->ip = ip; f->off = 0; f->readable = !(omode & O_WRONLY); f->writable = (omode & O_WRONLY) || (omode & O_RDWR); return f; //return fd; //return 0; }
/** * Close file f. (Decrement ref count, close when reaches 0.) */ void file_close(struct file *f) { struct file ff; spinlock_acquire(&ftable.lock); if(f->ref < 1) KERN_PANIC("file_close"); if(--f->ref > 0){ spinlock_release(&ftable.lock); return; } ff = *f; f->ref = 0; f->type = FD_NONE; spinlock_release(&ftable.lock); if(ff.type == FD_INODE){ begin_trans(); inode_put(ff.ip); commit_trans(); } }