Changeset 55


Ignore:
Timestamp:
05/14/2011 07:13:41 AM (2 years ago)
Author:
reyalp
Message:

improve error checking for upload/download arguments, use clean up path handling for downloaddir and deletefiles

Location:
trunk/lua
Files:
3 edited

Legend:

Unmodified
Added
Removed
  • trunk/lua/chdku.lua

    r54 r55  
    456456        end 
    457457        for i,name in ipairs(filenames) do 
    458                 local src = srcpath .. '/' .. name 
    459                 local dst = dstpath .. '/' .. name 
     458                local src = joinpath(srcpath,name) 
     459                local dst = joinpath(dstpath,name) 
    460460                printf("%s -> %s\n",src,dst) 
    461461                status,err = chdk.download(src,dst) 
    462462                if not status then 
    463463                        return status,err 
     464                end 
     465        end 
     466        return true 
     467end 
     468--[[ 
     469quick and dirty bulk delete, this may change or go away 
     470 
     471delete files from directory, optionally matching pattern 
     472note directory should not end in a /, unless it is A/ 
     473only *files* will be deleted, directories will not be touched 
     474]] 
     475function chdku.deletefiles(dir,pattern) 
     476        local files,err=chdku.listdir(dir,{stat="*",match=pattern}) 
     477        if not files then 
     478                return false, err 
     479        end 
     480        for i,st in ipairs(files) do 
     481                if st.is_file then 
     482                        local status,err=chdku.execwait("return os.remove('"..joinpath(dir,st.name).."')") 
     483                        if not status then 
     484                                return false,err 
     485                        end 
     486--                      print('del '..st.name) 
    464487                end 
    465488        end 
     
    755778        end 
    756779end 
    757 --[[ 
    758 quick and dirty bulk delete, this may change or go away 
    759  
    760 delete files from directory, optionally matching pattern 
    761 note directory should not end in a /, unless it is A/ 
    762 only *files* will be deleted, directories will not be touched 
    763 ]] 
    764 function chdku.deletefiles(dir,match) 
    765         local files,err=chdku.listdir(dir,{stat="*",match=match}) 
    766         if not files then 
    767                 return false, err 
    768         end 
    769         -- TODO we should fix up the path properly like CLI does 
    770         if dir ~= 'A/' then 
    771                 dir = dir .. '/' 
    772         end 
    773         for i,st in ipairs(files) do 
    774                 if st.is_file then 
    775                         local status,err=chdku.execwait("return os.remove('"..dir..st.name.."')") 
    776                         if not status then 
    777                                 return false,err 
    778                         end 
    779 --                      print('del '..st.name) 
    780                 end 
    781         end 
    782         return true 
    783 end 
    784780return chdku 
  • trunk/lua/cli.lua

    r53 r55  
    361361                func=function(self,args)  
    362362                        local src,args = cli:get_string_arg(args) 
     363                        if not src then 
     364                                return false, "missing source" 
     365                        end 
    363366                        local dst = cli:get_string_arg(args) 
    364367                        -- no dst, use filename of source 
     
    387390                func=function(self,args)  
    388391                        local src,args = cli:get_string_arg(args) 
     392                        if not src then 
     393                                return false, "missing source" 
     394                        end 
    389395                        local dst = cli:get_string_arg(args) 
    390396                        -- use final component 
     
    392398                                dst = util.basename(src) 
    393399                        -- trailing slash, append filename of source 
     400                        -- TODO should use stat to figure out if target is a directory 
    394401                        elseif string.find(dst,'[\\/]$') then 
    395402                                dst = dst .. util.basename(src) 
    396403                        end 
    397                         if not (src and dst) then 
     404                        if not dst then 
    398405                                return false, "bad/missing args ?" 
    399406                        end 
  • trunk/lua/util.lua

    r35 r55  
    234234]] 
    235235function util.basename(path,sfx) 
     236        if not path then 
     237                return nil 
     238        end 
    236239        local s,e,bn=string.find(path,'([^\\/]+)[\\/]?$') 
    237240        if not s then 
Note: See TracChangeset for help on using the changeset viewer.