如何根据返回的不区分大小写的delta删除文件系统中的文件?

时间:2021-03-28 07:37:39

I am attempting to write a code to fulfill this particular case documented in the Dropbox Core API python SDK.

我正在尝试编写代码来完成Dropbox Core API python SDK中记录的这种特殊情况。

[path, nil]: Indicates that there is no file/folder at the path on Dropbox. To update your local state to match, delete whatever is at path, including any children (you will sometimes also get “delete” delta entries for the children, but this is not guaranteed). If your local state doesn’t have anything at path, ignore this entry.

[path,nil]:表示Dropbox上的路径中没有文件/文件夹。要更新您要匹配的本地状态,请删除路径中的任何内容,包括任何子项(您有时也会为子项获取“删除”增量条目,但这不能保证)。如果您的本地州在路径上没有任何内容,请忽略此条目。

The API notes that the returned [path] is case insensitive.

API指出返回的[path]不区分大小写。

Remember: Dropbox treats file names in a case-insensitive but case-preserving way. To facilitate this, the path strings above are lower-cased versions of the actual path. The metadata dicts have the original, case-preserved path.

请记住:Dropbox以不区分大小写但保持大小写的方式处理文件名。为此,上面的路径字符串是实际路径的低级版本。元数据dicts具有原始的,保留大小写的路径。

How do I remove the file or directory in question from my system if I do not know the case-preserved version of the path?

如果我不知道路径的大小写保留版本,如何从我的系统中删除有问题的文件或目录?

If it is relevant, my operating system in is Linux, although I hope to get a solution that will work on Windows as well if at all possible.

如果它是相关的,我的操作系统是Linux,虽然我希望得到一个可以在Windows上运行的解决方案,如果可能的话。

2 个解决方案

#1


1  

If you need to be able to recover the path with the original casing (e.g., for your local case sensitive file system) from the lower cased path, one solution is to keep a mapping of the lowered path to the original path client side. The specific implementation details are up to you, but any simple key value store would probably do the job.

如果您需要能够从较低的套管路径恢复具有原始套管的路径(例如,对于本地区分大小写的文件系统),则一种解决方案是将降低的路径映射到原始路径客户端。具体的实现细节取决于您,但任何简单的键值存储都可能完成这项工作。

Then, when you get one of these deletes, you can use this mapping to lookup the original path and process it accordingly.

然后,当您获得其中一个删除时,可以使用此映射查找原始路径并相应地处理它。

#2


1  

I know this is a bit late but I just stumbled into the same problem and came up with a different solution. Maybe someone looking at this will prefer this method.

我知道这有点晚了但我偶然发现了同样的问题,并想出了一个不同的解决方案。也许看到这个的人会更喜欢这种方法。

Since my API was solely going to be used on a linux server and since deletes would be comparatively a rare occurrence for me, I depended on the linux find command to help me.

由于我的API仅用于Linux服务器,并且由于删除对我来说相当罕见,我依靠linux find命令来帮助我。

    # LINUX ONLY
    cmd = "find {0} -iwholename '{1}'".format(basepath, caseInsensitivePath)
    with os.popen(cmd) as f:
        caseSensitivePath = f.read()[:-1] # -1 to remove the '\n'
        # error if more than 1 line
        if caseSensitivePath.find('\n') != -1:
            print "Found multiple results including: \n", caseSensitivePath
            msg = "[!]ERROR Could not delete {0}. Multiple case-sensitive results exist".format(caseInsensitivePath)
            raise Exception(msg)
        else:
            return caseSensitivePath

basepath is the base path of the find. I would recommend finding a way of using something more precise than root '/'. In my case I already had a list of paths in the sync folder so I was able to do a comparison as follows:

basepath是查找的基本路径。我建议找一种比root'/'更精确的方法。在我的情况下,我已经在sync文件夹中有一个路径列表,所以我能够进行如下比较:

caseInsensitivePath = caseInsensitivePath.lower()
# find basepath
basepath = assets_root
for folder in self.myDict.keys():
    if caseInsensitivePath.lower().startswith(folder.lower()):
        basepath = folder

caseInsensitivePath is the pathname.

caseInsensitivePath是路径名。

#1


1  

If you need to be able to recover the path with the original casing (e.g., for your local case sensitive file system) from the lower cased path, one solution is to keep a mapping of the lowered path to the original path client side. The specific implementation details are up to you, but any simple key value store would probably do the job.

如果您需要能够从较低的套管路径恢复具有原始套管的路径(例如,对于本地区分大小写的文件系统),则一种解决方案是将降低的路径映射到原始路径客户端。具体的实现细节取决于您,但任何简单的键值存储都可能完成这项工作。

Then, when you get one of these deletes, you can use this mapping to lookup the original path and process it accordingly.

然后,当您获得其中一个删除时,可以使用此映射查找原始路径并相应地处理它。

#2


1  

I know this is a bit late but I just stumbled into the same problem and came up with a different solution. Maybe someone looking at this will prefer this method.

我知道这有点晚了但我偶然发现了同样的问题,并想出了一个不同的解决方案。也许看到这个的人会更喜欢这种方法。

Since my API was solely going to be used on a linux server and since deletes would be comparatively a rare occurrence for me, I depended on the linux find command to help me.

由于我的API仅用于Linux服务器,并且由于删除对我来说相当罕见,我依靠linux find命令来帮助我。

    # LINUX ONLY
    cmd = "find {0} -iwholename '{1}'".format(basepath, caseInsensitivePath)
    with os.popen(cmd) as f:
        caseSensitivePath = f.read()[:-1] # -1 to remove the '\n'
        # error if more than 1 line
        if caseSensitivePath.find('\n') != -1:
            print "Found multiple results including: \n", caseSensitivePath
            msg = "[!]ERROR Could not delete {0}. Multiple case-sensitive results exist".format(caseInsensitivePath)
            raise Exception(msg)
        else:
            return caseSensitivePath

basepath is the base path of the find. I would recommend finding a way of using something more precise than root '/'. In my case I already had a list of paths in the sync folder so I was able to do a comparison as follows:

basepath是查找的基本路径。我建议找一种比root'/'更精确的方法。在我的情况下,我已经在sync文件夹中有一个路径列表,所以我能够进行如下比较:

caseInsensitivePath = caseInsensitivePath.lower()
# find basepath
basepath = assets_root
for folder in self.myDict.keys():
    if caseInsensitivePath.lower().startswith(folder.lower()):
        basepath = folder

caseInsensitivePath is the pathname.

caseInsensitivePath是路径名。