00001 
00002 
00003 
00004 
00005 
00006 
00007 
00008 
00009 
00010 
00011 
00012 
00013 
00014 
00015 
00016 
00017 
00018 
00019 
00020 
00021 
00022 
00023 
00024 
00025 
00026 
00027 
00028 
00029 
00030 #include <vfs/vfs.h>
00031 #include <vfs/file.h>
00032 #include <ubixos/sched.h>
00033 #include <ubixos/vitals.h>
00034 #include <ubixos/kpanic.h>
00035 #include <ubixos/spinlock.h>
00036 #include <lib/kmalloc.h>
00037 #include <lib/string.h>
00038 #include <vmm/paging.h>
00039 #include <lib/kprintf.h>
00040 #include <assert.h>
00041 
00042 static spinLock_t fdTable_lock = SPIN_LOCK_INITIALIZER;
00043 
00044 
00045 fileDescriptor *fdTable = 0x0;
00046 
00047 
00048 
00049 void sysFwrite(char *ptr,int size,userFileDescriptor *userFd) {
00050   if (userFd == 0x0) {
00051       tty_print(ptr,_current->term);
00052     }
00053   else {
00054     fwrite(ptr,size,1,userFd->fd);
00055     }
00056   return;
00057   }
00058   
00059 void sysFgetc(int *ptr,userFileDescriptor *userFd) {
00060   fileDescriptor *tmpFd = 0x0;
00061   tmpFd = userFd->fd;
00062   if (userFd->fd == 0x0) {
00063     while (1) {
00064       if (_current->term == tty_foreground) {
00065         if ((*ptr = getch()) != 0x0)
00066           return;
00067         sched_yield();
00068         }
00069       else {
00070         sched_yield();
00071         }
00072  
00073 
00074 
00075 
00076 
00077 
00078 
00079 
00080 
00081       }
00082     }
00083   else {
00084     ptr[0] = (int) fgetc(tmpFd);
00085     }
00086   }
00087 
00088 void sysRmDir() {
00089   return;
00090   }
00091 
00092 void sysFseek(userFileDescriptor *userFd,long offset,int whence) {
00093   
00094   if (userFd == NULL)
00095         return;
00096   if (userFd->fd == NULL)
00097         return;
00098 
00099   userFd->fd->offset = offset+whence;
00100   }
00101 
00102 void sysChDir(const char *path) {
00103   if (strstr(path,":") == 0x0) {
00104     sprintf(_current->oInfo.cwd,"%s%s",_current->oInfo.cwd,path);
00105     }
00106   else {
00107     sprintf(_current->oInfo.cwd,path);
00108     }
00109   }
00110 
00111 void sysUnlink(const char *path,int *retVal) {
00112   *retVal = unlink(path);
00113   }
00114 
00115 
00116 
00117 
00118 
00119 
00120 
00121 
00122 void sysFopen(const char *file,char *flags,userFileDescriptor *userFd) {
00123   if (userFd == NULL)
00124     kprintf("Error: userFd == NULL, File: %s, Line: %i\n",__FILE__,__LINE__);
00125   userFd->fd = fopen(file,flags);
00126   if (userFd->fd != 0x0) {
00127     userFd->fdSize = userFd->fd->size;
00128     }
00129   
00130   return;
00131   }
00132   
00133 
00134 
00135 
00136 
00137 
00138 
00139   
00140 void sysFread(void *data,long size,userFileDescriptor *userFd) {
00141   
00142   if (userFd == NULL)
00143         return;
00144   if (userFd->fd == NULL)
00145         return;
00146   fread(data,size,1,userFd->fd);
00147     return;
00148   }
00149 
00150 
00151 
00152 
00153 
00154 
00155 
00156 
00157 void sysFclose(userFileDescriptor *userFd,int *status) {
00158   if (userFd == NULL )
00159   {
00160         *status = -1;
00161         return;
00162   }
00163   if (userFd->fd == NULL)
00164   {
00165         *status = -1;
00166         return;
00167   }
00168   *status = fclose(userFd->fd);
00169   
00170   return;
00171   }
00172   
00173   
00174 
00175 
00176 
00177 
00178 size_t fread(void *ptr,int size,int nmemb,fileDescriptor *fd) {
00179  
00180   if (fd == 0x0)
00181     return(0x0);
00182     
00183  if (nmemb == 0x0) nmemb = 1; 
00184  assert(fd);
00185  
00186  assert(fd->mp);
00187  assert(fd->mp->fs);
00188  fd->mp->fs->vfsRead(fd,ptr,fd->offset,size * nmemb);
00189  fd->offset += size * nmemb;
00190  return(size * nmemb);
00191   }
00192 
00193 size_t fwrite(void *ptr,int size,int nmemb,fileDescriptor *fd) {
00194   if (fd != 0x0) {
00195     fd->mp->fs->vfsWrite(fd,ptr,fd->offset,size * nmemb);
00196     fd->offset += size * nmemb;
00197     }
00198   return(0x0);
00199   }
00200 
00201 int fseek(fileDescriptor *tmpFd,long offset,int whence) {
00202   tmpFd->offset = offset+whence;
00203   return(tmpFd->offset);
00204   }
00205 
00206 
00207 
00208 
00209 
00210 
00211 
00212   
00213 int feof(fileDescriptor *fd) {
00214   if (fd->status == fdEof) {
00215     return(-1);
00216     }
00217   return(0);
00218   }
00219   
00220 
00221 
00222 
00223 
00224 
00225 
00226 
00227 int fputc(int ch,fileDescriptor *fd) {
00228   if (fd != 0x0) {
00229     ch = fd->mp->fs->vfsWrite(fd,(char *)ch,fd->offset,1);
00230     fd->offset++;
00231     return(ch);
00232     }
00233   
00234   return(0x0);
00235   }
00236   
00237 
00238 
00239 
00240 
00241 
00242 
00243   
00244 int fgetc(fileDescriptor *fd) {
00245   int ch = 0x0;
00246   
00247   if (fd != 0x0) {
00248     fd->mp->fs->vfsRead(fd,(char *)&ch,fd->offset,1);
00249     fd->offset++;
00250     return(ch);
00251     }
00252 
00253   
00254   return(0x0);
00255   }
00256 
00257 
00258 
00259 
00260 
00261 
00262 
00263 
00264 
00265 
00266 
00267 fileDescriptor *fopen(const char *file,const char *flags) {
00268   int             i          = 0x0;
00269   char           *path       = 0x0;
00270   char           *mountPoint = 0x0;
00271   char            fileName[1024];
00272   fileDescriptor *tmpFd      = 0x0;
00273   
00274   
00275     if((tmpFd = (fileDescriptor *)kmalloc(sizeof(fileDescriptor))) == 0x0) {
00276       kprintf("Error: tmpFd == NULL, File: %s, Line: %i\n",__FILE__,__LINE__);
00277       return(NULL);
00278     }
00279     
00280   sprintf(fileName,"%s",file);
00281   if (strstr(fileName,":")) {
00282     mountPoint = (char *)strtok((char *)&fileName,":");
00283     path = strtok(NULL,"\n");
00284     }
00285   else {
00286     path = fileName;
00287     
00288     }
00289  
00290   if (path[0] == '/') {
00291     sprintf(tmpFd->fileName,"%s", path);
00292     }
00293   else { 
00294     sprintf(tmpFd->fileName,"/%s",path);
00295     }    
00296 
00297   
00298   if (mountPoint == 0x0) {
00299     tmpFd->mp = vfs_findMount("sys");
00300     }
00301   else {
00302     tmpFd->mp = vfs_findMount(mountPoint);
00303     }
00304   
00305   if (tmpFd->mp == 0x0) {
00306     kprintf("Mount Point Bad\n");
00307     return(0x0);
00308     }
00309     
00310   
00311   tmpFd->mode = 0;
00312   for  (i = 0; '\0' != flags[i] ;i++ ) {
00313     switch(flags[i]) {
00314       case 'w':
00315       case 'W':
00316         tmpFd->mode |= fileWrite;
00317         break;
00318       case 'r':
00319       case 'R':
00320         tmpFd->mode |= fileRead;
00321         break;
00322       case 'b':
00323       case 'B':
00324         tmpFd->mode |= fileBinary;
00325         break;
00326       case 'a':
00327       case 'A':
00328         tmpFd->mode |= fileAppend;
00329         break;
00330       default:
00331         kprintf("Invalid mode '%c' for fopen\n", flags[i]);
00332         break;
00333       }
00334     }
00335   
00336   if (tmpFd->mp->fs->vfsOpenFile(tmpFd->fileName,tmpFd) == 0x1) {
00337     
00338 
00339 
00340    
00341 
00342     tmpFd->buffer = (char *)kmalloc(4096);
00343     if(tmpFd->buffer == 0x0)
00344     {
00345       kfree(tmpFd);
00346       kprintf("Error: tmpFd->buffer == NULL, File: %s, Line: %i\n",__FILE__,__LINE__);
00347       spinUnlock(&fdTable_lock);
00348       return 0x1;
00349     }
00350     
00351     tmpFd->status = fdOpen;
00352     
00353     
00354     tmpFd->offset = 0;
00355     tmpFd->prev = 0x0;
00356 
00357    
00358 
00359       spinLock(&fdTable_lock);
00360 
00361     
00362     systemVitals->openFiles++;
00363     
00364     tmpFd->next = fdTable;
00365     
00366     if (fdTable != 0x0)
00367       fdTable->prev = tmpFd;
00368 
00369      fdTable = tmpFd;
00370 
00371       spinUnlock(&fdTable_lock);
00372       
00373     
00374     
00375     return(tmpFd);
00376     }
00377   else {
00378     kfree(tmpFd->buffer);
00379     kfree(tmpFd);
00380     spinUnlock(&fdTable_lock);
00381     kprintf("File Not Found?\n");
00382     return (NULL);
00383     }
00384     
00385     
00386     return(0x0);
00387   }
00388 
00389 
00390 
00391 
00392 
00393 
00394 
00395 
00396 int fclose(fileDescriptor *fd) {
00397   fileDescriptor *tmpFd = 0x0;
00398   assert(fd);
00399   
00400   spinLock(&fdTable_lock);
00401   
00402   for (tmpFd = fdTable;tmpFd != 0x0;tmpFd = tmpFd->next) {
00403     if (tmpFd == fd) {
00404       if (tmpFd->prev)
00405         tmpFd->prev->next = tmpFd->next;
00406       if (tmpFd->next)
00407         tmpFd->next->prev = tmpFd->prev;
00408 
00409       if (tmpFd == fdTable)
00410         fdTable = tmpFd->next;
00411 
00412       systemVitals->openFiles--;
00413       spinUnlock(&fdTable_lock);
00414         if(tmpFd->buffer != NULL)
00415                 kfree(tmpFd->buffer);
00416       kfree(tmpFd);
00417       return(0x0);      
00418       }
00419     }
00420   
00421   spinUnlock(&fdTable_lock);
00422   return(0x1);
00423   }
00424 
00425 
00426 
00427 
00428 
00429 
00430 
00431 
00432 
00433 
00434 void sysMkDir(const char *path) {
00435   fileDescriptor *tmpFD = 0x0; 
00436   char tmpDir[1024];
00437   char rootPath[256];
00438   char *dir = 0x0;
00439   char *tmp = 0x0;
00440   rootPath[0] = '\0';
00441   dir = (char *)path; 
00442 
00443   if (strstr(path,":") == 0x0) {
00444     sprintf(tmpDir,"%s%s",_current->oInfo.cwd,path);
00445     dir = (char *)&tmpDir;
00446     }
00447   while (strstr(dir,"/")) {
00448     if (rootPath[0] == 0x0)
00449       sprintf(rootPath,"%s/",strtok(dir,"/"));
00450     else
00451       sprintf(rootPath,"%s%s/",rootPath,strtok(dir,"/"));
00452     tmp = strtok(NULL,"\n");
00453     dir = tmp;
00454     }
00455 
00456   
00457   tmpFD = fopen(rootPath,"rb");
00458 
00459   if (tmpFD->mp == 0x0) {
00460     kprintf("Invalid Mount Point\n");
00461     }
00462   tmpFD->mp->fs->vfsMakeDir(dir,tmpFD);
00463 
00464   fclose(tmpFD);
00465 
00466   return;
00467   }
00468 
00469 
00470 
00471 
00472 
00473 
00474 
00475 
00476 
00477 
00478 int unlink(const char *node) {
00479   char *path = 0x0,*mountPoint = 0x0;
00480   struct vfs_mountPoint *mp = 0x0;
00481 
00482   path = (char *)strtok((char *)node,"@");
00483   mountPoint = strtok(NULL,"\n");
00484   if (mountPoint == 0x0) {
00485     mp = vfs_findMount("sys"); 
00486     }
00487   else {
00488     mp = vfs_findMount(mountPoint);
00489     }
00490   if (mp == 0x0) {
00491     
00492     return(0x0);
00493     }
00494   mp->fs->vfsUnlink(path,mp);
00495   return(0x0);
00496   }
00497 
00498 
00499 
00500 
00501 
00502