int MacOpen(const char *path,int oflag, ...) { char RealFname[NAME_MAX]; AssertStr(path,path) RfDfFilen2Real(RealFname,path, MacZip.MacZipMode, MacZip.DataForkOnly, &MacZip.CurrentFork); /* convert to real fname and init global var MacZip.CurrentFork !! */ switch (MacZip.CurrentFork) { case DataFork: { return my_open(RealFname, oflag); break; } case ResourceFork: { return my_open( RealFname, oflag | O_RSRC); break; } default: /* for now (Zip ver 2.3b) MacOpen should never reach this point */ { /* however, this may change in the future ... */ printerr("open: no resource / datafork ",-1,-1,__LINE__,__FILE__,path); return -1; } } }
int main(int argc, char **argv) { int fd; // mandatory to init the myfs layer my_init_lib(); // create the empty file 'test.txt' in the root directory fd = my_open("/test.txt",O_CREATE); if (fd < 0) { printf("[test] unable to open file.\n"); return -1; } // close the file if (my_close(fd) < 0) { printf("[test] error closing file.\n"); return -1; } // check that the fila exists by reopening it without the O_CREATE flag fd = my_open("/test.txt",0); if (fd < 0) { printf("[test] unable to open existing.\n"); return -1; } // close the file if (my_close(fd) < 0) { printf("[test] error closing file.\n"); return -1; } printf("[test] end.\n"); return 0; }
void test_my_open () { unsigned int fd = my_creat("/foo2/hello4.txt"); unsigned int block_num = open_files[fd]; my_close(fd); int i, first_open_slot; for (i = 0; i < MAX_OPEN_FILES; i++) { if(open_files[i] == 0) { first_open_slot = i; i = MAX_OPEN_FILES; } } fd = my_open("/foo2/hello4.txt"); if (fd == first_open_slot) { printf("\ntest_my_open: PASSED\n"); } else { printf("\ntest_my_open: FAILED\n"); } // reset free block list bit setBlockInBitmapToStatus (0, open_files[fd]); my_close(fd); }
int main(int argc, char **argv) { int fd; char fname[1024]; my_init_lib(); for (int i = 0; i < NUM_FILES; i++) { snprintf(fname,sizeof(fname),"/f-%d",i); // create an empty file fd = my_open(fname,O_CREATE); if (fd < 0) { printf("[test] unable to open file.\n"); return -1; } // close the file if (my_close(fd) < 0) { printf("[test] error closing file.\n"); return -1; } // check that the file was added to the directory if(search_file(&fname[1]) <= 0) { // remove the '/' printf("[test] file was not added to the directory.\n"); exit(-1); } } printf("[test] passed.\n"); return 0; }
void put_in_tab(t_all *a) { int x; int y; int i; x = 0; y = 0; my_open(a); my_read(a); while (a->c != '\n') my_read(a); while ((i = my_read(a)) != 0) { if (a->c == '\n') { y++; x = 0; } if (a->c == '.' || a->c == 'o') { a->tab[y][x] = a->c; x++; } } }
void retr(t_server *serv, char *cmd) { int fd; char buf[1]; int nb; if (serv->data->pasv == 0) my_write(serv->client_fd, "425 Use PORT or PASV first\r\n"); else { if (strcmp(cmd, "RETR") == 0) { my_write(serv->client_fd, "550 fail\r\n"); xclose(serv->data->client_fd); } else if ((fd = my_open(serv, cmd)) != -1) { my_write(serv->client_fd, "150 file status OK\r\n"); while ((nb = read(fd, buf, 1)) > 0) write(serv->data->client_fd, buf, 1); xclose(serv->data->client_fd); my_write(serv->client_fd, "226 Transfert completed\r\n"); } } serv->data->pasv = 0; }
static int get_hw_battery_temp(void) { int fd; char buf[64]; char *pmtdbufp = NULL; ssize_t pmtdsize; char *pvalue = NULL; int got_value=0; //open file and read current value fd = my_open("/sys/class/power_supply/battery/batt_temp", O_RDONLY); if (fd < 0) { mtktsbattery_dprintk("[get_hw_battery_temp]: open file fail"); return 0; } mtktsbattery_dprintk("[get_hw_battery_temp]: open file ok"); buf[sizeof(buf) - 1] = '\0'; pmtdsize = sys_read(fd, buf, sizeof(buf) - 1); pmtdbufp = buf; got_value = simple_strtol(pmtdbufp,&pvalue,10); // close file my_close(fd); // debug mtktsbattery_dprintk("[get_hw_battery_temp]: got_value=%d\n", got_value); return got_value; }
/* {{{ mysql_local_infile_init */ static int mysql_local_infile_init(void **ptr, const char *filename, void *userdata) { MYSQL_INFILE_INFO *info; DBUG_ENTER("mysql_local_infile_init"); info = (MYSQL_INFILE_INFO *)my_malloc(sizeof(MYSQL_INFILE_INFO), MYF(MY_ZEROFILL)); if (!info) { DBUG_RETURN(1); } *ptr = info; info->filename = filename; info->fd = my_open(info->filename, O_RDONLY, MYF(0)); if (info->fd < 0) { my_snprintf((char *)info->error_msg, sizeof(info->error_msg), "Can't open file '%-.64s'.", filename); info->error_no = EE_FILENOTFOUND; DBUG_RETURN(1); } DBUG_RETURN(0); }
void symdirget(char *dir) { char buff[FN_REFLEN+1]; char *pos=strend(dir); if (dir[0] && pos[-1] != FN_DEVCHAR && my_access(dir, F_OK)) { File file; size_t length; char temp= *(--pos); /* May be "/" or "\" */ strmov(pos,".sym"); file= my_open(dir, O_RDONLY, MYF(0)); *pos++=temp; *pos=0; /* Restore old filename */ if (file >= 0) { if ((length= my_read(file, buff, sizeof(buff) - 1, MYF(0))) > 0) { for (pos= buff + length ; pos > buff && (iscntrl(pos[-1]) || isspace(pos[-1])) ; pos --); /* Ensure that the symlink ends with the directory symbol */ if (pos == buff || pos[-1] != FN_LIBCHAR) *pos++=FN_LIBCHAR; strmake(dir,buff, (size_t) (pos-buff)); } my_close(file, MYF(0)); } } }
static int do_rotate(LOGGER_HANDLE *log) { char namebuf[FN_REFLEN]; int result; unsigned int i; char *buf_old, *buf_new, *tmp; if (log->rotations == 0) return 0; memcpy(namebuf, log->path, log->path_len); buf_new= logname(log, namebuf, log->rotations); buf_old= log->path; for (i=log->rotations-1; i>0; i--) { logname(log, buf_old, i); if (!access(buf_old, F_OK) && (result= my_rename(buf_old, buf_new, MYF(0)))) goto exit; tmp= buf_old; buf_old= buf_new; buf_new= tmp; } if ((result= my_close(log->file, MYF(0)))) goto exit; namebuf[log->path_len]= 0; result= my_rename(namebuf, logname(log, log->path, 1), MYF(0)); log->file= my_open(namebuf, LOG_FLAGS, MYF(0)); exit: errno= my_errno; return log->file < 0 || result; }
int main(int argc,char *argv[]){ int n; char buffer[MAXLINE]; if(argc != 2) err_quit("usage mycat <filepath>"); if((fd = my_open(argv[1],O_READONLY)) < 0) err_sys("error get file descriptor!"); while((n=Read(fd,buffer,MAXLINE)) <0) Write(fd,buffer,n); exit(0); }
void *my_map_file(char *file) { void *data; int fd; if ((fd = my_open(file)) < 0) return (NULL); data = mmap(NULL, get_file_size(fd), PROT_READ, MAP_SHARED, fd, 0); if (data == (void*)-1 || !data || data == MAP_FAILED) { perror("mmap"); return (NULL); } return (data); }
byte ChangeDisk(byte ID, char *Name) { byte GZ_Format = 0; char* scan = 0; /* We only have MAXDRIVES drives */ if(ID>=MAXDRIVES) return 0; /* Close previous disk image */ my_close( ID ); if(!Name) return 1; scan = strrchr(Name, '.'); if (scan && (!strcasecmp(scan, ".dsz"))) { GZ_Format = 1; } //fprintf(stdout, "ChangeDisk id=%d Name=%s gz=%d\n", ID, Name, GZ_Format); /* Open new disk image */ if (my_open( ID, 0, GZ_Format, Name ) < 0) { /* If failed to open for writing, open read-only */ if (my_open( ID, 1, GZ_Format, Name ) < 0) { return 0; } } return 1; }
int main(int argc, char ** argv) { int fd, n; char buff[BUFFSIZE]; if(argc != 2) err_quit("usage: mycat <pathname>"); if( (fd = my_open(argv[1], O_RDONLY)) < 0) err_sys("cannot open %s", argv[1]); while( (n = read(fd, buff, sizeof(buff))) > 0) write(STDOUT_FILENO, buff, n); exit(0); }
int NtxHwCfg_Save(const char *szFileName,int iIsSeek) { int iRet; char *pszFileName = (char *)szFileName; if(0==szFileName) { #ifdef _X86_//[ return 0; #else //][!_X86_ pszFileName = "/dev/mmcblk0"; iIsSeek = 1; #endif //]_X86_ } { int iFd = -1; ssize_t tChk; iFd = my_open(pszFileName,O_RDWR|O_TRUNC|O_CREAT); if(iFd>=0) { if(iIsSeek) { iIsSeek = SYSHWCONFIG_SEEKSIZE; } else { iIsSeek = 0; } my_lseek(iFd,(unsigned int)iIsSeek,SEEK_SET); tChk = my_write(iFd,(unsigned char *)_gptNtxHwCfg,sizeof(_gtNtxHwCfg)); if((int)tChk==sizeof(_gtNtxHwCfg)) { iRet = HWCFG_RET_SUCCESS; } else { iRet = HWCFG_RET_FILEWRITEFAIL; } my_close(iFd);iFd=-1; } else { ERR_MSG("%s : File \"%s\" open fail !\n",__FUNCTION__,pszFileName); iRet = HWCFG_RET_FILEOPENFAIL; } } return iRet; }
static void invoke_s1_open(void *data) { callstate_t *c = (callstate_t *) data; edtk_debug("%s: threadid = %lx", __FUNCTION__, pthread_self()); c->o.ret_int = my_open(c->i.filename, c->i.flags); if (c->o.ret_int >= 0) { c->o.__expect = 1; } else { c->o.__expect = 0; c->o.__expect_errval = errno; /* Danger! Do not put debugging statement before saving error val! */ edtk_debug("%s: threadid = %lx expectation failed!", __FUNCTION__, pthread_self()); } edtk_debug("%s: threadid = %lx done", __FUNCTION__, pthread_self()); }
void reset_file(PAGECACHE_FILE file, char *file_name) { flush_pagecache_blocks(&pagecache, &file1, FLUSH_RELEASE); if (my_close(file1.file, MYF(0)) != 0) { diag("Got error during %s closing from close() (errno: %d)\n", file_name, errno); exit(1); } my_delete(file_name, MYF(0)); if ((file.file= my_open(file_name, O_CREAT | O_TRUNC | O_RDWR, MYF(0))) == -1) { diag("Got error during %s creation from open() (errno: %d)\n", file_name, errno); exit(1); } }
int get_size_of_ty(t_all *a) { int s; s = 0; my_open(a); my_read(a); s++; while (a->c != '\n') { check_num(a); my_read(a); s++; } my_close(a); a->c = 1; return (s); }
void read_write_test(){ int fd; fd = my_open("usr/data/data1/file1_1"); if(fd < 0){ printf("get fd error\n"); return; } char string[MAX_FILE_SIZE]; int i = 0; int count = 0; int j = 0; memset(string, 0, MAX_FILE_SIZE); /* exit(0); */ for(; i < MAX_FILE_SIZE; ++i){ string[i] = 'a'; } for(i = 0; i < 25; ++i){ if(my_write(fd, string, MAX_FILE_SIZE) != MAX_FILE_SIZE){ printf("write error\n"); exit(0); /* return; */ } } /* my_close(fd); */ /* return; */ for(j = 0; j < 25; ++ j){ memset(string, 0, MAX_FILE_SIZE); if(my_read(fd, string, MAX_FILE_SIZE) != MAX_FILE_SIZE){ printf("read error\n"); exit(0); /* return; */ } for(i = 0; i < MAX_FILE_SIZE; ++i){ if(string[i] == 'a'){ count ++; } } } printf("number of a is %d\n", count); my_close(fd); }
NTX_HWCONFIG *NtxHwCfg_Load(const char *szFileName,int iIsSeek) { NTX_HWCONFIG *ptRet = 0; char *pszFileName = (char *)szFileName; if(0==szFileName) { #ifdef _X86_//[ return 0; #else //][!_X86_ pszFileName = "/dev/mmcblk0"; iIsSeek = 1; #endif //]_X86_ } { int iFd = -1; ssize_t tChk; iFd = my_open(pszFileName,O_RDONLY); if(iFd>=0) { if(iIsSeek) { iIsSeek = SYSHWCONFIG_SEEKSIZE; } else { iIsSeek = 0; } my_lseek(iFd,(unsigned int)iIsSeek,SEEK_SET); tChk = my_read(iFd,(unsigned char *)_gptNtxHwCfg,sizeof(_gtNtxHwCfg)); if((int)tChk==sizeof(_gtNtxHwCfg)) { if(NtxHwCfg_ChkCfgHeaderEx(_gptNtxHwCfg,1)>=0) { ptRet = _gptNtxHwCfg; } } my_close(iFd);iFd=-1; } else { ERR_MSG("%s : File \"%s\" open fail !\n",__FUNCTION__,pszFileName); } } return ptRet; }
void count_ty(t_all *a, int s) { char *str; int i; if ((str = malloc(s * sizeof(char))) == NULL) my_exit("Error -> Malloc failed"); i = 0; my_open(a); while (a->c != '\n') { my_read(a); str[i] = a->c; i++; } str[i] = 0; a->c = 1; a->ty = my_get_nbr(str); }
void file_test(){ my_mkdir("usr/data"); show_file_list(); printf("\n"); my_mkdir("src"); my_mkdir("../usr/./data/data1"); show_file_list(); printf("\n"); int fd; fd = my_create("usr/.././usr/data/.././data/data1/file1"); show_file_list(); my_close(fd); fd = my_open("usr/.././usr/data/.././data/data1/file1"); my_close(fd); show_file_list(); }
LOGGER_HANDLE *logger_open(const char *path, unsigned long long size_limit, unsigned int rotations) { LOGGER_HANDLE new_log, *l_perm; /* I don't think we ever need more rotations, but if it's so, the rotation procedure should be adapted to it. */ if (rotations > 999) return 0; new_log.rotations= rotations; new_log.size_limit= size_limit; new_log.path_len= strlen(fn_format(new_log.path, path, mysql_data_home, "", MY_UNPACK_FILENAME)); if (new_log.path_len+n_dig(rotations)+1 > FN_REFLEN) { errno= ENAMETOOLONG; /* File path too long */ return 0; } if ((new_log.file= my_open(new_log.path, LOG_FLAGS, MYF(0))) < 0) { errno= my_errno; /* Check errno for the cause */ return 0; } if (!(l_perm= (LOGGER_HANDLE *) my_malloc(sizeof(LOGGER_HANDLE), MYF(0)))) { my_close(new_log.file, MYF(0)); new_log.file= -1; return 0; /* End of memory */ } *l_perm= new_log; mysql_mutex_init(key_LOCK_logger_service, &l_perm->lock, MY_MUTEX_INIT_FAST); return l_perm; }
static my_bool my_read_charset_file(const char *filename, myf myflags) { uchar *buf; int fd; size_t len, tmp_len; MY_STAT stat_info; if (!my_stat(filename, &stat_info, MYF(myflags)) || ((len= (uint)stat_info.st_size) > MY_MAX_ALLOWED_BUF) || !(buf= (uchar*) my_malloc(len,myflags))) return TRUE; if ((fd=my_open(filename,O_RDONLY,myflags)) < 0) goto error; tmp_len=my_read(fd, buf, len, myflags); my_close(fd,myflags); if (tmp_len != len) goto error; if (my_parse_charset_xml((char*) buf,len,add_collation)) { #ifdef NOT_YET printf("ERROR at line %d pos %d '%s'\n", my_xml_error_lineno(&p)+1, my_xml_error_pos(&p), my_xml_error_string(&p)); #endif } my_free(buf, myflags); return FALSE; error: my_free(buf, myflags); return TRUE; }
File create_temp_file(char *to, const char *dir, const char *prefix, int mode __attribute__((unused)), myf MyFlags __attribute__((unused))) { File file= -1; #ifdef __WIN__ TCHAR path_buf[MAX_PATH-14]; #endif DBUG_ENTER("create_temp_file"); DBUG_PRINT("enter", ("dir: %s, prefix: %s", dir, prefix)); #if defined (__WIN__) /* Use GetTempPath to determine path for temporary files. This is because the documentation for GetTempFileName has the following to say about this parameter: "If this parameter is NULL, the function fails." */ if (!dir) { if (GetTempPath(sizeof(path_buf), path_buf) > 0) dir = path_buf; } /* Use GetTempFileName to generate a unique filename, create the file and release it's handle - uses up to the first three letters from prefix */ if (GetTempFileName(dir, prefix, 0, to) == 0) DBUG_RETURN(-1); DBUG_PRINT("info", ("name: %s", to)); /* Open the file without the "open only if file doesn't already exist" since the file has already been created by GetTempFileName */ if ((file= my_open(to, (mode & ~O_EXCL), MyFlags)) < 0) { /* Open failed, remove the file created by GetTempFileName */ int tmp= my_errno; (void) my_delete(to, MYF(0)); my_errno= tmp; } #elif defined(HAVE_MKSTEMP) { char prefix_buff[30]; uint pfx_len; File org_file; pfx_len= (uint) (strmov(strnmov(prefix_buff, prefix ? prefix : "tmp.", sizeof(prefix_buff)-7),"XXXXXX") - prefix_buff); if (!dir && ! (dir =getenv("TMPDIR"))) dir=P_tmpdir; if (strlen(dir)+ pfx_len > FN_REFLEN-2) { errno=my_errno= ENAMETOOLONG; DBUG_RETURN(file); } strmov(convert_dirname(to,dir,NullS),prefix_buff); org_file=mkstemp(to); if (mode & O_TEMPORARY) (void) my_delete(to, MYF(MY_WME | ME_NOINPUT)); file=my_register_filename(org_file, to, FILE_BY_MKSTEMP, EE_CANTCREATEFILE, MyFlags); /* If we didn't manage to register the name, remove the temp file */ if (org_file >= 0 && file < 0) { int tmp=my_errno; close(org_file); (void) my_delete(to, MYF(MY_WME | ME_NOINPUT)); my_errno=tmp; } } #elif defined(HAVE_TEMPNAM) { extern char **environ; char *res,**old_env,*temp_env[1]; if (dir && !dir[0]) { /* Change empty string to current dir */ to[0]= FN_CURLIB; to[1]= 0; dir=to; } old_env= (char**) environ; if (dir) { /* Don't use TMPDIR if dir is given */ environ=(const char**) temp_env; temp_env[0]=0; } if ((res=tempnam((char*) dir, (char*) prefix))) { strmake(to,res,FN_REFLEN-1); (*free)(res); file=my_create(to,0, (int) (O_RDWR | O_BINARY | O_TRUNC | O_EXCL | O_NOFOLLOW | O_TEMPORARY | O_SHORT_LIVED), MYF(MY_WME)); } else { DBUG_PRINT("error",("Got error: %d from tempnam",errno)); } environ=(const char**) old_env; } #else #error No implementation found for create_temp_file #endif if (file >= 0) thread_safe_increment(my_tmp_file_created,&THR_LOCK_open); DBUG_RETURN(file); }
int my_copy(const char *from, const char *to, myf MyFlags) { size_t Count; my_bool new_file_stat= 0; /* 1 if we could stat "to" */ int create_flag; File from_file,to_file; uchar buff[IO_SIZE]; MY_STAT stat_buff,new_stat_buff; gid_t gid; DBUG_ENTER("my_copy"); DBUG_PRINT("my",("from %s to %s MyFlags %d", from, to, MyFlags)); from_file=to_file= -1; DBUG_ASSERT(!(MyFlags & (MY_FNABP | MY_NABP))); /* for my_read/my_write */ if (MyFlags & MY_HOLD_ORIGINAL_MODES) /* Copy stat if possible */ new_file_stat= test(my_stat((char*) to, &new_stat_buff, MYF(0))); if ((from_file=my_open(from,O_RDONLY | O_SHARE,MyFlags)) >= 0) { if (!my_stat(from, &stat_buff, MyFlags)) { my_errno=errno; goto err; } if (MyFlags & MY_HOLD_ORIGINAL_MODES && new_file_stat) stat_buff=new_stat_buff; create_flag= (MyFlags & MY_DONT_OVERWRITE_FILE) ? O_EXCL : O_TRUNC; if ((to_file= my_create(to,(int) stat_buff.st_mode, O_WRONLY | create_flag | O_BINARY | O_SHARE, MyFlags)) < 0) goto err; while ((Count=my_read(from_file, buff, sizeof(buff), MyFlags)) != 0) { if (Count == (uint) -1 || my_write(to_file,buff,Count,MYF(MyFlags | MY_NABP))) goto err; } /* sync the destination file */ if (MyFlags & MY_SYNC) { if (my_sync(to_file, MyFlags)) goto err; } if (my_close(from_file,MyFlags) | my_close(to_file,MyFlags)) DBUG_RETURN(-1); /* Error on close */ /* Copy modes if possible */ if (MyFlags & MY_HOLD_ORIGINAL_MODES && !new_file_stat) DBUG_RETURN(0); /* File copyed but not stat */ #if !defined(__WIN__) && !defined(__NETWARE__) /* Refresh the new_stat_buff */ if (!my_stat((char*) to, &new_stat_buff, MYF(0))) { my_errno= errno; goto err; } /* Copy modes */ if ((stat_buff.st_mode & 07777) != (new_stat_buff.st_mode & 07777) && chmod(to, stat_buff.st_mode & 07777)) { my_errno= errno; if (MyFlags & (MY_FAE+MY_WME)) my_error(EE_CHANGE_PERMISSIONS, MYF(ME_BELL+ME_WAITTANG), from, errno); goto err; } /* Copy ownership */ if (stat_buff.st_gid == new_stat_buff.st_gid) gid= -1; else gid= stat_buff.st_gid; if ((gid != (gid_t) -1 || stat_buff.st_uid != new_stat_buff.st_uid) && chown(to, stat_buff.st_uid, gid)) { my_errno= errno; if (MyFlags & (MY_FAE+MY_WME)) my_error(EE_CHANGE_OWNERSHIP, MYF(ME_BELL+ME_WAITTANG), from, errno); goto err; } #endif #if !defined(VMS) && !defined(__ZTC__) if (MyFlags & MY_COPYTIME) { struct utimbuf timep; timep.actime = stat_buff.st_atime; timep.modtime = stat_buff.st_mtime; VOID(utime((char*) to, &timep)); /* last accessed and modified times */ } #endif DBUG_RETURN(0); } err: if (from_file >= 0) VOID(my_close(from_file,MyFlags)); if (to_file >= 0) { VOID(my_close(to_file, MyFlags)); /* attempt to delete the to-file we've partially written */ VOID(my_delete(to, MyFlags)); } DBUG_RETURN(-1); } /* my_copy */
int main(int argc, char **argv) { my_init_lib(); // create a simple file tfunc_gen_and_check_file_zbin("/file1.txt", 1025); // create a simple directory tfunc_dir_make("/dir1"); /* * Test errors creating files */ // condition 1: try to create a file in a non existent directory if (my_open("/dir2/file2.txt",O_CREATE) >= 0) { printf("[test] open failed condition 1.\n"); exit(-1); } // condition 2: use the name of a file as subdirectory name in a path if (my_open("/file1.txt/file2.txt",O_CREATE) >= 0) { printf("[test] open failed condition 2.\n"); exit(-1); } // condition 3: use a path without the root if (my_open("file3.txt",O_CREATE) >= 0) { printf("[test] open failed: condition 3.\n"); exit(-1); } // condition 4: use the name of wrong double slash directory if (my_open("//file2.txt",O_CREATE) >= 0) { printf("[test] open failed: condition 4.\n"); exit(-1); } /* * Test errors creating directories */ // condition 1: try to create a directory in a non existent directory if (my_mkdir("/dir2/dir2") != -1) { printf("[test] mkdir failed: condition 1.\n"); exit(-1); } // condition 2: try to create a directory already existent if (my_mkdir("/dir1") != -1) { printf("[test] mkdir failed: condition 2.\n"); exit(-1); } // condition 3: use the name of an existent file if (my_mkdir("/file1.txt") != -1) { printf("[test] mkdir failed: condition 3.\n"); exit(-1); } // condition 4: use the name of a file as subdirectory name in a path if (my_mkdir("/file1.txt/dir2") != -1) { printf("[test] mkdir failed: condition 4.\n"); exit(-1); } // condition 5: create a double shashed directory with subdirs if (my_mkdir("//dir2") != -1) { printf("[test] mkdir failed: condition 5.\n"); exit(-1); } // condition 6: create directory without the root slash if (my_mkdir("dir2") != -1) { printf("[test] mkdir failed: condition 6.\n"); exit(-1); } /* * Check that after all this mess '/' contains only 'file1.txt' and 'dir1' */ char* buf; int numberOfFiles; if(my_listdir("/",&buf, &numberOfFiles)<0){ printf("[test] failed: error listing directory.\n"); return -1; } printf("Contents of '/':\n"); tfunc_dir_print(buf,numberOfFiles); if (numberOfFiles != 2) { printf("[test] failed: wrong number of files in directory.\n"); return -1; } if(tfunc_dir_search_file("file1.txt",buf,numberOfFiles) <= 0) { printf("[test] failed: '/file1.txt' not found.\n"); return -1; } if(tfunc_dir_search_file("dir1",buf,numberOfFiles) <= 0) { printf("[test] failed: '/dir1' not found.\n"); return -1; } free(buf); printf("[test] passed.\n"); return 0; }
int main(int argc, char **argv) { int fd, num; // mandatory to init the myfs layer my_init_lib(); // create a file fd = my_open(F_NAME,O_CREATE); if (fd < 0) { printf("[test] unable to open file.\n"); return -1; } // fill in a buffer with binary data with zeros char data[2*1024]; gen_data_rand_zbin(data,sizeof(data)); // write the data to the file num = my_write(fd, data, sizeof(data)); if (num < sizeof(data)) { printf("[test] error writing data to file.\n"); return -1; } // close the file if (my_close(fd) < 0) { printf("[test] error closing file.\n"); return -1; } /* * check that the file is not corrupted */ // reopen file fd = my_open(F_NAME,0); if (fd < 0) { printf("[test] unable to open file.\n"); return -1; } // read and compare file contents with written data char buffer[512]; int read = 0; while (1) { num = my_read(fd,buffer,sizeof(buffer)); if (num < 0) { printf("[test] error reading data from file.\n"); return -1; } if (num == 0) { break; } if (memcmp(&data[read],buffer,num) != 0) { printf("[test] file contents differ from data.\n"); return -1; } read += num; } // close the file if (my_close(fd) < 0) { printf("[test] error closing file.\n"); return -1; } printf("[test] test passed.\n"); return 0; }
/* =========================================================================== Opens a gzip (.gz) file for reading or writing. The mode parameter is as in fopen ("rb" or "wb"). The file is given either by file descriptor or path name (if fd == -1). az_open returns NULL if the file could not be opened or if there was insufficient memory to allocate the (de)compression state; errno can be checked to distinguish the two cases (if errno is zero, the zlib error is Z_MEM_ERROR). */ int az_open (azio_stream *s, const char *path, int Flags, File fd) { int err; int level = Z_DEFAULT_COMPRESSION; /* compression level */ int strategy = Z_DEFAULT_STRATEGY; /* compression strategy */ s->stream.zalloc = (alloc_func)0; s->stream.zfree = (free_func)0; s->stream.opaque = (voidpf)0; memset(s->inbuf, 0, AZ_BUFSIZE_READ); memset(s->outbuf, 0, AZ_BUFSIZE_WRITE); s->stream.next_in = s->inbuf; s->stream.next_out = s->outbuf; s->stream.avail_in = s->stream.avail_out = 0; s->z_err = Z_OK; s->z_eof = 0; s->in = 0; s->out = 0; s->back = EOF; s->crc = crc32(0L, Z_NULL, 0); s->transparent = 0; s->mode = 'r'; s->version = (unsigned char)az_magic[1]; /* this needs to be a define to version */ s->minor_version= (unsigned char) az_magic[2]; /* minor version */ s->dirty= AZ_STATE_CLEAN; /* We do our own version of append by nature. We must always have write access to take card of the header. */ DBUG_ASSERT(Flags | O_APPEND); DBUG_ASSERT(Flags | O_WRONLY); if (Flags & O_RDWR) s->mode = 'w'; if (s->mode == 'w') { err = deflateInit2(&(s->stream), level, Z_DEFLATED, -MAX_WBITS, 8, strategy); /* windowBits is passed < 0 to suppress zlib header */ s->stream.next_out = s->outbuf; if (err != Z_OK) { destroy(s); return Z_NULL; } } else { s->stream.next_in = s->inbuf; err = inflateInit2(&(s->stream), -MAX_WBITS); /* windowBits is passed < 0 to tell that there is no zlib header. * Note that in this case inflate *requires* an extra "dummy" byte * after the compressed stream in order to complete decompression and * return Z_STREAM_END. Here the gzip CRC32 ensures that 4 bytes are * present after the compressed stream. */ if (err != Z_OK) { destroy(s); return Z_NULL; } } s->stream.avail_out = AZ_BUFSIZE_WRITE; errno = 0; s->file = fd < 0 ? my_open(path, Flags, MYF(0)) : fd; if (s->file < 0 ) { destroy(s); return Z_NULL; } if (Flags & O_CREAT || Flags & O_TRUNC) { s->rows= 0; s->forced_flushes= 0; s->shortest_row= 0; s->longest_row= 0; s->auto_increment= 0; s->check_point= 0; s->comment_start_pos= 0; s->comment_length= 0; s->frm_start_pos= 0; s->frm_length= 0; s->dirty= 1; /* We create the file dirty */ s->start = AZHEADER_SIZE + AZMETA_BUFFER_SIZE; write_header(s); my_seek(s->file, 0, MY_SEEK_END, MYF(0)); } else if (s->mode == 'w') { uchar buffer[AZHEADER_SIZE + AZMETA_BUFFER_SIZE]; my_pread(s->file, buffer, AZHEADER_SIZE + AZMETA_BUFFER_SIZE, 0, MYF(0)); read_header(s, buffer); /* skip the .az header */ my_seek(s->file, 0, MY_SEEK_END, MYF(0)); } else { check_header(s); /* skip the .az header */ } return 1; }
int nisam_panic(enum ha_panic_function flag) { int error=0; LIST *list_element,*next_open; N_INFO *info; DBUG_ENTER("nisam_panic"); pthread_mutex_lock(&THR_LOCK_isam); for (list_element=nisam_open_list ; list_element ; list_element=next_open) { next_open=list_element->next; /* Save if close */ info=(N_INFO*) list_element->data; switch (flag) { case HA_PANIC_CLOSE: pthread_mutex_unlock(&THR_LOCK_isam); /* Not exactly right... */ if (nisam_close(info)) error=my_errno; pthread_mutex_lock(&THR_LOCK_isam); break; case HA_PANIC_WRITE: /* Do this to free databases */ #ifdef CANT_OPEN_FILES_TWICE if (info->s->base.options & HA_OPTION_READ_ONLY_DATA) break; #endif if (flush_key_blocks(info->s->kfile,FLUSH_RELEASE)) error=my_errno; if (info->opt_flag & WRITE_CACHE_USED) if (flush_io_cache(&info->rec_cache)) error=my_errno; if (info->opt_flag & READ_CACHE_USED) { if (flush_io_cache(&info->rec_cache)) error=my_errno; reinit_io_cache(&info->rec_cache,READ_CACHE,0, (pbool) (info->lock_type != F_UNLCK),1); } #ifndef NO_LOCKING if (info->lock_type != F_UNLCK && ! info->was_locked) { info->was_locked=info->lock_type; if (nisam_lock_database(info,F_UNLCK)) error=my_errno; } #else { int save_status=info->s->w_locks; /* Only w_locks! */ info->s->w_locks=0; if (_nisam_writeinfo(info, test(info->update & HA_STATE_CHANGED))) error=my_errno; info->s->w_locks=save_status; info->update&= ~HA_STATE_CHANGED; /* Not changed */ } #endif /* NO_LOCKING */ #ifdef CANT_OPEN_FILES_TWICE if (info->s->kfile >= 0 && my_close(info->s->kfile,MYF(0))) error = my_errno; if (info->dfile >= 0 && my_close(info->dfile,MYF(0))) error = my_errno; info->s->kfile=info->dfile= -1; /* Files aren't open anymore */ break; #endif case HA_PANIC_READ: /* Restore to before WRITE */ #ifdef CANT_OPEN_FILES_TWICE { /* Open closed files */ char name_buff[FN_REFLEN]; if (info->s->kfile < 0) if ((info->s->kfile= my_open(fn_format(name_buff,info->filename,"", N_NAME_IEXT,4),info->mode, MYF(MY_WME))) < 0) error = my_errno; if (info->dfile < 0) { if ((info->dfile= my_open(fn_format(name_buff,info->filename,"", N_NAME_DEXT,4),info->mode, MYF(MY_WME))) < 0) error = my_errno; info->rec_cache.file=info->dfile; } } #endif #ifndef NO_LOCKING if (info->was_locked) { if (nisam_lock_database(info, info->was_locked)) error=my_errno; info->was_locked=0; } #else { int lock_type,w_locks; lock_type=info->lock_type ; w_locks=info->s->w_locks; info->lock_type=0; info->s->w_locks=0; if (_nisam_readinfo(info,0,1)) /* Read changed data */ error=my_errno; info->lock_type=lock_type; info->s->w_locks=w_locks; } /* Don't use buffer when doing next */ info->update|=HA_STATE_WRITTEN; #endif /* NO_LOCKING */ break; } } if (flag == HA_PANIC_CLOSE) VOID(nisam_log(0)); /* Close log if neaded */ pthread_mutex_unlock(&THR_LOCK_isam); if (!error) DBUG_RETURN(0); my_errno=error; DBUG_RETURN(-1); } /* nisam_panic */